下面是我的代码片段:
fig2 = plt.figure(figsize=(8,6))
ax1 = fig2.add_subplot(111)
ax1.scatter((logngal),(logm200),c='r',label='$0.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],['$1\sigma\/confidence\/limts$'])
fig2.show()
当我执行上述操作时,我只看到图例中提到的$1\sigma\/confidence\/limts$
。
而正如你们所见,我也分别在ax1.scatter
和{label='$0.0<z<1.0$'
和{
这不会在图例中绘制。在
如何在图例中添加上述三个标签?
您需要在绘图时抓取
scatter
和plot
艺术家,然后将它们的句柄和标签提供给legend
。例如,下面是修改后的代码(在开始时有一些示例数据,只是为了让它运行):plt.plot
返回一个Line2D
对象的列表,因此,如果将其读作pplot, = plt.plot(...)
,则将该单项列表解压缩。在然后可以使用
.get_label()
onpplot
和pscat
为图例添加标签。在相关问题 更多 >
编程相关推荐