31

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?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Andrew Cheong
  • 3,576
  • 17
  • 42
  • That's odd. I'm running 9.0.1.0 for Windows x64. – Andrew Cheong Sep 05 '14 at 23:22
  • Looks like a bug, but what happens if you invert Sin and Cos and the corresponding list of directives? – Peltio Sep 05 '14 at 23:28
  • @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.) – Andrew Cheong Sep 05 '14 at 23:31

4 Answers4

33

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
11

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.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 3
    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? – m_goldberg Sep 09 '14 at 01:11
  • @m_goldberg Based on that I believe it should so I added the tag. Thank you. – Mr.Wizard Sep 09 '14 at 03:31
4

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

eldo
  • 67,911
  • 5
  • 60
  • 168
  • 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. – Andrew Cheong Sep 06 '14 at 00:09
  • Oh, wait. I didn't see until @Mr.Wizard pointed out, that you're using ListLinePlot instead of ListPlot. – Andrew Cheong Sep 06 '14 at 00:17
  • 1
    @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. – Mr.Wizard Sep 06 '14 at 00:18
1

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

Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78