我无法使matplotlibv.2.0.2图形的图例出现在python2.7中。下面是一个在for循环中在轴上绘制箭头的最小示例。在
由于每次迭代都指定label关键字定义并不能使图例出现,所以我尝试在第一次迭代期间使用条件来设置标签。在
尝试中的“bbox_to_anchor”和“loc”参数斧头图例()没有效果,所以我省略了任何传说的争论。在
有谁知道如何让一个图例出现,在标签旁边分别显示带有红色和黑色箭头标记的“箭头1”和“箭头2”标签吗?在
最小示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots( )
ax.set_xlim( 0, 11 )
ax.set_ylim( 0, 2 )
kwargs1 ={'color':'r'}
kwargs2 ={'color':'black'}
for i in range(1, 11):
ax.arrow( i-0.2, 0, 0, i/10., width=0.1,
label='Arrow1' if i == 1 else None, **kwargs1 )
ax.arrow( i+0.2, 0, 0, i/10., width=0.1,
label='Arrow2' if i == 1 else None, **kwargs2 )
ax.legend( )
见无图例输出图图片链接:
编辑:
似乎每次迭代都设置标签定义是可以的。下面是一个在图例中显示具有正确颜色和标签的线标记的解决方案:
^{pr2}$带图例的打印输出图片:
另一个编辑:
由于一些我不想深入了解的原因,我实际脚本的复杂性(不是最小的例子)需要一个无形的“代理艺术家”来为图例添加标签。这是在这些文档中为不受支持的艺术家建议的here
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
fig, ax = plt.subplots( )
ax.set_xlim( 0, 11 )
ax.set_ylim( 0, 2 )
kwargs1 ={'color':'red' }
kwargs2 ={'color':'black'}
for i in range(1, 11):
ax.arrow( i-0.2, 0, 0, i/10., width=0.1, **kwargs1 )
Arrow1 = Rectangle((0, 0), 1, 1, label='Arrow1', fc='red')
ax.arrow( i+0.2, 0, 0, i/10., width=0.1, **kwargs2 )
Arrow2 = Rectangle((0, 0), 1, 1, label='Arrow2', fc='black')
ax.legend( handles=[Arrow1, Arrow2], loc='upper left' )
我认为您需要指定legend()命令的“labels”和“handles”参数来显示它
https://matplotlib.org/users/legend_guide.html
相关问题 更多 >
编程相关推荐