简体   繁体   中英

Unable to change color of plotted lines on matplotlib

Referred to this SO post here: Matplotlib set_color_cycle versus set_prop_cycle

But I was unable to set all 20 lines' colors to be different, picture of the graph here: 在此处输入图像描述

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:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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