The easiest way! ! Generate dynamic bar graph with python
Article Directory
- The easiest way! ! Generate dynamic bar graph with python
-
- 1. Install the third-party package: bar_chart_race
- 2. Data preparation
- 3. Function explanation
- 4. Detailed examples (copy and use)
-
- 4.1 Basic bar chart
- 4.2 Histogram
- 4.3 Sorting diagram
- 4.4 Limit the number of bars
- 4.5 Order of fixed bars
- 4.6 Maximum fixed axis
- 4.7 Set the number of frames
- 4.8 Setting the step length and dwell time
- 4.9 Add insertion time
- 4.10 Set font properties
- 4.11 Customize bar attributes
- 4.12 Add text description
- 4.13 Increase the average vertical bar
- 4.14 Custom vertical bar-maximum
- 4.15 Bar graph colors are not repeated
- 5. Problems encountered
Recently, dynamic bar graphs are very popular. On video sites such as station B, such videos often have millions of views. Today, we use a third-party library: bar_chart_race (version 0.2) to generate dynamic bar graphs; The generated effect is shown in the figure:
Problem: I found that following the operations in the current popular articles, there will be a lot of errors , some of which I have solved, and some of which I have not solved. Now record the current progress here; the data and procedures are in Here: click to download ;
1. Install the third-party package: bar_chart_race
First of all, we must understand: only version 0.2 supports dynamic bar graphs, so we must ensure that the third-party library we install is version 0.2;
unfortunately, the third-party package we installed through the pip install bar_chart_race command is version 0.1. It cannot be upgraded either; therefore, we recommend downloading on github and then installing; the installation steps are as follows:
-
First of all, make sure you have installed the github software. If not, please download it here for windows: https://git-scm.com/download/win , click on your own version to download, and then install; the same goes for other systems~~
-
After git is installed, switch to the directory you want to download in cmd, and download:
git clone https://github.com/dexplo/bar_chart_race.git
Among them, https://github.com/dexplo/bar_chart_race.git is its git address;
the directory I downloaded here is:, theD:\PY_useful\3_python 基础
download is complete After that, a folder will be generated:bar_chart_race
-
Enter the folder in cmd, and then run:,
python setup.py install
after the installation is complete, it will prompt:,Finished processing dependencies for bar-chart-race==0.2.0
seeing this, it proves that we are successful!
2. Data preparation
The data needs to meet the following conditions:
- Each row must be a single date of data;
- Each column is a separate category;
- The index is preferably date data (optional);
- The data must be changed to pandas.DataFrame format;
Official website https://www.dexplo.org/bar_chart_race/
, examples of official websites are as follows:
date | Belgium | China | France | Germany | Iran | Italy | Netherlands | Spain | USA | United Kingdom |
---|---|---|---|---|---|---|---|---|---|---|
2020-04-08 | 2240 | 3337 | 10887 | 2349 | 3993 | 17669 | 2255 | 14792 | 14704 | 7111 |
2020-04-09 | 2523 | 3339 | 12228 | 2607 | 4110 | 18279 | 2403 | 15447 | 16553 | 7993 |
2020-04-10 | 3019 | 3340 | 13215 | 2767 | 4232 | 18849 | 2520 | 16081 | 18595 | 8974 |
2020-04-11 | 3346 | 3343 | 13851 | 2894 | 4357 | 19468 | 2653 | 16606 | 20471 | 9892 |
2020-04-12 | 3600 | 3343 | 14412 | 3022 | 4474 | 19899 | 2747 | 17209 | 22032 | 10629 |
However, the official website are examples of the use of df = bcr.load_dataset('covid19_tutorial')
load, and if we are not over the wall, it is unable to load the data, the data I put on here for everyone to practice: Click to download training data ;
3. Function explanation
The third-party module bar_chart_race has only two functions to generate our dynamic graph,
- Dynamic bar graph:;
bar_chart_race.bar_chart_race()
The following explains in detail all the parameters in this function: - Dynamic line graph
bar_chart_race.line_chart_race()
:; similar to the bar graph above;
import bar_chart_race as bcr
import pandas
df=pd.read_csv('数据.csv',index_col=0) # 下载数据后,放在.py文件所在文件夹;
# 以下有 # 注释的参数,是我实际使用中发现并没有的;可是官网资料上有这些参数,不知道为什么~~
bcr.bar_chart_race(
df=df, # 第一个参数就是数据,这个数据格式必须是 pandas.DataFrame 格式,同时满足数据准备中所说的条件;
filename='bar_chart.mp4', # 这个参数是生成文件的名字,一般为.mp4 & .gif;
orientation='h', # 方向
sort='desc', # 排序
n_bars=6, # 限制条形图数量
fixed_order=False, # 固定标签
fixed_max=True, # 固定轴的最大值
steps_per_period=10, # 帧数设置
interpolate_period=False, # 插入时间
# label_bars=True, # 是否有label
bar_size=.95, # 设置bar宽度 取值 0~1 之间;
period_label={
'x': .99, 'y': .25, 'ha': 'right', 'va': 'center'},
# period_fmt='%B %d, %Y', # 日期的格式设置
period_summary_func=lambda v, r: {
'x': .99, 'y': .18,
's': r'Total weigth: {v.sum():,.0f}',
'ha': 'right', 'size': 8, 'family': 'Courier New'},
perpendicular_bar_func='median',
period_length=500,
# figsize=(5, 3),
# dpi=144,
# cmap='dark12',
title='COVID-19 Deaths by Country',
# title_size='',
# bar_label_size=7,
# tick_label_size=7,
shared_fontdict={
'family' : 'DejaVu Sans', 'color' : '.1'},
scale='linear',
writer=None,
fig=None,
bar_kwargs={
'alpha': .7},
filter_column_colors=False)
4. Detailed examples (copy and use)
4.1 Basic bar chart
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,filename='动态条形图.mp4')
The effect is as follows:
4.2 Histogram
Using orientation
parameters of the conversion pattern; there orientation
are two values may be assigned h
and v
;
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态柱状图.mp4',orientation='v')
The effect is as follows:
4.3 Sorting diagram
By default, it is sorted in descending order. When we need to sort in ascending order, use the parameter:sort='asc'
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态柱状图_升序.mp4',sort='asc')
The effect is as follows:
4.4 Limit the number of bars
By default, all columns will be displayed; if we need to control the number, use the parameter:, n_bars=6
where the number 6 can be changed to any integer;
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态柱状图_5条形图.mp4',n_bars=5)
The effect is as follows:
4.5 Order of fixed bars
By default, the bar graph will be sorted in descending order. If we want to fix the order, use the parameter:, fixed_order
change it to True or assign a list;
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态柱状图_条形固定.mp4',fixed_order=['Apple', 'banana', 'watermelon', 'orange'])
The effect is as follows:
4.6 Maximum fixed axis
By default, the maximum value according to the time axis conversion, if desired fixed parameters: fixed_max=True
.
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态条形图_固定轴的最大值.mp4',fixed_max=True)
The effect is as follows:
4.7 Set the number of frames
By default, the number of frames is 10, we can increase this value to make the image smoother; of course, it can also be reduced;
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态条形图_设置帧数.mp4',steps_per_period=24)
The effect is as follows:
4.8 Setting the step length and dwell time
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态条形图_设置步长与停留时间.mp4',steps_per_period=20, period_length=200)
The effect is as follows:
4.9 Add insertion time
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,'动态条形图_插入时间.mp4',interpolate_period=True)
4.10 Set font properties
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,filename='动态条形图_设置字体属性.mp4',shared_fontdict={
'family': 'Helvetica', 'weight': 'bold','color': 'rebeccapurple'})
The effect is as follows:
4.11 Customize bar attributes
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,filename='动态条形图_定制条形属性.mp4',bar_kwargs={
'alpha': .2, 'ec': 'black', 'lw': 3})
The effect is as follows:
4.12 Add text description
This, the research has not been successful yet, update later~
4.13 Increase the average vertical bar
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,filename='动态条形图_增加垂直bar平均值.mp4',perpendicular_bar_func='mean')
The effect is as follows:
4.14 Custom vertical bar-maximum
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
# 自定义增加垂直bar
def func(values, ranks):
return values.max()
bcr.bar_chart_race(df,filename='动态条形图_增加垂直bar自定义最大值.mp4', perpendicular_bar_func=func)
The effect is as follows:
4.15 Bar graph colors are not repeated
import bar_chart_race as bcr
import pandas as pd
df=pd.read_csv('数据.csv',index_col=0)
bcr.bar_chart_race(df,filename='动态条形图_条形图颜色不重复.mp4',filter_column_colors=True)
The effect is as follows:
5. Problems encountered
5.1 [error] AttributeError: module ‘sip‘ has no attribute ‘setapi‘
Problem description: Import import bar_chart_race error report I
recently bar_chart_race
encountered an error when importing the package: AttributeError: module'sip' has no attribute'setapi'
Imported package:
import bar_chart_race as bcr
Just run the imported statement, it will report an error: AttributeError: module'sip' has no attribute'setapi'
Solution:
Finally, I found that the reason for this error was that the version of the matplotlib library was too high. The version I reported was 3.3.2. There is no problem if I downgrade it to 3.3.0;
Uninstall first:pip uninstall matplotlib
Install again:pip install matplotlib==3.3.0
problem solved! !
5.2 [Report Error] IndexError: list index out of range
Problem description: When the output is in .gif format (if it is in .mp4 format, no error will be reported), an error will be reported during runtime:IndexError: list index out of range
Solution: Download : download https://imagemagick.org/script/download.php
the software at the site, just install it, but this URL is slow to download, you can also click here to download ( click to download ); Download and install, remember to check these two
items when installing ; after the installation is complete, the problem is solved ! !
5.3 [Report Error] TypeError: bar_chart_race() got an unexpected keyword argument 'figsize'
Problem description: When configuring according to the example on the official website, it is found that some parameters are not in the actual function, listed as follows:
label_bars=True, # 是否有label
period_fmt='%B %d, %Y', # 日期的格式设置
figsize=(5, 3),
dpi=144,
cmap='dark12',
title_size='',
bar_label_size=7,
tick_label_size=7,
The parameters listed above are not included in the function, and I don’t know why~ Because the article about this third-party package, everyone is an example of the official website of the translation, and no one actually verified it, so this question, if you have You know the reason~ Welcome everyone to tell me~ Thanks! !