Matplotlib:从头开始制作彩色标记图例 [英] Matplotlib : making a colored markers legend from scratch
问题描述
在Matplotlib中,我试图用这样的彩色标记"制作一个图例:
In Matplotlib, i'm trying to make a legend with colored "markers" like this one :
这是使用scatter
函数完成的,但不适用于我的绘图.我想从头开始"生成一个图例,而没有关联的数据.
颜色很重要,因此应该成为每个标记的属性.
this one has been made using the scatter
function, but is not adapted to my plot. I'd like to produce a legend "from scratch", without associated data.
The color is important, and therefore should be an attribute of each marker.
我尝试过
import matplotlib.markers as mmark
list_mak = [mmark.MarkerStyle('.'),mmark.MarkerStyle(','),mmark.MarkerStyle('o')]
list_lab = ['Marker 1','Marker 2','Marker 3']
plt.legend(list_mak,list_lab)
但是:
1)MarkerStyle
类不支持颜色信息
1) The MarkerStyle
class doesn't support color information
2)我得到警告:
UserWarning: Legend does not support <matplotlib.markers.MarkerStyle object at 0x7fca640c44d0> instances.
A proxy artist may be used instead.
但是我如何基于标记定义代理艺术家呢?
But how can I define a proxy artist based on a marker ?
感谢您的帮助!
推荐答案
按照 Line2D
对象而不是marker
对象.
与指南中给出的示例的唯一区别是您要设置linestyle='None'
The only difference to the example given in the guide is you want to set linestyle='None'
import matplotlib.lines as mlines
import matplotlib.pyplot as plt
blue_star = mlines.Line2D([], [], color='blue', marker='*', linestyle='None',
markersize=10, label='Blue stars')
red_square = mlines.Line2D([], [], color='red', marker='s', linestyle='None',
markersize=10, label='Red squares')
purple_triangle = mlines.Line2D([], [], color='purple', marker='^', linestyle='None',
markersize=10, label='Purple triangles')
plt.legend(handles=[blue_star, red_square, purple_triangle])
plt.show()
这篇关于Matplotlib:从头开始制作彩色标记图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!