【问题标题】:add text to a legend in python在python中将文本添加到图例
【发布时间】:2016-02-26 11:06:07
【问题描述】:

我正在尝试向图例添加一些文本。这是关于框中的文本。另一种解决方案是文本框保持在图例下方,并且在放大图形时不会移动。

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 = 2.71H^2 - 2.20H + 0.98$',bbox=dict(facecolor='none',edgecolor='black',boxstyle='square'))
plt.title('Rating curve Catsop')
plt.ylabel('discharge')
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()

这是我现在的图表:

【问题讨论】:

标签: python matplotlib legend


【解决方案1】:

尝试在 plt.text 中使用变换。现在前两个坐标是相对于轴的。

ax = plt.gca()
ax.text(0.3,0.05,'measured rating curve $Q = 1.37H^2 + 0.34H - 0.007$\nmodeled ratign curve $Q = 2.71H^2 - 2.20H + 0.98$',transform=ax.transAxes, bbox=dict(facecolor='none',edgecolor='black',boxstyle='square')) 

【讨论】:

    【解决方案2】:

    您可以使用Patch,而不是绘制一个假矩形,它不会显示在图形或轴上:

    import matplotlib.patches as mpatches
    
    extraString = 'measured rating curve $Q = 1.37H^2 + 0.34H - 0.007$\nmodeled rating curve $Q = 2.71H^2 - 2.20H + 0.98$'
    handles, labels = plt.get_legend_handles_labels()
    handles.append(mpatches.Patch(color='none', label=extraString))
    plt.legend(handles=handles)
    

    这种方法有一个额外的效果,你首先得到图例中已经存在的任何东西,所以你不必手动明确地构建它。

    【讨论】:

      【解决方案3】:

      这会将文本添加到图例中(受answer 启发):

      from matplotlib.patches import Rectangle
      
      plt.plot(range(10))
      
      p = plt.axhline(y=0.6,xmin=0,xmax=1,color='black',linewidth = 4.0,label='measuring range')
      plt.ylabel('discharge')
      plt.ylim(0,2.5)
      plt.xlim(0,1.2)
      plt.xlabel('stageheight[m]')
      text1 = 'measured rating curve $Q = 1.37H^2 + 0.34H - 0.007$'
      text2 = 'modeled ratign curve $Q = 2.71H^2 - 2.20H + 0.98$'
      extra = Rectangle((0, 0), 1, 1, fc="w", fill=False, edgecolor='none', linewidth=0)
      plt.legend([p, extra, extra],[p.get_label(), text1, text2], loc='upper left', title='Legend')
      plt.grid(True)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-22
        相关资源
        最近更新 更多