python - 在 python 中的图例中添加一个 fill_between

标签 python matplotlib legend

我正在尝试将我的 fill_between 添加到图例中,但它不起作用? 为什么它与其他地 block 不同?这是我现在的图表:

enter image description here

plt.scatter(stageheight,discharge,color='b',label='measured data')
plt.plot(stageheight_hecras,discharge_hecras,'y^',label='modeled with HEC-RAS')
plt.plot(stageheight_masked,discharge_predicted,'r-',label='regression line measured data')
plt.plot(stageheight_hecras,discharge_predicted_hecras,'g-',label='regression line HEC-RAS')
plt.plot(stageheight_masked,upper,'r--',label='15% error measured data')
plt.plot(stageheight_masked,lower,'r--')
plt.plot(stageheight_hecras,upper_hecras,'g--',label='30% error HEC-RAS')
plt.plot(stageheight_hecras,lower_hecras,'g--')
plt.fill_between(stageheight_masked,upper,lower,facecolor='red',edgecolor='red',alpha=0.5,label='test')
plt.fill_between(stageheight_hecras,upper_hecras,lower_hecras,facecolor='green',alpha=0.5)
plt.axhline(y=0.6,xmin=0,xmax=1,color='black',linewidth = 4.0,label='measuring  range')
plt.text(0.02,0.7,'measured rating curve $Q = 1.37H^2 + 0.34H - 0.007$\nmodeled ratign curve $Q = 1.14H^2 - 0.51H + 0.07$',bbox=dict(facecolor='none',edgecolor='black',boxstyle='square'))
plt.title('Rating curve Catsop')
plt.ylabel('discharge$[m^3/s]$')
plt.ylim(0,2.5)
plt.xlim(0,1.2)
plt.xlabel('stageheight$[m]$')
plt.legend(loc='upper left', title='Legend')
plt.grid(True)
plt.show()

最佳答案

您使用的是什么版本的 matplotlib

如果您升级到 v1.5.0 或更高版本,这应该可以工作。有关更多详细信息,请参见此处:https://github.com/matplotlib/matplotlib/pull/3303#event-182205203

关于python - 在 python 中的图例中添加一个 fill_between,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35653251/

相关文章:

python - Google Speech API 不适用于嘈杂的音频

python - 由于对数范围 >10 个十进制,matplotlib 在 y 轴上缺少小刻度

python - '重新排序'/适应 matshow 矩阵图的刻度

r - R情节中的图例

r - 为什么我在 chartJSRadar() 之后调用 legend() 得到 "plot.new has not been called yet"

python - 如何获得 pandas 组中最受欢迎的项目?

python - 从Python文件中读取数据库

plot - 如何在 SGPLOT 图例中组合散点和系列线?

python - 有什么方法可以使用 xlwings 创建新工作表吗?

python - 给定一个邻接矩阵,如何用matplotlib画图?