有没有办法在 Matplotlib 中向图例添加一个空条目?
Is there a way to add an empty entry to a Legend in Matplotlib?
我想在 python 的 matplotib 图中的图例中添加空白 space。我在图例中有奇数个条目,目前看起来像:
_________________________________________________________________
| handle1 - label1 handle3 - label3 handle5 - label5 |
| handle2 - label2 handle4 - label4 |
不过,数据在逻辑上是成对分组的,有一个控件,所以如果看起来像这样就更好了:
_________________________________________________________________
| handle1 - label1 handle2 - label2 handle4 - label4 |
| handle3 - label3 handle5 - label5 |
在对数据集进行 运行 循环时自动生成图例:
for [folder1,folder2, label] in folder_list:
parse_folder([folder1,folder2])
color = next(colorgen)
marker = next(markergen)
ax1.errorbar(percent[0],percent[1], yerr=per_std, c=color, fmt=marker, label=label)
if label == 'Flat Plate':
print 'tripped'
ax1.plot(np.NaN, np.NaN, '-', color='none', label=' ')
然后在最后调用
leg = ax1.legend(loc='lower left',fancybox=True,prop={'size':fontsize-2},ncol=4,bbox_to_anchor=(-0.1, -0.315))
有没有办法将这个空白点插入到图例中?
你可以定义一条假白线
l = Line2D([0],[0],color="w")
然后绘制数据并将line/markers保存在变量
中
f = figure()
ax = f.add_subplot(111)
l1, = ax.plot(1*arange(10))
l2, = ax.plot(2*arange(10))
l3, = ax.plot(3*arange(10))
l4, = ax.plot(4*arange(10))
l5, = ax.plot(5*arange(10))
最后你调用legend
如下
ax.legend((l1,l,l2,l3,l4,l5),("label1","","label2","label3","label4","label5"),
loc='upper center',fancybox=True,ncol=3)
其中每个 line/marker 都与不同的标签相关联。在您想要在图例中留下空白的地方插入假白线 l
并为其关联一个空白字符串。
希望对您有所帮助。
Farenorth 的解决方案(在 Op 的评论中给出)给出了最清晰的答案。
在需要空白处的位置添加一条命令,数据使用np.NaN
,颜色设置为none
。
ax1.plot(np.NaN, np.NaN, '-', color='none', label='')
请注意,这必须是同一类型,因为用于自动生成图例的方法会依次获得不同的绘图类型,即所有绘图,然后是所有误差线等,因此空白条目需要相同键入以保持函数调用的顺序。
我想在 python 的 matplotib 图中的图例中添加空白 space。我在图例中有奇数个条目,目前看起来像:
_________________________________________________________________
| handle1 - label1 handle3 - label3 handle5 - label5 |
| handle2 - label2 handle4 - label4 |
不过,数据在逻辑上是成对分组的,有一个控件,所以如果看起来像这样就更好了:
_________________________________________________________________
| handle1 - label1 handle2 - label2 handle4 - label4 |
| handle3 - label3 handle5 - label5 |
在对数据集进行 运行 循环时自动生成图例:
for [folder1,folder2, label] in folder_list:
parse_folder([folder1,folder2])
color = next(colorgen)
marker = next(markergen)
ax1.errorbar(percent[0],percent[1], yerr=per_std, c=color, fmt=marker, label=label)
if label == 'Flat Plate':
print 'tripped'
ax1.plot(np.NaN, np.NaN, '-', color='none', label=' ')
然后在最后调用
leg = ax1.legend(loc='lower left',fancybox=True,prop={'size':fontsize-2},ncol=4,bbox_to_anchor=(-0.1, -0.315))
有没有办法将这个空白点插入到图例中?
你可以定义一条假白线
l = Line2D([0],[0],color="w")
然后绘制数据并将line/markers保存在变量
中f = figure()
ax = f.add_subplot(111)
l1, = ax.plot(1*arange(10))
l2, = ax.plot(2*arange(10))
l3, = ax.plot(3*arange(10))
l4, = ax.plot(4*arange(10))
l5, = ax.plot(5*arange(10))
最后你调用legend
如下
ax.legend((l1,l,l2,l3,l4,l5),("label1","","label2","label3","label4","label5"),
loc='upper center',fancybox=True,ncol=3)
其中每个 line/marker 都与不同的标签相关联。在您想要在图例中留下空白的地方插入假白线 l
并为其关联一个空白字符串。
希望对您有所帮助。
Farenorth 的解决方案(在 Op 的评论中给出)给出了最清晰的答案。
在需要空白处的位置添加一条命令,数据使用np.NaN
,颜色设置为none
。
ax1.plot(np.NaN, np.NaN, '-', color='none', label='')
请注意,这必须是同一类型,因为用于自动生成图例的方法会依次获得不同的绘图类型,即所有绘图,然后是所有误差线等,因此空白条目需要相同键入以保持函数调用的顺序。