Cov*_*ich 11 python colors matplotlib legend markers
在Matplotlib中,我试图用这样的彩色"标记"制作一个传奇:
这个是使用该scatter功能制作的,但不适合我的情节.我想"从头开始"制作一个没有相关数据的传奇.颜色很重要,因此应该是每个标记的属性.
我试过了
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)
Run Code Online (Sandbox Code Playgroud)
但是:
1)该MarkerStyle课程不支持颜色信息
2)我收到警告:
UserWarning: Legend does not support <matplotlib.markers.MarkerStyle object at 0x7fca640c44d0> instances.
A proxy artist may be used instead.
Run Code Online (Sandbox Code Playgroud)
但是如何根据标记定义代理艺术家呢?
谢谢你的帮助 !
tmd*_*son 18
按照图例指南中给出的示例,您可以使用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()
Run Code Online (Sandbox Code Playgroud)
您可以对 进行子类化,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()
Run Code Online (Sandbox Code Playgroud)