Python画树状图
程序员文章站
2022-10-04 08:39:55
import matplotlib.pyplot as pltimport squarifymy_dpi=96plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)squarify.plot(sizes=[13,22,35,5], label=["group A", "group B", "group C", "group D"], alpha=.7 )plt.axis('off')plt.show()import mat....
import matplotlib.pyplot as plt
import squarify
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
squarify.plot(sizes=[13,22,35,5], label=["group A", "group B", "group C", "group D"], alpha=.7 )
plt.axis('off')
plt.show()
import matplotlib.pyplot as plt
import squarify
import pandas as pd
df = pd.DataFrame({'nb_people':[8,3,4,2], 'group':["group A", "group B", "group C", "group D"] })
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
squarify.plot(sizes=df['nb_people'], label=df['group'], alpha=.8 )
plt.axis('off')
plt.show()
import matplotlib.pyplot as plt
import squarify
import pandas as pd
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
squarify.plot(sizes=[13,22,35,5], label=["group A", "group B", "group C", "group D"], color=["red","green","blue", "grey"], alpha=.4 )
plt.axis('off')
plt.show()
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import squarify
my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
my_values=[i**3 for i in range(1,100)]
cmap = matplotlib.cm.Blues
mini=min(my_values)
maxi=max(my_values)
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in my_values]
squarify.plot(sizes=my_values, alpha=.8, color=colors )
plt.axis('off')
plt.show()
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import squarify
import pandas as pd
df = pd.DataFrame({
'group':["group A", "group A", "group A", "group A", "group B", "group B", "group C", "group C", "group C"],
'subgroup':[1,2,3,4,1,2,1,2,3],
'value':[13,5,22,12,11,7,3,1,23]
})
mycolors=list()
all_pal=[matplotlib.cm.Blues, matplotlib.cm.Greens, matplotlib.cm.Oranges]
num=-1
for i in df.group.unique():
num+=1
cmap = all_pal[num]
tmp=df[df.group==i]
mini=min(tmp.value)
maxi=max(tmp.value)
norm = matplotlib.colors.Normalize(vmin=mini-12, vmax=maxi+12)
colors = [cmap(norm(value)) for value in tmp.value]
mycolors=mycolors+colors
squarify.plot(sizes=df['value'], alpha=.8, color=mycolors )
plt.axis('off')
plt.show()
本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。
本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py
本文地址:https://blog.csdn.net/weixin_41869644/article/details/107450277