31
$\begingroup$

I'm looking at Graphics Directives but can't figure out a way to turn off the Dashed graphics directive. For example, given

ListPlot[{Table[Sin[i], {i, 25}], Table[Cos[i], {i, 25}]}, 
 Joined -> True, PlotStyle -> {{Red, Thick, Dashed}, {Blue, Thin}}]

enter image description here

how do I restore a solid blue line? Thick can be "disabled" with Thin but what's Dashed's complement?

$\endgroup$
3
  • $\begingroup$ That's odd. I'm running 9.0.1.0 for Windows x64. $\endgroup$ Commented Sep 5, 2014 at 23:22
  • $\begingroup$ Looks like a bug, but what happens if you invert Sin and Cos and the corresponding list of directives? $\endgroup$
    – Peltio
    Commented Sep 5, 2014 at 23:28
  • $\begingroup$ @Peltio - If I switch the directive sets then the results are as expected—sequentially applied. (If you're saying it's one way to achieve my desired results, then I should clarify that this is only a simplified example.) $\endgroup$ Commented Sep 5, 2014 at 23:31

4 Answers 4

33
$\begingroup$

You can turn it off with Dashing[None].

ListPlot[{Table[Sin[i], {i, 25}], Table[Cos[i], {i, 25}]}, 
  Joined -> True, 
  PlotStyle -> {{Red, Thick, Dashed}, {Dashing[None], Blue, Thin}}]

plot

$\endgroup$
4
  • 3
    $\begingroup$ @AndrewCheong "Dashing[{}] specifies that lines should be solid." from the docs of Dashing, and here is the example they give :) $\endgroup$
    – Öskå
    Commented Sep 5, 2014 at 23:35
  • $\begingroup$ @Öskå - I missed that in the documentation. Thanks for pointing it out! $\endgroup$ Commented Sep 5, 2014 at 23:36
  • $\begingroup$ @AndrewCheong So did I.. :) $\endgroup$
    – Öskå
    Commented Sep 5, 2014 at 23:37
  • 3
    $\begingroup$ It is my experience that wherever a graphic directive takes { } as an off switch, it will take None as well. $\endgroup$
    – m_goldberg
    Commented Sep 5, 2014 at 23:38
11
$\begingroup$

You are supposed to use ListLinePlot for line charts since Mathematica 6, which as eldo illustrated handles this correctly.

The behavior of ListPlot is due to the structure of the Graphics object that is produced:

g1 = ListPlot[{Table[Sin[i], {i, 25}], Table[Cos[i], {i, 25}]}, Joined -> True, 
   PlotStyle -> {{Red, Thick, Dashed}, {Blue, Thin}}];

g1[[1]] /. (h : Hue | Line | Directive)[___] :> h[]
{{}, {Hue[], Directive[], Line[], Hue[], Directive[], Line[]}, {}}

Observe that the body is a single list of the form {color, directive, line, color, directive, line, ...}.
This has the implication that any earlier directive which is not expressly overridden by a later one will affect not only the Line immediately following it but all the rest. Colors do not appear to persist in this fashion only because Mathematica automatically provides these styles.

Now compare the output of ListLinePlot:

g2 = ListLinePlot[{Table[Sin[i], {i, 25}], Table[Cos[i], {i, 25}]}, 
   PlotStyle -> {{Red, Thick, Dashed}, {Blue, Thin}}];

g2[[1]] /. (h : Hue | Line | Directive)[___] :> h[]
{{}, {{{}, {}, {Hue[], Directive[], Line[]}, {Hue[], Directive[], Line[]}}}, {}}

Note the structure {{color, directive, line}, {color, directive, line}, ...}. This has the effect of localizing the styling directives to a specific line; they will not persist across multiple lines.

I suppose either behavior could be intentional but I find the latter less surprising, and therefore better.

$\endgroup$
2
  • 3
    $\begingroup$ I reported the behavior of Dashed in ListPlot to WRI tech support, and in their reply they agreed it was a bug. Should the question, therefore, be tagged with the bugs tag? $\endgroup$
    – m_goldberg
    Commented Sep 9, 2014 at 1:11
  • $\begingroup$ @m_goldberg Based on that I believe it should so I added the tag. Thank you. $\endgroup$
    – Mr.Wizard
    Commented Sep 9, 2014 at 3:31
4
$\begingroup$

Somehow easier for me than Mr. Goldberg's excellent answer:

ListLinePlot[{Table[Sin[i], {i, 25}], Table[Cos[i], {i, 25}]},
   PlotStyle -> {{Red, Thick, Dashed}, {Blue, Thin}}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Unfortunately the comments on the question were deleted, but it appears that depending on the version of Mathematica (or perhaps not that, but some other state of the environment), omitting Dashed in subsequent sets of directives does not seem to disable the directive. Hence my asking. $\endgroup$ Commented Sep 6, 2014 at 0:09
  • $\begingroup$ Oh, wait. I didn't see until @Mr.Wizard pointed out, that you're using ListLinePlot instead of ListPlot. $\endgroup$ Commented Sep 6, 2014 at 0:17
  • 1
    $\begingroup$ @Andrew You may have overlooked the difference between ListPlot and ListLinePlot. I expound on this in my own answer below. In my opinion eldo's answer is the correct solution, but m_goldberg's is the direct answer to the specific question you asked. $\endgroup$
    – Mr.Wizard
    Commented Sep 6, 2014 at 0:18
1
$\begingroup$

Based on Mr.Wizard answer , I would suggest for this particular example is to switch the data and to put the dashed data at the end.

ListPlot[{Table[Cos[i], {i, 25}], Table[Sin[i], {i, 25}]}, 
 Joined -> True, PlotStyle -> {{Blue, Thin}, {Red, Thick, Dashed}}]

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.