MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: A Question About Directive

  • To: mathgroup at smc.vnet.net
  • Subject: [mg111158] Re: A Question About Directive
  • From: Brett Champion <brettc at wolfram.com>
  • Date: Thu, 22 Jul 2010 05:42:51 -0400 (EDT)

On Jul 21, 2010, at 6:15 AM, Kevin J. McCann wrote:

> Thanks Murray. I understand, but that seems like very little  
> improvement
> at the cost of a new function. Surely, there must be more. BTW, I  
> think
> I like the old way better - fewer keystrokes, and I am old  
> fashioned ;)
>

The advantage of Directive is that it removes ambiguity.

Here are two plots with PlotStyle -> {Thick, Red}.  In the first one,  
for legacy reasons, the Thick and Red are both applied to the curve.  
In the second the first curve is Thick (with the default used by Plot)  
and the second curve is Red (with the default line weight.)  If you're  
building up your code, this can be a "gotcha".

Plot[Sin[x], {x, 0, Pi}, PlotStyle -> {Thick, Red}]
Plot[{Sin[x], Cos[x]}, {x, 0, Pi}, PlotStyle -> {Thick, Red}]

If you want everything to be Red and Thick, Directive allows you to  
specify that clearly:

Plot[Sin[x], {x, 0, Pi}, PlotStyle -> Directive[Thick, Red]]
Plot[{Sin[x], Cos[x]}, {x, 0, Pi}, PlotStyle -> Directive[Thick, Red]]

Most newer visualization functions only accept Directive as the way to  
specify multiple styles for a single object.

BarChart[Range[5], ChartStyle -> {Orange, EdgeForm[Thick]}]
BarChart[Range[5], ChartStyle -> Directive[Orange, EdgeForm[Thick]]]

Brett Champion
Wolfram Research

> Kevin
>
> Murray Eisenberg wrote:
>> In some situations, using Directive[opt1,opt2] instead of {opt1,opt2}
>> can make code easier to read by avoiding nested parentheses.
>>
>> Compare, for example:
>>
>>   Plot[{Sin[x],Cos[x]},{x,0,Pi},PlotStyle->
>>      {Directive[Thick,Red],Directive[Thick,Dashed,Blue]}]
>>
>>   Plot[{Sin[x], Cos[x]}, {x, 0, Pi},
>>       PlotStyle -> {{Thick, Red}, {Thick, Dashed, Blue}}]
>>
>> On 7/19/2010 2:10 AM, Kevin J. McCann wrote:
>>> I have noticed the use of Directive in some of the graphics examples
>>> from Bob Hanlon, David Park, and others. I am curious about this
>>> command, since it does not appear to do much that is new. For  
>>> example,
>>> here is an example from the Help on Directive:
>>>
>>> Graphics[{Directive[Red, Thick], Circle[],
>>>    Directive[Blue, Opacity[0.5]], Rectangle[{0, -1}, {2, 1}]}]
>>>
>>> However, if I change to this, I get the same thing:
>>>
>>> Graphics[{Red, Thick, Circle[], Blue, Opacity[0.5],
>>>    Rectangle[{0, -1}, {2, 1}]}]
>>>
>>> So, what is the benefit of Directive?
>>>
>>> Thanks,
>>>
>>> Kevin
>>>
>>
>



  • Prev by Date: Re: Scoping constructs Block, Module, ModuleBlock violate
  • Next by Date: Mathematica Special Interest Group (Washington DC Area)
  • Previous by thread: Re: A Question About Directive
  • Next by thread: Re: A Question About Directive