1

Hi I'm plotting several curves and want one of them to be dashed when negative and solid when positive, any idea how this can be obtained?

This is what I have so far ( example ):

 Plot[{v, v - 10 , v + 10}, {v, -9, 9},PlotStyle -> {Blue, Directive[Dashed,Red], Red}]

Cheers

jarhead
  • 2,065
  • 12
  • 19
  • I don't understand what you're asking. Here's a guess, something like PlotStyle -> {Blue, Directive[Dashed, Red], Red}? Or do you want a line to be dashed up to some point and then solid after? -- What did you mean by "the second and second plots"? – Michael E2 Oct 11 '15 at 12:23
  • yes ,say dashed from -9 to 0 and solid from 0 to 9, edited the Q – jarhead Oct 11 '15 at 12:24
  • 1
    How about Plot[{ConditionalExpression[v, v < 0], ConditionalExpression[v, v >= 0]}, {v, -9, 9}, PlotStyle -> {Directive[Blue, Dashed], Blue}]? – J. M.'s missing motivation Oct 11 '15 at 12:56
  • @J.M.isback., like your Comment, pls post as an Answer. –  Oct 11 '15 at 13:18
  • 1
    @MichaelE2 Agree it's a duplicate. – Silvia Oct 11 '15 at 17:37

2 Answers2

5

Here's a brute-force approach with Piecewise

Plot[
 {
  Piecewise[{{v, v < 0}}, None], Piecewise[{{v, v >= 0}}, None],
  Piecewise[{{v - 10, v < 0}}, None], 
  Piecewise[{{v - 10, v >= 0}}, None],
  Piecewise[{{v + 10, v < 0}}, None], 
  Piecewise[{{v + 10, v >= 0}}, None]
  },
 {v, -9, 9},
 PlotStyle -> {Directive[Purple, Dashed], Green, 
   Directive[Blue, Dashed], Pink, Directive[Red, Dashed], Black}
 ]

enter image description here

TransferOrbit
  • 3,547
  • 13
  • 26
3
Show[Plot[{v, v - 10, v + 10}, {v, -9, 0}, 
PlotStyle -> {Directive[Dashed, Blue], Directive[Dashed, Red], 
Directive[Dashed, Red]}], 
Plot[{v, v - 10, v + 10}, {v, 0, 9}, PlotStyle -> {Blue, Red, Red}], 
PlotRange -> All]

enter image description here

Mariusz Iwaniuk
  • 13,841
  • 1
  • 25
  • 41
  • This is indeed a solution, however, I want to obtain this without using Show, I use manipulate and need everything concatenated. – jarhead Oct 11 '15 at 12:42
  • 3
    @jarhead - While you may prefer some other approach/solution, a requirement to use Manipulate does not preclude use of Show. Show is compatible with Manipulate. – Bob Hanlon Oct 11 '15 at 14:11