【问题标题】:Adding a legend to PyPlot in Matplotlib in the simplest manner possible以最简单的方式在 Matplotlib 中的 PyPlot 中添加图例
【发布时间】:2013-10-08 04:13:10
【问题描述】:

TL;DR ->如何在MatplotlibPyPlot 中为折线图创建图例而不创建任何额外的变量?

请考虑下面的绘图脚本:

if __name__ == '__main__':
    PyPlot.plot(total_lengths, sort_times_bubble, 'b-',
                total_lengths, sort_times_ins, 'r-',
                total_lengths, sort_times_merge_r, 'g+',
                total_lengths, sort_times_merge_i, 'p-', )
    PyPlot.title("Combined Statistics")
    PyPlot.xlabel("Length of list (number)")
    PyPlot.ylabel("Time taken (seconds)")
    PyPlot.show()

如您所见,这是matplotlibPyPlot 的一个非常基本的用法。理想情况下,这会生成如下图:

没什么特别的,我知道。然而,目前还不清楚哪些数据被绘制在哪里(我试图绘制一些排序算法的数据,长度与所用时间的关系,我想确保人们知道哪条线是哪条线)。因此,我需要一个图例,但请看下面的示例(from the official site):

ax = subplot(1,1,1)
p1, = ax.plot([1,2,3], label="line 1")
p2, = ax.plot([3,2,1], label="line 2")
p3, = ax.plot([2,3,1], label="line 3")

handles, labels = ax.get_legend_handles_labels()

# reverse the order
ax.legend(handles[::-1], labels[::-1])

# or sort them by labels
import operator
hl = sorted(zip(handles, labels),
            key=operator.itemgetter(1))
handles2, labels2 = zip(*hl)

ax.legend(handles2, labels2)

你会看到我需要创建一个额外的变量ax。如何在我的图表中添加图例而不必创建这个额外的变量并保持我当前脚本的简单性?

【问题讨论】:

  • 我对您对创建额外变量的担忧感到困惑。无论如何,你必须在幕后制作这些对象。
  • @tcaswell 好吧,让我试着安抚他们。我不想创建额外的变量,因为它增加了整个脚本的复杂性。我正在尝试向一群学生这个,因为他们以前没有使用过matplotlib,所以我想让事情尽可能简单。此外,如果您查看 Rob 的答案,它比网站上显示的示例要简单得多。我希望这会有所帮助。
  • 我认为,从长远来看,使用状态机接口会更难理解,因为其中大部分都是“魔术”完成的。此外,约定是使用import matplotlib.pyplot as plt 而不是PyPlot

标签: python matplotlib plot


【解决方案1】:

为绘图调用中的每个参数添加标签,对应于它正在绘制的系列,即label = "series 1"

然后只需将Pyplot.legend() 添加到脚本底部,图例就会显示这些标签。

【讨论】:

  • 这是正确的想法,但您从不添加标签,因此图例将为空
【解决方案2】:

在每个plot() 调用中添加label=,然后调用legend(loc='upper left')

考虑这个示例(使用 Python 3.8.0 测试):

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x, y1, "-b", label="sine")
plt.plot(x, y2, "-r", label="cosine")
plt.legend(loc="upper left")
plt.ylim(-1.5, 2.0)
plt.show()

本教程稍作修改:http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html

【讨论】:

  • 如果您在绘制系列时不知道标签,有没有办法做到这一点? IE。一种在已经绘制后向系列添加标签的方法?或者在显示图例之前修改占位符标签的方法?
  • plt.legend(loc='upper left') 也有效,其中plt 来自import matplotlib.pyplot as plt
  • 请注意:plt.legend() 调用 需要 plt.plot(label="lab1")
  • @davidA 是的,您可以简单地将字符串列表传递给plt.legendplt.legend(['First Label', 'Second Label'])
  • 我确信matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.legend.html 上的实际文档也回答了这个问题,但与这里相比,从那里获取这些信息真是太痛苦了。我们需要重新构想文档。大声笑
【解决方案3】:

这里有一个例子可以帮助你...

fig = plt.figure(figsize=(10,5))
ax = fig.add_subplot(111)
ax.set_title('ADR vs Rating (CS:GO)')
ax.scatter(x=data[:,0],y=data[:,1],label='Data')
plt.plot(data[:,0], m*data[:,0] + b,color='red',label='Our Fitting 
Line')
ax.set_xlabel('ADR')
ax.set_ylabel('Rating')
ax.legend(loc='best')
plt.show()

【讨论】:

  • 我很好奇,为什么你的拟合线与数据相差这么远?
【解决方案4】:

您可以使用 plt.gca() 访问 Axes 实例 (ax)。在这种情况下,您可以使用

plt.gca().legend()

您可以通过在每个plt.plot() 调用中使用label= 关键字或将标签分配为legend 中的元组或列表来执行此操作,如以下工作示例所示:

import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-0.75,1,100)
y0 = np.exp(2 + 3*x - 7*x**3)
y1 = 7-4*np.sin(4*x)
plt.plot(x,y0,x,y1)
plt.gca().legend(('y0','y1'))
plt.show()

但是,如果您需要多次访问 Axes 实例,我建议您将其保存到变量 ax

ax = plt.gca()

然后调用ax 而不是plt.gca()

【讨论】:

  • 复制粘贴不需要任何阅读的答案,并带有图片!这个答案值得更多的信任
  • 嗨,Cameron,我试图联系您,这是我找到的最佳选择...您是我经常使用的一个非常有用的工具的作者:web.media.mit.edu/~crtaylor/calculator.html 不幸的是,它不适用于现在有一段时间了。是我还是工具,都坏了。如果是这样,你能解决它吗?
  • 哎呀!固定的。感谢你的信息!希望你学到了一些关于 Axes 实例的有用知识:)
【解决方案5】:

带有图例的正弦和余弦曲线的简单图。

使用matplotlib.pyplot

import math
import matplotlib.pyplot as plt
x=[]
for i in range(-314,314):
    x.append(i/100)
ysin=[math.sin(i) for i in x]
ycos=[math.cos(i) for i in x]
plt.plot(x,ysin,label='sin(x)')  #specify label for the corresponding curve
plt.plot(x,ycos,label='cos(x)')
plt.xticks([-3.14,-1.57,0,1.57,3.14],['-$\pi$','-$\pi$/2',0,'$\pi$/2','$\pi$'])
plt.legend()
plt.show()

【讨论】:

    【解决方案6】:

    您可以添加自定义图例documentation

    first = [1, 2, 4, 5, 4]
    second = [3, 4, 2, 2, 3]
    plt.plot(first, 'g--', second, 'r--')
    plt.legend(['First List', 'Second List'], loc='upper left')
    plt.show()
    

    【讨论】:

      【解决方案7】:
          # Dependencies
          import numpy as np
          import matplotlib.pyplot as plt
      
          #Set Axes
          # Set x axis to numerical value for month
          x_axis_data = np.arange(1,13,1)
          x_axis_data
      
          # Average weather temp
          points = [39, 42, 51, 62, 72, 82, 86, 84, 77, 65, 55, 44]
      
          # Plot the line
          plt.plot(x_axis_data, points)
          plt.show()
      
          # Convert to Celsius C = (F-32) * 0.56
          points_C = [round((x-32) * 0.56,2) for x in points]
          points_C
      
          # Plot using Celsius
          plt.plot(x_axis_data, points_C)
          plt.show()
      
          # Plot both on the same chart
          plt.plot(x_axis_data, points)
          plt.plot(x_axis_data, points_C)
      
          #Line colors
          plt.plot(x_axis_data, points, "-b", label="F")
          plt.plot(x_axis_data, points_C, "-r", label="C")
      
          #locate legend
          plt.legend(loc="upper left")
          plt.show()
      
      

      【讨论】:

        猜你喜欢
        • 2021-02-03
        • 1970-01-01
        • 2018-12-21
        • 2016-01-26
        • 2021-08-04
        • 2017-12-12
        • 2020-07-28
        • 2013-12-24
        相关资源
        最近更新 更多