【问题标题】:Matplotlib vline label parameter not showingMatplotlib vline 标签参数未显示
【发布时间】:2015-01-25 02:44:12
【问题描述】:

我想用 matplotlib 的 .vline 命令标记我的垂直线,但由于某种原因,标签参数没有做任何事情/在最终绘图上显示任何内容。有谁知道如何让标签显示?

plt.vlines(x=pah, ymin=0, ymax=0.6, colors='0.75', linestyles='dashed', label='PAHs')

除了标签之外,一切都有效。

非常感谢,

L

【问题讨论】:

    标签: python matplotlib enthought


    【解决方案1】:

    对于您所在行附近的文本,如 example 使用:

    vline_value = 3
    
    fig, ax = plt.subplots(figsize=(10,10))
    ax.axvline(x=vline_value, ymin=0, ymax=1) 
    x_bounds = ax.get_xlim()
    ax.annotate(s='vline_value', xy =(((vline_value-x_bounds[0])/(x_bounds[1]-x_bounds[0])),1.01), xycoords='axes fraction', verticalalignment='right', horizontalalignment='right bottom' , rotation = 270)
    fig.savefig('example')
    

    此外,如果您愿意,这个简短的脚本还包含更多选项: https://pythonhosted.org/lineid_plot/#

    【讨论】:

      【解决方案2】:

      这行得通

      plt.plot(x,y)
      plt.vlines(x=pah, ymin=0, ymax=0.6, colors='0.75', linestyles='dashed', label='PAHs')
      plt.legend()
      

      但我不知道这是不是你所期望的

      【讨论】:

        【解决方案3】:

        标签关键字显示在图例中。您需要明确创建 legend 才能在图中看到标签:

        plt.vlines([1,2,3], 0, 1, label='test')
        plt.legend()
        

        【讨论】:

        • 谢谢你,现在合乎逻辑了......但是在连续盯着屏幕几个小时后,你陷入了疯狂。
        • 同样,我也陷入了疯狂。在 y 轴顶部(即ymax = plot.get_ylim()[1])而不是图例显示标签的最佳方式是什么? (vlines 的每个位置可能有不同的标签)?有关更多上下文,我对标签有以下问题:github.com/chapmanb/bcbio-nextgen/pull/1204
        猜你喜欢
        • 2015-08-05
        • 2022-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-11
        • 2021-05-14
        • 2021-10-24
        相关资源
        最近更新 更多