laserson / squarify

Pure Python implementation of the squarify treemap layout algorithm
Other
293 stars 36 forks source link

Squarify/?Matplotlib plot incorrectly #6

Closed scienceML closed 7 years ago

scienceML commented 7 years ago

I am running the code below from the gist "https://gist.github.com/gVallverdu/0b446d0061a785c808dbe79262a37eea":

import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import pandas as pd import squarify

df = pd.read_csv("C:/PProjects/structure_population.csv", sep=";") df = df.set_index("libgeo") df = df[["superf", "p11_pop"]] df2 = df.sort_values(by="superf", ascending=False)

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

mini, maxi = df2.drop("PAU").p11_pop.min(), df2.drop("PAU").p11_pop.max() norm = matplotlib.colors.Normalize(vmin=mini, vmax=maxi) colors = [cmap(norm(value)) for value in df2.p11_pop]

labels = ["%s\n%d km2\n%d hab" % (label) for label in zip(df2.index, df2.superf, df2.p11_pop)] labels[11] = "MAZERES-\nLEZONS\n%d km2\n%d hab" % (df2["superf"]["MAZERES-LEZONS"], df2["p11_pop"]["MAZERES-LEZONS"])

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 carre est proportionnelle a la superficie de la commune\n", fontsize=10)

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=10) 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/", 2 fontsize=14, ha="center") plt.savefig('foo.png') `

If I plot manually using rects coordinates , then it render correctly. Tried on both python 2.7/3/6 , Windows, PyCharm for GUI coding, backend for matplotlib is TkAgg, changing to Agg /creating image did not make difference.

My output plot is below foo

scienceML commented 7 years ago

Small comment: I tried using Anaconda and it worked perfectly so I guess nothing wrong with "squarify". Probably matplotlib is reading differently the coordinates.

scienceML commented 7 years ago

Please close it - I could not replicate after installing everything on my new computer

emilecaron commented 7 years ago

@scienceaiden i'm running into the same issue. Did you change matplotlib version when you solved your problem?

emilecaron commented 7 years ago

this seems to be a regression introduced with matplotlib 2.0 i submitted a fix (https://github.com/laserson/squarify/pull/7), until then you might want to pass align='edge' when calling squarify.plot()

scienceML commented 7 years ago

@emilecaron no, like I said, after installing on the new computer all my tools from scratch, the problem disappeared