Matplotlib:从头开始制作彩色标记图例
Matplotlib : making a colored markers legend from scratch
在 Matplotlib 中,我正在尝试用彩色 "markers" 制作一个像这样的图例:
这个是用scatter
函数制作的,但不适合我的情节。我想生成一个没有关联数据的图例 "from scratch"。
颜色很重要,因此应该是每个标记的属性。
我试过了
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
class 不支持颜色信息
2) 我收到警告:
UserWarning: Legend does not support <matplotlib.markers.MarkerStyle object at 0x7fca640c44d0> instances.
A proxy artist may be used instead.
但是如何根据标记定义代理艺术家?
感谢您的帮助!
遵循 legend guide, you can use a Line2D
对象而不是 marker
对象中给出的示例。
与指南中给出的示例的唯一区别是您要设置 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()
您可以将 HandlerBase
子类化以从 (color, marker)
的元组创建处理程序。
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase
list_color = ["c", "gold", "crimson"]
list_mak = ["d","s","o"]
list_lab = ['Marker 1','Marker 2','Marker 3']
ax = plt.gca()
class MarkerHandler(HandlerBase):
def create_artists(self, legend, tup,xdescent, ydescent,
width, height, fontsize,trans):
return [plt.Line2D([width/2], [height/2.],ls="",
marker=tup[1],color=tup[0], transform=trans)]
ax.legend(list(zip(list_color,list_mak)), list_lab,
handler_map={tuple:MarkerHandler()})
plt.show()
在 Matplotlib 中,我正在尝试用彩色 "markers" 制作一个像这样的图例:
这个是用scatter
函数制作的,但不适合我的情节。我想生成一个没有关联数据的图例 "from scratch"。
颜色很重要,因此应该是每个标记的属性。
我试过了
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
class 不支持颜色信息
2) 我收到警告:
UserWarning: Legend does not support <matplotlib.markers.MarkerStyle object at 0x7fca640c44d0> instances.
A proxy artist may be used instead.
但是如何根据标记定义代理艺术家?
感谢您的帮助!
遵循 legend guide, you can use a Line2D
对象而不是 marker
对象中给出的示例。
与指南中给出的示例的唯一区别是您要设置 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()
您可以将 HandlerBase
子类化以从 (color, marker)
的元组创建处理程序。
import matplotlib.pyplot as plt
from matplotlib.legend_handler import HandlerBase
list_color = ["c", "gold", "crimson"]
list_mak = ["d","s","o"]
list_lab = ['Marker 1','Marker 2','Marker 3']
ax = plt.gca()
class MarkerHandler(HandlerBase):
def create_artists(self, legend, tup,xdescent, ydescent,
width, height, fontsize,trans):
return [plt.Line2D([width/2], [height/2.],ls="",
marker=tup[1],color=tup[0], transform=trans)]
ax.legend(list(zip(list_color,list_mak)), list_lab,
handler_map={tuple:MarkerHandler()})
plt.show()