Create Racing Bar Graph - Python Plotly
Вставка
- Опубліковано 21 лют 2020
- Learn how to create a Racing Bar graph all in Python, using Plotly. We will be using real-life data on suicide rates around the world. Links below:
Racing Bar Chart video example:
• Highest Suicide Rates ...
The Code:
github.com/Coding-with-Adam/D...
Data:
drive.google.com/file/d/1x_TY...
Plotly Bar Graph Documentation:
plot.ly/python/reference/#bar
Plotly Forum:
community.plot.ly/c/dash
Video Layout:
00:39 - Data Used
02:00 - Dictionary of Frames/Years
05:52 - Creating the Figure
09:37 - The Play Button
13:40 - Final Result
15:11 - Tip of the Week - Наука та технологія
Hello, thanks for this video. From this video I am able to create a racing graph. Just wanted to know if the bar movement animation is possible using Plotly?
I saw multiple videos where it is there, but all of them were made with flourish studio (I guess)
Hi Ajinka, yes it is. See this video ua-cam.com/video/N1GwQNatOwo/v-deo.html . At 22 minutes and 45 seconds, I talk about bar animation.
I wonder if there is a way to create those fancy racing bar graph where you can see the effect of bars jumping over bars vertically, as their values exceed the values of those on top of them.
I don't think that's possible with Plotly yet, at least not the type I've seen on youtube.
Hello and thanks for the great content. Just a quick question. Can I have a racebar for each one of the continents using a dropdown? So basically do what you did for Europe, but for all the continents and then i choose each one of them via a dropdown.
Hi @Uni 13 , Yes you can. It's a bit more complicated because each continent has a different x_axis and y_axis length, so you would have to reconfigure each axis according to the continent chosen in the Dropdown by the user. It would be a lot of coding, but yes, it is possible.
@@CharmingData Thank you for the answer. One more question. Which is the parameter that makes the number of x (suicide number ) visible on each bar while they are moving. Thank you.
@@uni1357 For sure. "text=n_frame['one']" is the parameter that adds the text to the bar and makes it visible. "texttemplate=..." is the parameter you can use to change the template of the text.
Hello, there Thank you again for this wonderful explanation. In my graph the text in the bar with maximum value disappears and reappear time to time. How do I address the issue. I tried defining range but not to my avail.
hi Ravi, I'm not sure why this is happening. It depends on your data and your code, and many others things. It's hard to say. If you'd like, I can take a look at your code and sample data and try to find the reason. You can find me on Plotly Forum community.plotly.com/
@@CharmingData I always end up here hehe. Will get you in the community. Thank you
Hi, great content!! I have one question:
How to make the transition more smooth like we could find in other Race Bar Charts using Flourish or Power Bi?
More precisely I would like to see the bars moving from one position to another during the transition from one value to the new one.
This snippet of code seems to do the job:
buttons=[dict(label="Play",
method="animate",
# github.com/plotly/plotly.js/blob/master/src/plots/animation_attributes.js
args=[None,
{"frame": {"duration": 2*1000, "redraw": True}, # 10*1000ms
"transition": {"duration":250,
"easing": "linear-in"}}]
What do you recommend for the argument "easing" to achieve the desired "moving effect"?
Hi Mastafa, Great code. I actually tried different "easing" values, but none have been better than the "linear" one that I'm using. If you find one that is better, I would love to hear it :)
@@CharmingData Thanks for your reply, I've been trying a couple of the existing 'easing' values and none gives a real-time effect unfortunately...
@@mastafafoufa5121 I'm sorry to hear that. If you find a way to create a real-time effect, I would love to hear it. Thank you, Mastafa
Any Chance you could show how to add a slider and a pause button for this or on Plotly Express? Thanks!!
Hi Dave, this video shows how Plotly Express creates an animation with a stop button that acts like a pause button. ua-cam.com/video/RCUrpCpGZ5o/v-deo.html What do you mean by slider? Do you want a separate slider?
@@CharmingData Hi, yes I meant a separate slider, so that you could go through each frame, as well as have the Pause button. However, I found the solution yesterday.
There is an example of Plotly Express that can be modified to a Horizontal barchart race: (second example from the top)
plotly.com/python/animations/
However could not get them ordered as in actual bar chart race, until I read this example:
community.plotly.com/t/plotly-express-histogram-any-way-to-sort-bar-by-value/23905/7
Anyway, thanks for the quick response, I'll have to check out your new video with APIs :)
Sir, I want to do a Bar chart race on Corona. i will Share Dataset of Corona from WHO. I Have Problem in Coding... So can you help me
I Goutam, It's complicated to work on code on youtube. After trying to write your code, if you're having difficulties, please join this forum community.plot.ly/ and pose your questions there. Me and the community will try our best to help you out.
If you came to learn how to increase the suicide rate, this video is not the thing you should watch. Otherwise, its very interesting.
how can i change the code if my data set doesn't have a color code column?
Hi Niki, just erase this line of code: "marker={'color':n_frame['one']['color_code']}"
@@CharmingData is there any way to add in colors for our bar chart when we don't have the pre-set color column in the data?
@@niki6399 yes, Try adding color name to marker. For example: marker={'color':'blue'}
@@CharmingData thanks. this worked for changing all of the bars but i'm trying to figure out how to change it for each of the 5 bars in the end result.
@@niki6399 Hi Niki, do you mean that you'd like to set a certain color for each of the 5 country bars at the end?
I think the best way to do that is by presetting those colors in pandas.
can we plot by dates instead of years ????
Hi Raj, sure you can, if the data has date values instead of the year values. Just insert dates in the xaxis instead of years.
@@CharmingData hie thanks for the reply i tried using dates not able to plot it properly if you can explain to me with an example it would be highly appreciated. Thanks in advance.
@@rajsaraiya7125 hi Raj, what is the error message that you get?
@@CharmingData hello actually i am having daily dates which are updating every day so I am unable to create a list of dates which will iterate over all the dates cause for your dataset the years are fixed.