【问题标题】:matplotlib plot Label along plot linematplotlib plot 沿绘图线的标签
【发布时间】:2023-03-23 19:30:01
【问题描述】:

matplotlib 社区的一个:

假设我有一条直线:

plot([37, 45], [-0.67778, -0.67778], '--k', lw=1.2)

我是否可以沿线而不是图例向这条线添加标签?即类似于以下内容(但不是等高线图,只是普通的线图):

【问题讨论】:

    标签: python matplotlib plot


    【解决方案1】:

    下面是一个简单的例子来展示如何在不考虑外观的情况下完成它。有关注释图的更多信息,请参阅detailed demonstration

    import matplotlib.pyplot as plt
    x = [37, 45]; y = [-0.67778, -0.67778]
    
    # as an example for where to place the text we can use the mean
    xmean = sum(i for i in x) / float(len(x))
    ymean = sum(i for i in y) / float(len(y))
    
    plt.plot(x, y, '--k', lw=1.2)
    plt.annotate('some text', xy=(xmean,ymean), xycoords='data')
    plt.show() # or plt.savefig('filename.png')
    

    产量:

    【讨论】:

      猜你喜欢
      • 2013-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2017-09-11
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多