I would like to change color of individual subplot:
1. Specifying desired color of plots by hand
2. Using random colors
Basic code (taken from 1)
df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot(subplots=True)
plt.legend(loc='best')
plt.show()
I tried this:
colors = ['r','g','b','r'] #first option
colors = list(['r','g','b','r']) #second option
colors = plt.cm.Paired(np.linspace(0,1,4)) #third option
df.plot(subplots=True, color=colors)
But all of them doesn' work. I found 2 but I'm not sure how to change this:
plots=df.plot(subplots=True)
for color in plots:
??????
You can easlity do this by providing the style
parameter with a list of color abbrevations:
from pandas import Series, DataFrame, date_range
import matplotlib.pyplot as plt
import numpy as np
ts = Series(np.random.randn(1000), index=date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
df = DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
ax = df.plot(subplots=True, style=['r','g','b','r'], sharex=True)
plt.legend(loc='best')
plt.tight_layout()
plt.show()
If you only want to use the "standard" colors (blue, green, red, cyan, magenta, yellow, black, white), you can define an array containing the color abbrevations and pass a random sequence of these as argument to the style
parameter:
colors = np.array(list('bgrcmykw'))
...
ax = df.plot(subplots=True,
style=colors[np.random.randint(0, len(colors), df.shape[1])],
sharex=True)
You'll have to loop over each axis by itself and do this manually as far as I can tell. This should probably be a feature.
df = DataFrame(np.random.randn(1000, 4), columns=list('ABCD'))
df = df.cumsum()
colors = 'r', 'g', 'b', 'k'
fig, axs = subplots(df.shape[1], 1,sharex=True, sharey=True)
for ax, color, (colname, col) in zip(axs.flat,colors,df.iteritems()):
ax.plot(col.index,col,label=colname,c=color)
ax.legend()
ax.axis('tight')
fig.tight_layout()
Or if you have dates in your index do:
import pandas.util.testing as tm
df = tm.makeTimeDataFrame()
df = df.cumsum()
colors = 'r', 'g', 'b', 'k'
fig, axs = subplots(df.shape[1], 1,sharex=True, sharey=True)
for ax, color, (colname, col) in zip(axs.flat, colors, df.iteritems()):
ax.plot(col.index, col, label=colname,c=color)
ax.legend()
ax.axis('tight')
fig.tight_layout()
fig.autofmt_xdate()
to get
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With