gyli / PyWaffle

🧇 Make Waffle Charts in Python.
MIT License
578 stars 105 forks source link

Display labels in the same order as grids when plotting in vertical mode #23

Closed alexcb closed 2 years ago

alexcb commented 2 years ago

This PR changes the order of the legend to match the order of the plotted grids when plotting with vertical=True

Before this PR:

before

After this PR:

test

Signed-off-by: Alex Couture-Beil alex@mofo.ca

gyli commented 2 years ago

Thanks for the PR!! I might need to test it with more parameters as it's more related to starting_location. Similar thing happens when starting_location='SE' or 'NE', that blocks are plotted from right to left, while the order of legend is not changed. Thinking if we need a new parameter for flipping the legend.

alexcb commented 2 years ago

The parameter might do the trick.

I noticed a different issue where if I use a sum of values that are less than 100, I get an empty box in the top right corner, when I would expect it to be in the bottom right instead.

test

gyli commented 2 years ago

That is because the default value of starting_location is 'SW', so it starts plotting from bottom left to top right, since it's using coordinate system within matplotlib. So you will have to set starting_location='NW'.

alexcb commented 2 years ago

Setting starting_location='NW' did the trick -- thanks!

data = [50, 40, 9.4]
labels = ['fifty', 'fourty', 'nine point four']
fig = plt.figure(
    figsize=(10.0, 3.0),
    dpi=100,
    FigureClass=pywaffle.Waffle,
    starting_location='NW',
    vertical=True,
    rows=10,
    columns=10,
    values=data,
    labels=labels,
    legend={'loc': 'upper left', 'bbox_to_anchor': (1, 1)},
)
fig.savefig('test.png', bbox_inches='tight')

produces

test

It does seem a bit strange that one needs to specify both vertical=True and starting_location='NW' to get a plot like this with the legend correctly ordered. One possibility could be to set starting_location='auto' which would pick NW when vertical, and maintain the existing default otherwise. Then in the case of the starting location being South, then perhaps the legend order should be reversed?

gyli commented 2 years ago

I am trying to compare these two solutions:

  1. Your solution, adding starting_location='auto'. Not as default value for now, but could be default in future version. When vertical=True, it's just starting_location='NW' Pros: match the legend of course, especially without flipping the legend. Otherwise the order of values and legend would be different too. Cons: auto is not clear enough to explain how the starting_location is chosen

  2. Adding parameter flip_legend, allowing us to flip it manually when needed. Pros: Consistent plotting direction; User is notified when legend and values follow different order, as they need to manually specify this parameter Cons: Flipped legend order might be unexpected as it's different to value order

Any idea or any pros/cons you would like to add?

alexcb commented 2 years ago

After playing around a bit more with different data points, I would agree with 2, seems like it offers the most flexibility for various users' cases without trying to guess what the user wants to do.