我好像哪儿也找不到答案!我找到了一个讨论here,但是尝试这个我得到了一个TypeError: 'NoneType' object is not iterable
:
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> x, y = np.meshgrid(np.arange(10),np.arange(10))
>>> z = x + y
>>> cs = plt.contourf(x,y,z,levels=[2,3])
>>> cs.collections[0].set_label('test')
>>> plt.legend()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2791, in legend
ret = gca().legend(*args, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 4475, in legend
self.legend_ = mlegend.Legend(self, handles, labels, **kwargs)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend.py", line 365, in __init__
self._init_legend_box(handles, labels)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend.py", line 627, in _init_legend_box
handlebox)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 110, in __call__
handlebox.get_transform())
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 352, in create_artists
width, height, fontsize)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/legend_handler.py", line 307, in get_sizes
size_max = max(orig_handle.get_sizes())*legend.markerscale**2
TypeError: 'NoneType' object is not iterable
编辑:我正在查找以下内容:
您可以创建代理艺术家来制作图例:
也可以直接使用轮廓线,而不使用代理艺术家。
将产生:
但是,您可能还希望根据自己的需要查找注释。在我看来,它可以让您更精细地控制在图像上的位置和内容,下面是带有一些注释的相同示例:
我有一个类似的question,但需要超越HYRY's answer。为了方便用户使用,我希望
ax.legend()
在不需要用户传递任何句柄的情况下工作,这可以通过将标签传递给代理来实现然后将代理添加到axis的修补程序中:
(执行
ax = plt.gca()
以获取当前轴)这在this answer中有更详细的描述。
相关问题 更多 >
编程相关推荐