Mic*_*hal 3 python matplotlib pandas
我想更改单个子图的颜色:
1.手动指定图的期望颜色
2.使用随机颜色
基本代码(取自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()
Run Code Online (Sandbox Code Playgroud)
我尝试了这个:
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)
Run Code Online (Sandbox Code Playgroud)
但是所有这些都不起作用。我发现2,但不确定如何更改:
plots=df.plot(subplots=True)
for color in plots:
??????
Run Code Online (Sandbox Code Playgroud)
您可以通过为style参数提供颜色缩写列表来轻松实现此目的:
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()
Run Code Online (Sandbox Code Playgroud)

如果您只想使用“标准”颜色(蓝色,绿色,红色,青色,品红色,黄色,黑色,白色),则可以定义一个包含颜色缩写的数组,并将它们的随机序列作为参数传递给style参数:
colors = np.array(list('bgrcmykw'))
...
ax = df.plot(subplots=True,
style=colors[np.random.randint(0, len(colors), df.shape[1])],
sharex=True)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3202 次 |
| 最近记录: |