我正在使用Squarify在 Python 中实现一个简单的树形图。
我正在绘制艺术家姓名及其在所考虑的歌曲图表中的流百分比,正方形越大/越暗,值越高。
我的代码如下:
dataGoals = sort_by_streams[sort_by_streams["Streams"]>1]
#Utilise matplotlib to scale our stream number between the min and max, then assign this scale to our values.
norm = matplotlib.colors.Normalize(vmin=min(dataGoals.Streams), vmax=max(dataGoals.Streams))
colors = [matplotlib.cm.Blues(norm(value)) for value in dataGoals.Streams]
#Create our plot and resize it.
fig1 = plt.figure()
ax = fig1.add_subplot()
fig1.set_size_inches(16, 4.5)
#Use squarify to plot our data, label it and add colours. We add an alpha layer to ensure black labels show through
labels = ["%s\n%.2f" % …
Run Code Online (Sandbox Code Playgroud) 我正在绘制树形图,想知道如何绘制树类的相对百分比,即
A 组 =100
组 B =30
组 C =50
组 D =20
然后,在图中,它应该
在其“X 组”标签旁边添加:“
50%”用于 A 组,“
15%”用于 B 组
等。鉴于此代码,我将如何做到这一点?
!pip install squarify
import squarify
df = pd.DataFrame({'customers':[8,3,4,2], 'cluster':["group A", "group B", "group C", "group D"] })
squarify.plot(sizes=df['customers'], label=df['cluster'], alpha=.8 )
plt.axis('off')
plt.show();
Run Code Online (Sandbox Code Playgroud)
我有一个数据框,我想用它来绘制树图squarify
。我想通过编辑参数在图表上显示country_name
和,但它似乎只采用一个值。counts
labels
示例数据
import squarify
import pandas as pd
from matplotlib import pyplot as plt
d = {'country_name':['USA', 'UK', 'Germany'], 'counts':[100, 200, 300]}
dd = pd.DataFrame(data=d)
Run Code Online (Sandbox Code Playgroud)
fig = plt.gcf()
ax = fig.add_subplot()
fig.set_size_inches(16, 4.5)
norm = matplotlib.colors.Normalize(vmin=min(dd.counts), vmax=max(dd.counts))
colors = [matplotlib.cm.Blues(norm(value)) for value in dd.counts]
squarify.plot(label=dd.country_name, sizes=dd.counts, alpha=.7, color=colors)
plt.axis('off')
plt.show()
Run Code Online (Sandbox Code Playgroud)
预期输出将在图表上同时出现counts
和。country_name