Hello everyone, today we are going to talk about how to use Pyecharts to create dynamic ranking charts 👇

The TimeLine in Pyecharts is used to create such a dynamic graph. The code is actually a little difficult to implement, but I hope to explain the process of making such a dynamic graph so that readers can use Pyecharts to animate any kind of graph. Let’s begin!

First of all, we need to think about the generation logic of such a dynamic graph, isn’t it to make a bar graph of daily data and then rotate it? OK, so what kind of data should we organize? A Dataframe, each column containing 20 days’ worth of data for a country, and a list containing 20 days’ worth of data 👇




Once we have the data we’ll go to the official Pyecharts sample site and find a similar diagram

http://gallery.pyecharts.org/#/Timeline/timeline_bar_with_graphicCopy the code

All we need to do is change this code to the form we want it to be. Now let’s copy this code into the Notebook and modify it to display the image in the Notebook

Next we look at the code and figure, the first thing to change is to change it from two groups of variables to one group of variables, and then delete and modify some unnecessary words 👇

The left side of the figure is the code before modification, and the right side is the modified part of the code. There is no need to do more explanation and look at the figure directly. Now our figure is like this 👇

Isn’t that interesting, and now the part that’s going to be a little bit harder is changing the axes and the corresponding data, so let’s look at how this code controls the axes and the data, okay

So you can see that the X-axis calls faker.choose (), and the Y-axis calls faker.values () prints let’s make a separate call and see

You see, these two methods return a list at a time, and the list changes, and notice that the X is written outside the loop, so the X-axis stays the same, but the Y-axis is written inside the loop, so that the graph is generated differently each time.

So our next goal is clear, because our bar graph is sequential, we need to write two functions that return a set of X-axis data and a set of Y-axis data in order to generate each graph, so we can write 👇

To explain the changes a little bit, the get_value() function passes in df1 and passes out each row of data. K is used to identify the number of calls so that the next row of data is called each time. In the loop, the number of confirmed Pandas and the country names are listed in descending order. The number of confirmed Pandas is listed in descending order.

And now we’re done, replacing the X and Y of the source code with this X and Y

Actually, at this point, it is over, click play to implement the wheel, but then you can modify some of the code slightly to adjust the details

The above two changes are to adjust the switching time of each graph, auto play and reverse the XY axis, and now you are done, your bar graph is successfully moving 👇




That’s how to create a dynamic bar chart from 0 to 1. If you’re interested, type the code. Note that this article uses the Bar chart example, but whether it’s a Bar chart or a pie chart or a Line chart or even a map, the code is a Bar or a Line, and making a different dynamic diagram is just a matter of changing a Line of code. Let’s review the previous steps: Think of the diagram you want to make — > find a demo from the official website — > look at the code — > modify some code — > Done! Have you learned?

Note: The data and code used in this article are available in Python