0

我正在根据以下内容创建树形图,df其中每个country在 col 中都有自己的颜色color

import pandas as pd  
import matplotlib
import matplotlib.pyplot as plt
import squarify 
  
data = {'name': ['Marcus Raboy', 'Jay Karas', 'Cathy Garcia-Molina', \
                 'Jay Chapman', 'Raúl Campos, Jan Suter', 'David Dhawan', \
                 'Shannon Hartman', 'Ryan Polito', 'Hakan Algül', 'Martin Scorsese'], \
        'counrty':['United States', 'United States', 'Philippines', 'United States', \
                   'Mexico', 'India', 'United States', 'United States', 'Turkey', 'United States'], \
        'color': ['yellow', 'yellow', 'blue', 'yellow', 'green', 'red', 'yellow', 'yellow', 'grey', 'yellow'],
        'show_cnt': [15, 14, 13, 12, 9, 9, 8, 8, 8, 8]}  

df = pd.DataFrame(data)  

我在这个片段中绘制了树形图:

fig = plt.gcf()
ax = fig.add_subplot()
fig.set_size_inches(23, 10)
squarify.plot(sizes=df.show_cnt, label=df.director, color=df.color, alpha=.5)
plt.axis('off')
plt.show()

树状图

我尝试将此代码用于图例:

plt.legend(handles=df.color, labels=df.country)

输出:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

如何根据颜色国家模式渲染图例?

4

0 回答 0