我使用以下代码来创建一个自定义matplotlib图例。
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
colors = ["g", "w"]
texts = ["Green Data Description", "RedData Description"]
patches = [ mpatches.Patch(color=colors[i], label="{:s}".format(texts[i]) ) for i in range(len(texts)) ]
plt.legend(handles=patches, bbox_to_anchor=(0.5, 0.5), loc='center', ncol=2 )生成的图例如下:

1-不显示图例中的白色符号,因为默认图例背景也是白色的。如何将图例背景设置为其他颜色?
2-如何将图例中的矩形符号更改为圆形?
发布于 2017-05-22 20:41:22
plt.legend()的facecolor参数设置图例的背景色。plt.legend(facecolor="plum")
plt.plot([],[],marker="o",ms=10,ls="")
完整示例:
import matplotlib.patches as mpatches
import matplotlib.pyplot as plt
colors = ["g", "w"]
texts = ["Green Data Description", "RedData Description"]
patches = [ plt.plot([],[], marker="o", ms=10, ls="", mec=None, color=colors[i],
label="{:s}".format(texts[i]) )[0] for i in range(len(texts)) ]
plt.legend(handles=patches, bbox_to_anchor=(0.5, 0.5),
loc='center', ncol=2, facecolor="plum", numpoints=1 )
plt.show()(请注意,只有旧版本的matplotlib才需要mec和numpoints参数)

对于图例中更复杂的形状,您可以使用自定义处理程序映射,请参阅legend guide或例如this answer作为示例
发布于 2017-05-21 23:57:55
试试这个:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerPatch
colors = ["g", "w"]
texts = ["Green Data Description", "RedData Description"]
class HandlerEllipse(HandlerPatch):
def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize, trans):
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
p = mpatches.Ellipse(xy=center, width=width + xdescent,
height=height + ydescent)
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]
c = [ mpatches.Circle((0.5, 0.5), 1, facecolor=colors[i], linewidth=3) for i in range(len(texts))]
plt.legend(c,texts,bbox_to_anchor=(0.5, 0.5), loc='center', ncol=2, handler_map={mpatches.Circle: HandlerEllipse()}).get_frame().set_facecolor('#00FFCC')
plt.show()输出:

更新:
要循环,请在mpatches.Ellipse中将width设置为height
去掉外面的黑线,在mpatches.Circle中设置edgecolor="none"
代码:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.patches as mpatches
from matplotlib.legend_handler import HandlerPatch
colors = ["g", "w"]
texts = ["Green Data Description", "RedData Description"]
class HandlerEllipse(HandlerPatch):
def create_artists(self, legend, orig_handle,
xdescent, ydescent, width, height, fontsize, trans):
center = 0.5 * width - 0.5 * xdescent, 0.5 * height - 0.5 * ydescent
p = mpatches.Ellipse(xy=center, width=height + xdescent,
height=height + ydescent)
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]
c = [ mpatches.Circle((0.5, 0.5), radius = 0.25, facecolor=colors[i], edgecolor="none" ) for i in range(len(texts))]
plt.legend(c,texts,bbox_to_anchor=(0.5, 0.5), loc='center', ncol=2, handler_map={mpatches.Circle: HandlerEllipse()}).get_frame().set_facecolor('#00FFCC')
plt.show()新图片:

发布于 2021-07-24 01:01:55
由于其他答案对我不起作用,我添加了一个超级简单和直接的答案:
import matplotlib.pyplot as plt
handles = []
for x in colors:
handles.append(plt.Line2D([], [], color=x, marker="o", linewidth=0))你可以调整标记的大小和任何你想要的东西(可能是星形等等),线宽去掉了线条,只留下了标记。工作完美,而且超级简单!
更简单:
import matplotlib.pyplot as plt
handles = [(plt.Line2D([], [], color=x, marker="o", linewidth=0)) for x in colors]https://stackoverflow.com/questions/44098362
复制相似问题