This article first appeared in the quantitative investment and Machine Learning
Reproduced in https://mp.weixin.qq.com/s/KaB_7oXZf0_IV97y0pRPmQ
Recently, this sort of dynamic video super fire bar, as shown below:
Specifically, this map can be called: Bar Chart Race, what is the country's GDP, a certain history of ups and downs and so on, in order to attribute identity number of the public, we created this simple visualization video for everyone: 2 015 ~ 2019 Chinese private equity market changes , first to see it now!
You can make more interesting to visualize data based private equity fund! Everyone is invited to also contribute oh!
Choose a day as collision, public No. taught you today!
We use the most popular Python language , based on Matplotlib to achieve dynamic effects above. Cases and data since 1500 the world's 10 largest cities ranked.
Ready to work
Import-related library:
Use pandas urban population read the relevant data set. We need only four data: 'name', 'group', 'year', 'value'. A name is mapped to a group, there is a value each year.
The original data set:
Data Conversion
Use pandas conversion, the first 10 values obtained.
Foundation drawing
Let's draw a basic histogram. First, create a graphic and a coordinate axis. Then ax.barh (x, y) draw a horizontal bar graph.
Colors, labels
Next, let's add value labels and color groups according to the group. Use colors and add color to group_lk bar.
group_lk is a mapping between a group name and a value.
Details of the characterization
For convenience, we will migrate the code to draw_barchart function:
-
Text: update the font size, color, direction
-
Axis: x-axis to the top, and add color subtitle
-
Grid:在bars添加
-
Format:逗号分隔值和轴标记符
-
Remove:框架,y轴标签
动态化,激动人心!
要对上图进行动画处理,我们将使用 matplotlib.animation 中的 FuncAnimation。
Functionatio 通过反复调用函数(在画布上绘制)来创建动画。在本例中,该函数将是 draw_barchart。 我们使用 frames,这个参数接受你想运行的 draw_barchart 的值——我们将从1968年运行到2018年。
我们还可以通过下面的红框,调整速度、倒退等操作。
你可以将动画对象保存为视频或gif形式:
OK,大功告成!赶快一显身手吧!
整个代码不到40行!
获取代码
链接:https://pan.baidu.com/s/1O-f2tQFErHJ-APjopGGqBA(或点击阅读原文)
密码:c9ow