laserson / squarify

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

draw tiny boxes borders of a given colour instead of padding #35

Closed abubelinha closed 1 year ago

abubelinha commented 1 year ago

Probably this is not a squarify issue but some matplotlib-related help requests. As they might be interesting for other squarify-matplotlib new users like me, I dare to ask them here:

(1) When using plot, is it possible to draw boxes with a tiny one pixel border of a chosen contrasting colour (i.e., a black and white dashed or dotted line, so it will be always visible)?

When I run the example below (based in #26), boxes colors are random and I am OK with that ... but sometimes two neighbour colors are so similar I can't tell them apart. (2) I know I can use padding, but then coloured boxes are not of their real sizes (that is specially relevant for the smallest sized boxes: see E box below, which represents 5 square units but looks like 1-sized in the padded graph). A tiny line showing the real box limits would improve visibility without producing this wrong box size impression.

    import matplotlib.pyplot as plt
    import squarify
    squarify.plot(
        label=["A", "B", "C", "D","E","F","G","H"], # list-like used as label text
        sizes=[25, 13, 22, 35, 5, 250, 650, 24], # sum = 1024
        norm_x=32, norm_y=32,  # product = 1024
        pad=False, # white gap from each rectangle border towards its center
        )
    plt.show()

In my plot, A-B-C-D-E are all drawn in a left column, whereas F-G-H occupy most of the remaining area. If you run the script several times, there are chances that random colors are too close to each other, so square boundaries in that left column are almost invisible (see B-C-D or F-H boundaries below).

(3) Another question which you can also see in these examples.
I would expect a 32x32 square graphic, but I see rectangles. Looks like axes ratios are being modified somehow. How can I control this?

Thanks a lot for this great package !! @abubelinha

squarify_example2 squarify_example_pad

laserson commented 1 year ago

Thanks for the issue @abubelinha. Indeed I think this is a matplotlib question, so I suggest you post on one of their forums instead. Please feel free to open any other issues related to the squarify functionality itself.

abubelinha commented 1 year ago

I edited the original post a little bit, since there are 3 separated questions (not sure if 2nd one is pure-matplotlib or half-squarify).

I kinda solved it myself googling about _matplotlib pyplot_. I leave the changes here just in case this helps someone else:

import matplotlib.pyplot as plt
import squarify
squarify.plot(
    label=["A", "B", "C", "D", "E", "F", "G", "H"], # list-like used as label text
    sizes=[25, 13, 22, 35, 5, 250, 650, 24], # sum(sizes) = 1024
    norm_x=32, norm_y=32,  # product = 1024
    pad=False, # don't add a white gap from each rectangle border towards its center
    bar_kwargs = {'edgecolor':'white','lw':1,'ls':'solid'}, # <--- I prefer a tiny white box solid border
    )

# https://stackoverflow.com/questions/332289/how-do-i-change-the-size-of-figures-drawn-with-matplotlib
plt.gcf().set_size_inches(6,6) # <--- changes the default output size (8x6 inches width-height)

plt.show()

I finally opted for a non-dashed border, but here are some other links to play around with style of borders: https://stackoverflow.com/questions/50412224/border-colour-of-matplotlib-filled-boxplots https://stackoverflow.com/questions/59130371/can-the-off-color-be-set-for-a-matplotlib-dashed-line squarify_solved

Thanks a lot again for sharing your work @laserson :+1: :+1: :+1: IMHO it wouldn't hurt to show a simple complete example in squarify docs (like this or any other), to let newbies be using your package a little bit faster.