Визуализация карты дерева в Python

Мне интересно нарисовать карту дерева:

пример древовидной карты

Какой самый простой способ сделать это в Python? Есть ли библиотека, которая могла бы создать такую ​​графику, учитывая правильные входные данные?


person Primoz    schedule 19.07.2010    source источник
comment
Спустя 7 лет все еще смешно, что нет приличного пакета Python, способного построить простую древовидную карту. Ответ, приведенный ниже, по-прежнему актуален, но крайне бесполезен. С R или js это вопрос двух строчек кода, но зачем так сложно в питоне? Я не понимаю.   -  person MERose    schedule 22.02.2017
comment
На самом деле есть squarify, который упрощает задачу: python-graph-gallery.com/200-basic-treemap-with-python   -  person Nickolay    schedule 26.05.2018


Ответы (4)


В кулинарной книге SciPy есть пример с использованием matplotlib, но без меток.

Ссылка IA: https://web.archive.org/web/20150324163314/http://wiki.scipy.org/Cookbook/Matplotlib/TreeMap

person nkint    schedule 02.04.2011

Вы можете использовать:

1) Pygal пакет

Это просто: http://www.pygal.org/en/stable/documentation/types/treemap.html

2) squarify пакет

Использует matplotlib в качестве API для построения графиков. Пример кода:

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()
person Nabih Ibrahim Bawazir    schedule 25.09.2017
comment
Хорошо, спасибо за отзыв для вас обоих, я обновлю свой ответ. - person Nabih Ibrahim Bawazir; 25.09.2017
comment
@NabihIbrahimBawazir выглядит намного лучше, спасибо за обновление вашего ответа! - person g00glen00b; 25.09.2017

Они могут быть использованы:

Я заметил, что в pypi также есть библиотека древовидных карт. Я не пробовал это.

person SiggyF    schedule 19.07.2010
comment
Подход Matlab на самом деле представляет собой кулинарную книгу SciPy, доступную по адресу scipy-cookbook.readthedocs.io/items. /Matplotlib_TreeMap.html - person MERose; 22.02.2017

Вот несколько вариантов:

person ars    schedule 19.07.2010
comment
Плохо документировано. Не могу их рекомендовать. - person MERose; 22.02.2017