Ell*_*iot 8 python graph matplotlib legend
我想在 python 中的 matplotib 图中为图例添加一个空格。我在图例中有奇数个条目,目前看起来像:
_________________________________________________________________
| handle1 - label1 handle3 - label3 handle5 - label5 |
| handle2 - label2 handle4 - label4 |
Run Code Online (Sandbox Code Playgroud)
但是,数据在逻辑上与控件成对分组,因此最好是这样的:
_________________________________________________________________
| handle1 - label1 handle2 - label2 handle4 - label4 |
| handle3 - label3 handle5 - label5 |
Run Code Online (Sandbox Code Playgroud)
在对数据集自动运行 for 循环期间生成图例:
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=' ')
Run Code Online (Sandbox Code Playgroud)
然后最后打电话
leg = ax1.legend(loc='lower left',fancybox=True,prop={'size':fontsize-2},ncol=4,bbox_to_anchor=(-0.1, -0.315))
Run Code Online (Sandbox Code Playgroud)
有没有办法将这个空白点插入到图例中?
Ell*_*iot 15
Farenorth 的解决方案(在 Op 的评论中给出)给出了最干净的答案。
在需要空白点的位置添加一个附加命令,用于np.NaN数据并将颜色设置为none。
ax1.plot(np.NaN, np.NaN, '-', color='none', label='')
Run Code Online (Sandbox Code Playgroud)
请注意,这必须是相同的类型,因为用于自动生成图例的方法会顺序获取不同的绘图类型,即所有绘图,然后是所有错误栏等,因此空白条目需要是相同的类型才能保持顺序的函数调用。
您可以定义一条假白线
l = Line2D([0],[0],color="w")
Run Code Online (Sandbox Code Playgroud)
然后绘制您的数据并将线条/标记保存在变量中
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))
Run Code Online (Sandbox Code Playgroud)
最后,你调用legend如下
ax.legend((l1,l,l2,l3,l4,l5),("label1","","label2","label3","label4","label5"),
loc='upper center',fancybox=True,ncol=3)
Run Code Online (Sandbox Code Playgroud)
其中每条线/标记与不同的标签相关联。在您想要在图例中留出空隙的地方插入假白线l并为其关联一个空白字符串。
希望能帮助到你。
| 归档时间: |
|
| 查看次数: |
5914 次 |
| 最近记录: |