问题描述
我有一个(图集,步骤)matplotlib直方图,带有图例。但是,我对这个传说并不完全满意。我想在其中画线,而不是像我在左侧画图(带着我所有的绘画热情)画这些矩形一样
我不知道它是否有帮助,但这是绘制此图的代码:
hlines = [0.2,0.4,0.6,0.8,1]
for hline in hlines:
plt.axhline(y=hline,color='lightgrey',linewidth=0.5,zorder=0.5)
plt.hist(freq_days_bw_hist1,bins=5400,density=True,cumulative=True,color='navy',label='c1',histtype='step',linewidth=2)
plt.hist(freq_days_bw_hist2,color='red',label='c2',linewidth=2)
plt.rc('legend',fontsize=16)
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
#cumulative=True,#plt.plot(po,est_exp)
axes = plt.gca()
axes.set_xlim([0,365])
axes.set_ylim([0,1.1])
axes.set_xlabel('days',size=20)
axes.set_ylabel('cdfs',size=20)
plt.legend(loc='upper right')
plt.show()
谢谢!
解决方法
这应该可以解决您的问题:
from matplotlib.lines import Line2D
custom_lines = [Line2D([0],[0],color=cmap(0.),lw=4),Line2D([0],color=cmap(.5),color=cmap(1.),lw=4)]
fig,ax = plt.subplots()
lines = ax.plot(data)
ax.legend(custom_lines,['Cold','Medium','Hot'])
您可以在此网站上找到一些有用的提示:https://matplotlib.org/3.1.1/gallery/text_labels_and_annotations/custom_legends.html
您的示例应如下所示:
# rest of you code here
custom_lines = [Line2D([0],color='navy',linewidth=4),color='red',linewidth=4)]
axes.legend(custom_lines,['c1','c2'],loc='upper right')
plt.show()