This question already has an answer here:

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?


marked as duplicate by Simon Woods, ybeltukov, Sjoerd C. de Vries, Michael E2, Mr.Wizard Sep 11 '14 at 22:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ That's odd. I'm running for Windows x64. $\endgroup$ – Andrew Cheong Sep 5 '14 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 Sep 5 '14 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$ – Andrew Cheong Sep 5 '14 at 23:31

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}}]


  • 3
    $\begingroup$ @AndrewCheong "Dashing[{}] specifies that lines should be solid." from the docs of Dashing, and here is the example they give :) $\endgroup$ – Öskå Sep 5 '14 at 23:35
  • $\begingroup$ @Öskå - I missed that in the documentation. Thanks for pointing it out! $\endgroup$ – Andrew Cheong Sep 5 '14 at 23:36
  • $\begingroup$ @AndrewCheong So did I.. :) $\endgroup$ – Öskå Sep 5 '14 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 Sep 5 '14 at 23:38

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.

  • 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 Sep 9 '14 at 1:11
  • $\begingroup$ @m_goldberg Based on that I believe it should so I added the tag. Thank you. $\endgroup$ – Mr.Wizard Sep 9 '14 at 3:31

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

  • $\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$ – Andrew Cheong Sep 6 '14 at 0:09
  • $\begingroup$ Oh, wait. I didn't see until @Mr.Wizard pointed out, that you're using ListLinePlot instead of ListPlot. $\endgroup$ – Andrew Cheong Sep 6 '14 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 Sep 6 '14 at 0:18

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


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