基礎(chǔ)柱狀圖
通過Bar構(gòu)建基礎(chǔ)柱狀圖
from pyecharts.charts import Bar
from pyecharts.options import LabelOpts
# 使用Bar構(gòu)建基礎(chǔ)柱狀圖
bar = Bar()
# 添加X軸
bar.add_xaxis(["中國", "美國", "英國"])
# 添加Y軸 # 設(shè)置數(shù)值標(biāo)簽在右側(cè)
bar.add_yaxis("GDP", [30, 50, 40], label_opts=LabelOpts(position="right"))
# 反轉(zhuǎn)X軸和Y軸
bar.reversal_axis()
# 繪圖
bar.render("基礎(chǔ)柱狀圖.html")
1. 通過Bar()構(gòu)建一個柱狀圖對象
2. 和折線圖一樣,通過add_xaxis()和add_yaxis()添加x和y軸數(shù)據(jù)
3. 通過柱狀圖對象的:reversal_axis(),反轉(zhuǎn)x和y軸
4. 通過label_opts=LabelOpts(position="right")設(shè)置數(shù)值標(biāo)簽在右側(cè)顯示
基礎(chǔ)時間線柱狀圖
創(chuàng)建時間線
Timeline()-時間線
柱狀圖描述的是分類數(shù)據(jù),回答的是每一個分類中『有多少?』這個問題. 這是柱狀圖的主要特點,同時柱狀圖很難動態(tài)的描述一個趨勢性的數(shù)據(jù). 這里pyecharts為我們提供了一種解決方案-時間線
如果說一個Bar、Line對象是一張圖表的話,時間線就是創(chuàng)建一個 一維的x軸,軸上每一個點就是一個圖表對象
# 導(dǎo)入bar柱狀圖 Timeline時間線
from pyecharts.charts import Bar, Timeline
# 導(dǎo)入系統(tǒng)配置項
from pyecharts.options import LabelOpts
# 導(dǎo)入ThemeType主題類型
from pyecharts.globals import ThemeType
# 使用Bar構(gòu)建基礎(chǔ)柱狀圖
bar1 = Bar()
# 添加X軸
bar1.add_xaxis(["中國", "美國", "英國"])
# 添加Y軸
bar1.add_yaxis("GDP", [30, 50, 40], label_opts=LabelOpts(position="right"))
bar2 = Bar()
bar2.add_xaxis(["中國", "美國", "英國"])
bar2.add_yaxis("GDP", [40, 40, 20], label_opts=LabelOpts(position="right"))
bar3 = Bar()
bar3.add_xaxis(["中國", "美國", "英國"])
bar3.add_yaxis("GDP", [50, 30, 30], label_opts=LabelOpts(position="right"))
bar4 = Bar()
bar4.add_xaxis(["中國", "美國", "英國"])
bar4.add_yaxis("GDP", [60, 20, 50], label_opts=LabelOpts(position="right"))
# 構(gòu)建時間線對象
timeline = Timeline({"theme": ThemeType.LIGHT}) # 主題設(shè)置
# 在時間線內(nèi)部添加柱狀圖對象
timeline.add(bar1, "點1")
timeline.add(bar2, "點2")
timeline.add(bar3, "點3")
timeline.add(bar4, "點4")
自動播放?
timeline.add_schema(
play_interval=1000, # 自動播放的時間間隔,單位毫秒
is_timeline_show=True, # 是否在自動播放的時候顯示時間線
is_auto_play=True, # 是否自動播放
is_loop_play=True # 是否循環(huán)自動播放
)
?時間線設(shè)置主題
from pyecharts.globals import ThemeType
timeline = Timeline({"theme": ThemeType.LIGHT}) # 主題設(shè)置
?完整代碼
"""
基礎(chǔ)時間線柱狀圖
"""
# 導(dǎo)入bar柱狀圖 Timeline時間線
from pyecharts.charts import Bar, Timeline
# 導(dǎo)入系統(tǒng)配置項
from pyecharts.options import LabelOpts
# 導(dǎo)入ThemeType主題類型
from pyecharts.globals import ThemeType
# 使用Bar構(gòu)建基礎(chǔ)柱狀圖
bar1 = Bar()
# 添加X軸
bar1.add_xaxis(["中國", "美國", "英國"])
# 添加Y軸
bar1.add_yaxis("GDP", [30, 50, 40], label_opts=LabelOpts(position="right"))
bar2 = Bar()
bar2.add_xaxis(["中國", "美國", "英國"])
bar2.add_yaxis("GDP", [40, 40, 20], label_opts=LabelOpts(position="right"))
bar3 = Bar()
bar3.add_xaxis(["中國", "美國", "英國"])
bar3.add_yaxis("GDP", [50, 30, 30], label_opts=LabelOpts(position="right"))
bar4 = Bar()
bar4.add_xaxis(["中國", "美國", "英國"])
bar4.add_yaxis("GDP", [60, 20, 50], label_opts=LabelOpts(position="right"))
# 構(gòu)建時間線對象
timeline = Timeline({"theme": ThemeType.LIGHT}) # 主題設(shè)置
# 在時間線內(nèi)部添加柱狀圖對象
timeline.add(bar1, "點1")
timeline.add(bar2, "點2")
timeline.add(bar3, "點3")
timeline.add(bar4, "點4")
# 自動播放設(shè)置
timeline.add_schema(
play_interval=1000, # 自動播放的時間間隔,單位毫秒
is_timeline_show=True, # 是否在自動播放的時候顯示時間線
is_auto_play=True, # 是否自動播放
is_loop_play=True # 是否循環(huán)自動播放
)
# 繪圖
timeline.render("基礎(chǔ)時間線柱狀圖.html")
GDP動態(tài)柱狀圖繪制
需求分析
簡單分析后,發(fā)現(xiàn)最終效果圖中需要:
1. GDP數(shù)據(jù)處理為億級
2. 有時間軸,按照年份為時間軸的點
3. x軸和y軸反轉(zhuǎn),同時每一年的數(shù)據(jù)只要前8名國家
4. 有標(biāo)題,標(biāo)題的年份會動態(tài)更改
5. 設(shè)置了主題為LIGHT
代碼
"""
演示第三個圖表:GDP動態(tài)柱狀圖開發(fā)
"""
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 讀取數(shù)據(jù)
f = open("D:/1960-2019全球GDP數(shù)據(jù).csv", "r", encoding="GB2312")
data_lines = f.readlines()
# 關(guān)閉文件
f.close()
# 刪除第一條數(shù)據(jù)
data_lines.pop(0)
# 將數(shù)據(jù)轉(zhuǎn)換為字典存儲,格式為:
# { 年份: [ [國家, gdp], [國家,gdp], ...... ], 年份: [ [國家, gdp], [國家,gdp], ...... ], ...... }
# { 1960: [ [美國, 123], [中國,321], ...... ], 1961: [ [美國, 123], [中國,321], ...... ], ...... }
# 先定義一個字典對象
data_dict = {}
for line in data_lines:
year = int(line.split(",")[0]) # 年份
country = line.split(",")[1] # 國家
gdp = float(line.split(",")[2]) # gdp數(shù)據(jù)
# 如何判斷字典里面有沒有指定的key呢?
try:
data_dict[year].append([country, gdp])
except KeyError:
data_dict[year] = []
data_dict[year].append([country, gdp])
# print(data_dict[1960])
# 創(chuàng)建時間線對象
timeline = Timeline({"theme": ThemeType.LIGHT})
# 排序年份
sorted_year_list = sorted(data_dict.keys())
for year in sorted_year_list:
data_dict[year].sort(key=lambda element: element[1], reverse=True)
# 取出本年份前8名的國家
year_data = data_dict[year][0:8]
x_data = []
y_data = []
for country_gdp in year_data:
x_data.append(country_gdp[0]) # x軸添加國家
y_data.append(country_gdp[1] / 100000000) # y軸添加gdp數(shù)據(jù)
# 構(gòu)建柱狀圖
bar = Bar()
x_data.reverse()
y_data.reverse()
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(億)", y_data, label_opts=LabelOpts(position="right"))
# 反轉(zhuǎn)x軸和y軸
bar.reversal_axis()
# 設(shè)置每一年的圖表的標(biāo)題
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年全球前8GDP數(shù)據(jù)")
)
timeline.add(bar, str(year))
# for循環(huán)每一年的數(shù)據(jù),基于每一年的數(shù)據(jù),創(chuàng)建每一年的bar對象
# 在for中,將每一年的bar對象添加到時間線中
# 設(shè)置時間線自動播放
timeline.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=False
)
# 繪圖
timeline.render("1960-2019全球GDP前8國家.html")
文章來源:http://www.zghlxwxcb.cn/news/detail-607467.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-607467.html
到了這里,關(guān)于數(shù)據(jù)可視化 - 動態(tài)柱狀圖的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!