Article directory
foreword
We have learned to use pyecharts
the modules in the package and the corresponding methods to draw line charts and maps, so today I will share with you how to draw a histogram with a timeline.
How to draw a histogram
The steps to draw a column chart are roughly the same as those for a line chart.
Drawing histograms in python depends on the methods pyecharts.charts
under the module Bar
.
from pyecharts.charts import Bar
bar = Bar()
x_data = ["英国","美国","中国"]
y_data = [10,20,30]
bar.add_xaxis(x_data)
bar.add_yaxis("GDP",y_data)
bar.render("柱状图.html")
We can also reverse the abscissa and ordinate to make the display of the data more vivid.
Use bar.reversal_axis()
to invert the x and y axes.
Here the data is displayed in the histogram, which is not very convenient for us to see. I can also set the series configuration options to make the data displayed on the right side of the histogram.
bar.add_yaxis("GDP",y_data,label_opts=LabelOpts(position="right"))
So our improved overall code is:
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
bar = Bar()
x_data = ["英国","美国","中国"]
y_data = [10,20,30]
bar.add_xaxis(x_data)
bar.add_yaxis("GDP",y_data,label_opts=LabelOpts(position="right"))
bar.reversal_axis() # 反转x轴和y轴
bar.render("柱状图.html")
add timeline
By adding a timeline, we can see a variety of different data. Each time is actually a histogram, and the timeline is composed of histograms one by one.
from pyecharts.charts import Bar,Timeline
from pyecharts.options import LabelOpts,TitleOpts
bar1 = Bar()
bar2 = Bar()
bar3 = Bar()
x_data = ["英国","美国","中国"]
y_data1 = [10,20,30]
y_data2 = [20,30,40]
y_data3 = [40,50,70]
bar1.add_xaxis(x_data)
bar1.add_yaxis("GDP",y_data1,label_opts=LabelOpts(position="right"))
bar1.reversal_axis() # 反转x轴和y轴
bar1.set_global_opts(title_opts=TitleOpts(title="2021年GDP"))
bar2.add_xaxis(x_data)
bar2.add_yaxis("GDP",y_data2,label_opts=LabelOpts(position="right"))
bar2.reversal_axis() # 反转x轴和y轴
bar2.set_global_opts(title_opts=TitleOpts(title="2022年GDP"))
bar3.add_xaxis(x_data)
bar3.add_yaxis("GDP",y_data3,label_opts=LabelOpts(position="right"))
bar3.reversal_axis() # 反转x轴和y轴
bar3.set_global_opts(title_opts=TitleOpts(title="2023年GDP"))
timeline = Timeline()
timeline.add(bar1,"2021")
timeline.add(bar2,"2022")
timeline.add(bar3,"2023")
timeline.render("2021-2023中美英三国GDP.html")
If we want to animate the dynamic histogram, we need to set configuration options.
timeline.add_schema(
play_interval=1000, # 每个柱状图播放间隔时间,单位(毫秒)
is_timeline_show=True, # 是否显示时间线,默认显示
is_auto_play=True, # 是否自动播放
is_loop_play=True # 是否循环播放
)
Draws a dynamic histogram based on the provided data
We show the top eight countries and data for national GDP data from 1960 to 2014. You can private message me for the data provided here.
The data provided here is relatively simple, we only need to delete the useless data in the first row, and then convert these data into the data we need to draw the histogram.
Read and delete useless data
f = open("D:/桌面/1960-2019全球GDP数据.csv","r",encoding="GB2312")
data_lines = f.readlines()
f.close()
data_lines.pop(0)
GB2312 encoding is the Chinese encoding format
convert the data into a dictionary
data_dict = {
}
for line in data_lines:
data_list = line.split(",") # 每一行以逗号分割,返回一个列表
year = data_list[0]
country = data_list[1]
GDP = float(data_list[2][:-1]) # 每一行最后有一个换行符
# 这里需要做出异常判断,因为当我们第一次插入数据的时候并没有容器来装这些数据
try:
data_dict[year].append((country, GDP))
except:
data_dict[year] = []
data_dict[year].append([country, GDP])
Create a histogram and add it to the timeline
sorted_year_line = sorted(data_dict.keys()) # 按时间顺序排序
timeline = Timeline({
"scheme":ThemeType.LIGHT}) # 在创建时间线的时候传入scheme参数可以设置时间线的主题,也就是柱状图的颜色
for year in sorted_year_line:
x_data = []
y_data = []
data_dict[year].sort(key=lambda element : element[1],reverse=True)
year_data = data_dict[year][0:8] # 取GDP前八的数据
for data in year_data:
x_data.append(data[0])
y_data.append(data[1] / 100000000)
bar = Bar()
x_data.reverse()
y_data.reverse() # 让GDP排名第一的数据在最上面,所以我们将x_data 和 y_data中的数据反转一下
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(亿)",y_data,label_opts=LabelOpts(position="right"))
bar.reversal_axis() # 将x轴和y轴翻转
bar.set_global_opts(
title_opts=TitleOpts(title=f"{
year}年全国GDP数据前八")
)
timeline.add(bar,year)
Configure options and generate a line chart with data
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
timeline.render("1960-2014年全国GDP数据前八.html")