gpt4 book ai didi

每个句柄具有不同数量和颜色标记的 Matplotlib 图例

转载 作者:行者123 更新时间:2023-12-05 05:22:51 29 4
gpt4 key购买 nike

鉴于以下情况:

import pandas as pd
import matplotlib.pyplot as plt
d=pd.DataFrame({'category':['a','a','a','b','b','b'],
'year':[1,2,1,2,1,2],
'x':[2,4,5,1,2,3],
'y':[1,2,3,2,4,6],
'clr':['grey','green','grey','blue','grey','orange']})
d
category clr x y year
0 a grey 2 1 1
1 a green 4 2 2
2 a grey 5 3 1
3 b blue 1 2 2
4 b grey 2 4 1
5 b orange 3 6 2

for i in np.arange(len(d)):
plt.plot(d.x[i],d.y[i],marker='o',linestyle='none',markerfacecolor=d.clr[i],
markeredgecolor='none',markersize=15)
#clean up axes
plt.tick_params(axis='x',which='both',bottom='off',top='off',color='none',labelcolor='none')
plt.tick_params(axis='y',which='both',left='off',right='off',color='none',labelcolor='none')
lgnd=plt.legend(['Year 1','Year 2'],
numpoints=1,
loc=0,
ncol=1,
fontsize=10,
frameon=False)
lgnd.legendHandles[0]._legmarker.set_markersize(15)
lgnd.legendHandles[1]._legmarker.set_markersize(15)

enter image description here

我希望图例在第 1 年标记有一个灰点(目前如此),但对于第 2 年标记,每个不同颜色都有一个点(在本例中,橙色、蓝色和此时,同一行上的绿点顺序无关紧要,连续)。像这样:

enter image description here

我尝试了以下方法,但无济于事:

lgnd.legendHandles[1]._legmarker.set_numpoints(len(d.clr.unique()))
lgnd.legendHandles[1]._legmarker.set_markeredgecolor(d.clr)

提前致谢!

最佳答案

我很高兴为您的问题找出解决方案(并在此过程中学习了一些新技巧)。本质上,您可以创建自己的图例处理程序对象以将所有颜色映射到年份。可以通过制作具有函数 legend_artist(self, legend, orig_handle, fontsize, handlebox) 的任何对象来制作自定义图例处理程序。可以在 this page 的“实现自定义处理程序”部分中找到有关其工作原理的详细信息。 .我把代码中的所有解释都注释掉了,因为文字太多,没有代码可以证明。

示例代码:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pdb

import matplotlib.patches as mpatches


class MyLegendHandler(object):
def __init__(self,color):
self.color = color

def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0, y0 = handlebox.xdescent, handlebox.ydescent #offset of the lower left corner
width, height = handlebox.width, handlebox.height #width, height bound box of legend, for now, it is the dimension of each circle legend
#NOTE: to be practicle, let's just set radius = height as if width != height, it's an ellipse
#NOTE: these will latter on be changed internally to accomdate adding text
handlebox.width += len(self.color)*height # reset width of handlebox to accomodate multiple legends
for i in range(len(self.color)): #loop through all colors
#for each color, draw a circle of that color
#NOTE: play around here to align the legends right rather than left :)
center = [0.5*(i + 1) * width - 0.5*x0, 0.5 * height - 0.5 * y0]
patch = mpatches.Ellipse(center, height, height, facecolor=self.color[i],
edgecolor=None, hatch=None, transform=handlebox.get_transform())

handlebox.add_artist(patch)
return patch


###################################

d=pd.DataFrame({'category':['a','a','a','b','b','b'],
'year':[1,2,1,2,1,2],
'x':[2,4,5,1,2,3],
'y':[1,2,3,2,4,6],
'clr':['grey','green','grey','blue','grey','orange']})
unique_year_elements = []
years_seen = []
tmp = None
my_map = {}
for i in np.arange(len(d)):
tmp, = plt.plot(d.x[i],d.y[i],marker='o',linestyle='none',markerfacecolor=d.clr[i],
markeredgecolor='none',markersize=15)
#collect the plot elements that are of unique years-- 1 year might have several plot element, we only need 1
if not (d.year[i] in years_seen):
years_seen.append(d.year[i])
unique_year_elements.append(tmp)

#build handler_map for plt.legend to map elements to its legend handler object
for i in np.arange(len(years_seen)):
color_list = d.loc[d['year'] == years_seen[i]].clr.unique().tolist()
#pdb.set_trace()
my_map[unique_year_elements[i]] = MyLegendHandler(color_list)

#creating the legend object
plt.legend( unique_year_elements, ["Year "+str(y) for y in years_seen],
handler_map=my_map)
#clean up axes
plt.tick_params(axis='x',which='both',bottom='off',top='off',color='none',labelcolor='none')
plt.tick_params(axis='y',which='both',left='off',right='off',color='none',labelcolor='none')

plt.show()

示例输出:

enter image description here

关于每个句柄具有不同数量和颜色标记的 Matplotlib 图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39538863/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com