回答
matlab方法實際上是一個SciPy食譜,可在http://scipy-cookbook.readthedocs.io/items/Matplotlib_TreeMap.html – MERose 2017-02-22 12:17:26
您可以使用Pygal庫,這是如此簡單
http://pygal.org/en/stable/documentation/types/treemap.html
另一種方法是你可以使用squarify庫,這裏就是我使用了代碼
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()
儘管這個鏈接可能回答這個問題,但最好在這裏包含答案的基本部分,並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 - [來自評論](/評論/低質量帖子/ 17434830) – Oz123 2017-09-25 07:26:23
好吧,感謝您的反饋,我會更新我的答案 – 2017-09-25 07:44:34
@NabihIbrahimBawazir看起來好多了,謝謝你更新你的答案! – g00glen00b 2017-09-25 08:13:03
- 1. python中的樹形圖
- 2. 樹形圖和列表視圖
- 3. 圖形在Python
- 4. 家族樹的Python簡單圖形化表示
- 5. treeview內樹視圖?或者如何在樹形視圖中顯示列表
- 6. Python分析器輸出的樹形圖可視化視圖?
- 7. 使用動作樹形圖
- 8. 樹形視圖中的項
- 9. VC++樹形視圖控件
- 10. 自定義樹形視圖
- 11. D3樹形圖標頭
- 12. 排序hasmap樹形圖
- 13. 樹形視圖組合框
- 14. 樹形圖高度太長
- 15. DataBind Telerik樹形視圖
- 16. Silverlight庫創建樹形圖
- 17. 樹形圖和字節[]
- 18. MySql的PHP樹形視圖
- 19. 樹形圖構造函數
- 20. 樹形圖比較器
- 21. ANTLR的樹語法AST圖形視圖
- 22. 生成樹/圖形的漂亮圖像
- 23. 將樹形圖添加到ggplot2熱圖
- 24. ř圖形:在圖表上
- 25. 條形圖Python
- 26. mfc將圖像加載到樹形控件的圖像列表
- 27. 將數據標籤添加到樹形圖圖表
- 28. 保持樹形圖在引導區div
- 29. Highcharter樹形圖不在shinydashboard中渲染
- 30. 如何在Python中表示圖形/樹以及如何檢測週期?
7年過去了,仍然沒有像樣的python軟件包能夠繪製簡單的treemap。下面列出的答案仍然是最先進的,但非常無益。使用R或js它是兩行代碼的問題,但爲什麼在Python中如此複雜?我不明白。 – MERose 2017-02-22 12:49:35