更改matplotlib中多条线的线样式



我用以下代码为不同的绘图线指定了不同的颜色。如果有人能帮助为每一行指定单独的线条样式,我将不胜感激。线条应具有实线或虚线样式。在plot()函数中添加linestyles = linestyles参数会引发错误AttributeError: 'Line2D' object has no property 'linestyles'

fig, ax = plt.subplots()
linestyles =['-', '-', '--', '-', '--', '-', '--']
cmap = matplotlib.colors.LinearSegmentedColormap.from_list("", ['black', 'brown', 'brown', 'orange', 'orange', 'green', 'green'])
new_df_brindabella[['est_fmc', '1h_surface', '1h_profile', '10h_fuel stick',
'100h debri', 'Daviesia', 'Eucalyptus', 'obs_fmc_mean']].resample("1M").mean().interpolate().plot(figsize=(15,5), 
cmap = cmap, ax=ax)
ax.legend(loc='center left', markerscale=6, bbox_to_anchor=(1, 0.4))

参数称为linestyle。但是,如果你试图像那样将列表传递给它,它无论如何都会给你一个错误。

我不知道有什么方法可以像你对颜色所做的那样,在一次绘图调用中传递多个线型,如果可能的话。但是,可以为多线打印的各种特性设置循环器。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
df = pd.DataFrame(np.arange(42).reshape((6, 7)))
# Might as well do both colours and line styles in one go
cycler = plt.cycler(linestyle=['-', '-', '--', '-', '--', '-', '--'],
color=['black', 'brown', 'brown', 'orange', 'orange', 'green', 'green'],
)
fig, ax = plt.subplots()
ax.set_prop_cycle(cycler)
df.plot(ax=ax)
plt.show()

我唯一能想到的另一种方法是保持一切如常,然后从AxesSubplot对象中提取单独的行,并手动设置它们的行样式。

new_df_brindabella.plot(...)
for line, ls in zip(ax.get_lines(), linestyles):
line.set_linestyle(ls)

最新更新