Python 如何使用数据框在matplotlib中创建图例?

Python 如何使用数据框在matplotlib中创建图例?,python,pandas,matplotlib,data-visualization,seaborn,Python,Pandas,Matplotlib,Data Visualization,Seaborn,我已经用squarify创建了一个树状图,现在我正在尝试创建一个图例,以便在树状图中图形旁边显示数据 内置的legend函数没有生成我想要的图例(它当前正在显示我的数据帧的第一列和每行的索引),因此我一直在尝试使用它,但没有成功。我希望传奇是: 单位SKU体积 a 1 b 2 C3 d 4 e 5 f 6 g 7 H20 找到了一种方法。 我删除了标签,并使用plt.table创建了我的图例版本。 还利用了我创造的颜色,使传奇看起来更漂亮 def generate_legend_表(ax、颜色

我已经用squarify创建了一个树状图,现在我正在尝试创建一个图例,以便在树状图中图形旁边显示数据

内置的legend函数没有生成我想要的图例(它当前正在显示我的数据帧的第一列和每行的索引),因此我一直在尝试使用它,但没有成功。我希望传奇是:

单位SKU体积

a 1

b 2

C3

d 4

e 5

f 6

g 7

H20


找到了一种方法。 我删除了标签,并使用plt.table创建了我的图例版本。 还利用了我创造的颜色,使传奇看起来更漂亮

def generate_legend_表(ax、颜色、无库存单位到图、数据片):
# ___________________________________________________________________________________
#描述
#此函数用于生成一个图例表,以便使用树形图打印
#------------------------------------------------------------------------------------
#论据
#ax:要绘制表格的子地块轴
#颜色:matplotlibs颜色库生成的颜色列表对象
#no_of_SKU_to_图:树图中表示了多少SKU
#数据片:要写入表中的数据
# ___________________________________________________________________________________
#从标准化颜色对象创建十六进制颜色列表
十六进制列表=[]
对于范围内的n(透镜(颜色)):
hex_list.append(mpl.colors.to_hex(colors[n]))
#在所需轴上创建表格对象
图例\u table=ax.table(cellText=data\u slice.values,
colLabels=数据_slice.columns,
loc='right',
colLoc='right',
冷宽=[0.2,0.2],
边=“”)
#更改表格文本颜色
i=0
而我
import matplotlib as mpl
import squarify
import matplotlib.cm
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

#create figure
fig = plt.gcf()
fig.set_size_inches(16, 5)

#set data values
data = [['a',1],['b',2],['c',3],['d',4],['e',5],['f',6],['g',7],['h',20]]
data_slice = pd.DataFrame(data, columns=['Atom SKU Code','Total Volume'])

print(data_slice)

#create color set
norm = mpl.colors.Normalize(vmin=min(data_slice['Total Volume']), vmax=max(data_slice['Total Volume']))
colors = [mpl.cm.BuGn(norm(value)) for value in data_slice['Total Volume']]

#plot figure
ax1 = squarify.plot(label=data_slice['Atom SKU Code'], sizes=data_slice['Total Volume'], color=colors, alpha=.6)
plt.title("Volume by SKU (Units Sold)", fontsize=23, fontweight="bold")
plt.axis('off')
plt.legend(title='SKU Volume in Units', loc='center left',bbox_to_anchor=(1, 0.5),frameon=False)
plt.tight_layout()
plt.show()
def genereate_legend_table(ax,colors,no_of_skus_to_graph,data_slice):
# ___________________________________________________________________________________
# DESCRIPTION
# This functions generates a legend table to be plotted with the treemap plots

#------------------------------------------------------------------------------------
# ARGUMENTS
# ax: subplot axis where the table is to be plotted

# colors: color list object generated by matplotlibs color library

# no_of_skus_to_graph: how many skus are being represented in the tree map

# data_slice: the data to be written in the table
# ___________________________________________________________________________________
# Create hex color list from normalized color object
hex_list = []
for n in range(len(colors)):
    hex_list.append(mpl.colors.to_hex(colors[n]))


# Create table object on desired axis
legend_table = ax.table(cellText=data_slice.values,
                          colLabels=data_slice.columns,
                          loc='right',
                          colLoc='right',
                          colWidths=[0.2, 0.2],
                          edges='')

# Change table text color
i = 0
while i <= no_of_skus_to_graph - 1:
    legend_table[(i + 1, 0)].get_text().set_color(hex_list[i])
    legend_table[(i + 1, 1)].get_text().set_color(hex_list[i])
    s = '¥{:,.2f}'.format(float(legend_table[(i + 1, 1)].get_text().get_text()))
    legend_table[(i + 1, 1)].get_text().set_text(s)
    i = i + 1

# Return table
return legend_table