laserson / squarify

Pure Python implementation of the squarify treemap layout algorithm
Other
297 stars 37 forks source link

Release 0.4 breaks passing matplotlib.plot() kwargs #15

Closed GillesJ closed 5 years ago

GillesJ commented 5 years ago

Release 0.4.0 breaks passing matplotlib.plot() kwargs in the squarify.plot() method. This worked in v0.3.0. This feature is mainly useful for plotting squarify output on matplotlib figures. For instance, in the code example below I use a figure with fixed size to plot a treemap on.

import squarify

def plot_type_treemap_matplot(type_df, fp="type_treemap_matplot.png"):

    type_df["label"] = type_df.apply(lambda x: f"{x.name}\n{x['pct']}% (n={x['n'].astype('int')})", axis=1)

    figsize = [9, 5]
    plt.rcParams["figure.figsize"] = figsize
    cmap = plt.get_cmap("tab20", lut=len(type_df.index))
    # Change color
    fig = plt.figure(figsize=figsize, dpi=300)

    squarify.plot(sizes=type_df["n"], label=type_df["label"],
                  color=cmap.colors, alpha=.4, figure=fig)  # This does not work in v0.4
    plt.title("Distribution of event categories in SENTiVENT English corpus.", fontsize=12, figure=fig)
    plt.axis('off', figure=fig)
    plt.show()
    fig.savefig(fp)

In v0.4.0 this introduced AttributeErrors for the "figure" and "alpha" kwargs, while it produces the expected output in v0.3.0.

laserson commented 5 years ago

Yes, that was careless of me. I'll push a fix shortly.

laserson commented 5 years ago

Ok, I pushed a 0.4.1 that should've fixed this. Please let me know if there are still problems.

GillesJ commented 5 years ago

Thanks for the quick fix and writing this package! Makes for a nice visualization of category frequency in my datasets.

laserson commented 5 years ago

Btw, the fix I pushed was broken on all but py36. The latest (0.4.2) should be good on all python versions.