In the code from the question, each subplot has its own colorcycle, such that next
gives you the second color of the axes' colorcycle in each subplot.
A solution would be to share the same color cycle among all subplots.
For the case where you create the subplots inside the loop, you might use
import matplotlib.pyplot as plt
import numpy as np
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
for k in xrange(4):
ax1 = plt.subplot2grid((4, 1), (k, 0))
if k==0:
c = ax1._get_lines.prop_cycler
else:
ax1._get_lines.prop_cycler = c
ax1.plot(a[:,k])
plt.legend(labels=lab[k:k+1])
plt.show()
Or in a case where subplots are created all at once,
import matplotlib.pyplot as plt
import numpy as np
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
fig, axes = plt.subplots(4,1)
for k, ax in enumerate(axes):
ax._get_lines.prop_cycler = axes[0]._get_lines.prop_cycler
ax.plot(a[:,k])
ax.legend(labels=lab[k])
plt.show()
Of course you could also use the color cycler from each axes individually and propagate it to the next color
import matplotlib.pyplot as plt
import numpy as np
a = np.cumsum(np.cumsum(np.random.randn(7,4), axis=0), axis=1)
lab = np.array(["A","B","C","E"])
fig, axes = plt.subplots(4,1)
for k, ax in enumerate(axes):
for i in range(k):
next(ax._get_lines.prop_cycler)
ax.plot(a[:,k])
ax.legend(labels=lab[k])
plt.show()
In all cases the result would look like this:
