在图例中使用代理艺术家,matplotlib,Python
Using a proxy artist inside a legend, matplotlib, Python
这是我的代码片段:
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
ax1.scatter((logngal),(logm200),c='r',label='[=11=].0<z<1.0$')
ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper, color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
ax1.legend([p1],['\sigma\/confidence\/limts$'])
fig2.show()
当我执行上述操作时,我只看到图例中提到的\sigma\/confidence\/limts$
。
然而正如您所见,我还在 ax1.scatter
和 ax1.plot
中分别调用了 label='[=13=].0<z<1.0$'
和 label='$slope=%s \pm %s$'%(slope1,slope1_err)
。
这并没有在图例中绘制出来。
如何在图例中添加以上三个标签?
您需要在绘制时抓住 scatter
和 plot
艺术家,然后将它们的句柄和标签提供给 legend
。例如,这里是您修改的代码(开头有一些示例数据只是为了让它达到 运行):
plt.plot
returns 一个 Line2D
对象的列表,因此如果您将其读作 pplot, = plt.plot(...)
,您将解压该单项列表。
然后您可以在 pplot
和 pscat
上使用 .get_label()
为图例添加标签。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
# Some things to make your script run when I don't have your data
slope1,slope1_err='this','that'
logngal = np.linspace(0,1,20)
logm200 = np.random.rand(20)
x_pred = np.linspace(0,1,20)
curve_y_1 = 0.5*(np.sin(logngal)/2.+np.cos(logngal))
upper = np.sin(x_pred)/2.
lower = np.cos(x_pred)
# end of sample data
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
pscat = ax1.scatter((logngal),(logm200), c='r',label='[=10=].0<z<1.0$')
pplot, = ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper,color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
handles = [p1,pplot,pscat]
labels = ['\sigma\/confidence\/limts$',pplot.get_label(),pscat.get_label()]
ax1.legend(handles,labels)
fig2.show()
这是我的代码片段:
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
ax1.scatter((logngal),(logm200),c='r',label='[=11=].0<z<1.0$')
ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper, color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
ax1.legend([p1],['\sigma\/confidence\/limts$'])
fig2.show()
当我执行上述操作时,我只看到图例中提到的\sigma\/confidence\/limts$
。
然而正如您所见,我还在 ax1.scatter
和 ax1.plot
中分别调用了 label='[=13=].0<z<1.0$'
和 label='$slope=%s \pm %s$'%(slope1,slope1_err)
。
这并没有在图例中绘制出来。
如何在图例中添加以上三个标签?
您需要在绘制时抓住 scatter
和 plot
艺术家,然后将它们的句柄和标签提供给 legend
。例如,这里是您修改的代码(开头有一些示例数据只是为了让它达到 运行):
plt.plot
returns 一个 Line2D
对象的列表,因此如果您将其读作 pplot, = plt.plot(...)
,您将解压该单项列表。
然后您可以在 pplot
和 pscat
上使用 .get_label()
为图例添加标签。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
# Some things to make your script run when I don't have your data
slope1,slope1_err='this','that'
logngal = np.linspace(0,1,20)
logm200 = np.random.rand(20)
x_pred = np.linspace(0,1,20)
curve_y_1 = 0.5*(np.sin(logngal)/2.+np.cos(logngal))
upper = np.sin(x_pred)/2.
lower = np.cos(x_pred)
# end of sample data
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
pscat = ax1.scatter((logngal),(logm200), c='r',label='[=10=].0<z<1.0$')
pplot, = ax1.plot((logngal),(curve_y_1),'y',linewidth=2,label='$slope=%s \pm %s$'%(slope1,slope1_err))
ax1.fill_between(x_pred, lower, upper,color='#888888', alpha=0.5)
p1 = mpatches.Rectangle((0, 0), 1, 1, fc="#888888",alpha=0.5)
handles = [p1,pplot,pscat]
labels = ['\sigma\/confidence\/limts$',pplot.get_label(),pscat.get_label()]
ax1.legend(handles,labels)
fig2.show()