Python中的树图可视化 [英] Treemap visualization in Python

查看:99
本文介绍了Python中的树图可视化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对绘制



2) squaify



使用matplotlib作为绘图API。
示例代码:

  import matplotlib 
import matplotlib.pyplot as plt
import pandas as pd
进口平方

#画出的质量
#平方面积是城镇的表面积(超级)
#颜色比例是2011年的城镇人口(p11_pop)

#从csv文件中读取数据
#从CAPP opendata中读取数据http://opendata.agglo-pau.fr/index.php/fiche?idQ=27
df = pd .read_excel( Customer Success New.xlsx)
df = df.set_index( location_id)
df = df [[ user_id, company_id]]
df2 = df .sort_values(by = user_id,ascending = False)

#树形图参数
x =0。
y =0。
宽度=100。
高度=100。
cmap = matplotlib.cm.viridis

#总体上的色标
#不包含Pau的最小值和最大值
mini,maxi = df2。 company_id.min(),df2.company_id.max()
norm = matplotlib.colors.Normalize(vmin = mini,vmax = maxi)
colors = [cmap(norm(value))表示df2.company_id]
c olors [1] = #FBFCFE

#正方形的标签
#labels = [ hab%(标签),用于zip(df2.index,df2.user_id)中的标签, df2.company_id)]
#labels [11] = MAZERES%(df2 [ user_id] [ MAZERES-LEZONS],df2 [ company_id] [ MAZERES-LEZONS])

#制作图
图= plt.figure(figsize =(12,10))
图.suptitle(人口与超级社区的CAPP,fontsize = 20)
轴= fig.add_subplot(111,Aspect = equal)
轴= squarify.plot(df2.superf,color = colors,label = labels,ax = ax,alpha = .7)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title( L'aire de chaquecarréest rationelleàla superficie de la commune\n,fontsize = 14)

#颜色栏
#创建带有颜色图的虚拟隐形图像
img = plt.imshow([df2.p11_pop],cmap = cmap)
img.set_visible(False)
fig.colorbar(img,orientation = vertical,收缩= .96)

fig.text(.76,.9,人口, fontsize = 14)
fig.text(.5,0.1,
Superficie totale %d km2,CAPP人口:%d hab%(df2.superf.sum(),df2.p11_pop.sum()),
fontsize = 14,
ha = center)
fig.text(.5,0.07,
来源:http://opendata.agglo-pau.fr/,
fontsize = 14,
ha = center )

plt.show()


I'm interested in drawing a treemap:

What is the easiest way to make one in Python? Is there a library that could produce such a graphic, given the proper input data?

解决方案

You can use:

1) Pygal package

It's simple: http://www.pygal.org/en/stable/documentation/types/treemap.html

2) squarify package

Uses matplotlib as plotting API. Example code:

import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import squarify

# qualtities plotted
# squarre area is the town surface area (superf)
# color scale is the town population in 2011 (p11_pop)

# read data from csv file
# data from CAPP opendata http://opendata.agglo-pau.fr/index.php/fiche?idQ=27
df = pd.read_excel("Customer Success New.xlsx")
df = df.set_index("location_id")
df = df[["user_id", "company_id"]]
df2 = df.sort_values(by="user_id", ascending=False)

# treemap parameters
x = 0.
y = 0.
width = 100.
height = 100.
cmap = matplotlib.cm.viridis

# color scale on the population
# min and max values without Pau
mini, maxi = df2.company_id.min(), df2.company_id.max()
norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi)
colors = [cmap(norm(value)) for value in df2.company_id]
colors[1] = "#FBFCFE"

# labels for squares
#labels = ["hab" % (label) for label in zip(df2.index, df2.user_id), df2.company_id)]
#labels[11] = "MAZERES" % (df2["user_id"]["MAZERES-LEZONS"], df2["company_id"]["MAZERES-LEZONS"])

# make plot
fig = plt.figure(figsize=(12, 10))
fig.suptitle("Population et superficie des communes de la CAPP", fontsize=20)
ax = fig.add_subplot(111, aspect="equal")
ax = squarify.plot(df2.superf, color=colors, label=labels, ax=ax, alpha=.7)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title("L'aire de chaque carré est proportionnelle à la superficie de la commune\n", fontsize=14)

# color bar
# create dummy invisible image with a color map
img = plt.imshow([df2.p11_pop], cmap=cmap)
img.set_visible(False)
fig.colorbar(img, orientation="vertical", shrink=.96)

fig.text(.76, .9, "Population", fontsize=14)
fig.text(.5, 0.1,
         "Superficie totale %d km2, Population de la CAPP : %d hab" % (df2.superf.sum(), df2.p11_pop.sum()),
         fontsize=14,
         ha="center")
fig.text(.5, 0.07,
         "Source : http://opendata.agglo-pau.fr/",
         fontsize=14,
         ha="center")

plt.show()

这篇关于Python中的树图可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆