2

When I plot three lines in one go, matplotlib samples through three steps of the current cycler, giving each its own style:

In [11]: x = linspace(0, 5, 50)

In [12]: y = vstack([x**2, x**3, x**4]).T

In [13]: plot(x, y)
Out[13]: 
[<matplotlib.lines.Line2D at 0x7edfa9144550>,
 <matplotlib.lines.Line2D at 0x7edfa9144b38>,
 <matplotlib.lines.Line2D at 0x7edfa9144cf8>]

I can make them all the same color with plot(x, y, color="black"). However, I don't want them all black; I want that each of the lines respects the current (next) state of the cycle.

Getting the current state is cumbersome at best. I could expand x and flatten y to get all three with the same style:

In [54]: plot(vstack([x]*3).ravel(), y.T.ravel())

but this will create a spurious line connecting the three, so it only works with plot-styles that draw marks but not lines.

Is there a pleasant way to tell matplotlib that I would like to use the same cycle state to draw multiple lines?

Community
  • 1
  • 1
gerrit
  • 24,025
  • 17
  • 97
  • 170

1 Answers1

2

Here via getting the current state: (Not pleasant, but not too cumbersome.)

In [94]: plt.plot(x, y[:,1:], c=plt.plot(x, y[:,0])[0].get_c())
Ulrich Stern
  • 10,761
  • 5
  • 55
  • 76
  • Creative! This also provides a solution for a different challenge, which is how to get the correct legend entries when using `label=x`. – gerrit Jun 24 '16 at 17:16