dexplo / bar_chart_race

Create animated bar chart races in Python with matplotlib
MIT License
1.35k stars 351 forks source link

How can I plot from smallest to the largest values? #54

Open antreask opened 2 years ago

antreask commented 2 years ago

Hello,

I am trying to plot the bottom 10 categories from my dataset. I am changing the sort attribute to 'asc' and then it just plots the top 10 categories in ascending order, let's say we have 5 categories e.g. Category | Value category 1| 1 category 2| 2 category 3| 3 category 4| 4 category 5| 5

and I want to plot (animate) the bottom 3. If I change the sort to attribute to asc then the plot is like this: category 3| 3 category 4| 4 category 5| 5

but it should have been category 1| 1 category 2| 2 category 3| 3

Do you have any idea how to fix this?

Thanks a lot, Andreas

bzimons commented 2 years ago

Hey,

I had the same issue. I solved it by changing codes on the original package. I think you can download it, check it out, maybe it can help you!

https://github.com/bzimons/MCspeedrun

antreask commented 2 years ago

Hi,

Thank you for the prompt response. Can you, by any chance, point me out where to look in the code? In any case, thanks a lot for your help.

Andreas

bzimons commented 2 years ago

First you need to put the "bar_chart_race2" folder on the same folder that your project is. The modified code is "_make_chart.py". There are some '#' that indicates where in the code was modified. But basicaly what I did was invert the way Data is shown on the plot, the "lower" bar stays on top.

The code that uses this modified library is "rsg_barplot.py "

It's important to import the library like: import bar_chart_race2 as bcr

The code "rsg_barplot.py " basically do a clean up to the date on the data.frame, put it as the index. and create the plot.

I'm not sure if it will work well with any data, and I forgot lot of what was done. But I hope if you look carefully through the code it can help you :)

antreask commented 2 years ago

Thanks a lot :)

sjberkeley commented 11 months ago

Thank you Beatriz! There were a number of changes in your modified version of _make_chart.py. Most of them are related to formatting of labels. The change needed to fix this reported issue is just one line. Line 893 needs to change from:

df_ranks = df_values.rank(axis=1, method='first', ascending=False).clip(upper=n_bars + 1)

to:

df_ranks = df_values.rank(axis=1, method='first', ascending=True).clip(upper=n_bars + 1)

This worked perfectly on my data. Thanks again.