简体   繁体   English

无法更改 matplotlib 上绘制线的颜色

[英]Unable to change color of plotted lines on matplotlib

Referred to this SO post here: Matplotlib set_color_cycle versus set_prop_cycle在此处引用此 SO 帖子: Matplotlib set_color_cycle 与 set_prop_cycle

But I was unable to set all 20 lines' colors to be different, picture of the graph here:但我无法将所有 20 行的 colors 设置为不同,这里的图表图片: 在此处输入图像描述

Here is my code:这是我的代码:

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd
from cycler import cycler


df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)


df.rolling(window=30).max()[30:].head(20)
ax = df.plot()
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
ax.set_prop_cycle('color',plt.cm.jet(np.linspace(0,1,20)))
plt.show()

Do help me out here!请帮帮我!

User DavidG has helped me with the issue, for future reference the updated code will be included here:用户 DavidG 帮助我解决了这个问题,以供将来参考,更新的代码将包含在此处:

import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd  
from cycler import cycler

fig, ax = plt.subplots()

ax.set_prop_cycle('color',plt.cm.tab20(np.linspace(0,1,20)))

df = pd.read_csv(r'data.csv', index_col="Date", parse_dates=True)


df.rolling(window=30).max()[30:].head(20)
df.plot(ax=ax)
ax.set(title='Qingdao Port', ylabel='Monthly Average Prices')
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM