一 基礎(chǔ)柱狀圖
1.1 創(chuàng)建簡單柱狀圖
-
pyecharts
是一個基于 Echarts 的 Python 圖表庫,它提供了豐富的圖表類型和交互功能。可以使用使用pyecharts
創(chuàng)建柱狀圖
首先,安裝 pyecharts
庫。如果沒有安裝,可以使用以下命令安裝:
pip install pyecharts
然后,創(chuàng)建一個簡單的柱狀圖:
from pyecharts import options as opts
from pyecharts.charts import Bar
# 示例數(shù)據(jù)
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 40, 30, 50]
# 創(chuàng)建柱狀圖實例
bar_chart = Bar()
# 添加數(shù)據(jù)
bar_chart.add_xaxis(categories)
bar_chart.add_yaxis("Values", values)
# 設(shè)置標題和標簽
bar_chart.set_global_opts(title_opts=opts.TitleOpts(title="Basic Bar Chart"),
xaxis_opts=opts.AxisOpts(name="Categories"),
yaxis_opts=opts.AxisOpts(name="Values"))
# 渲染圖表
bar_chart.render("basic_bar_chart.html")
- 在這個示例中,首先導(dǎo)入
pyecharts
的必要模塊。然后,創(chuàng)建一個Bar
實例,代表柱狀圖。接著,使用add_xaxis
和add_yaxis
方法分別添加 x 軸和 y 軸的數(shù)據(jù)。還使用set_global_opts
方法設(shè)置了標題和軸標簽的選項。最后,使用render
方法將圖表渲染為 HTML 文件。
1.2 反轉(zhuǎn)x和y軸
- 可以使用
pyecharts
的 Bar 類中的reversal_axis()
方法翻轉(zhuǎn) x 軸和 y 軸
from pyecharts import options as opts
from pyecharts.charts import Bar
# 示例數(shù)據(jù)
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 40, 30, 50]
# 創(chuàng)建柱狀圖實例
bar_chart = Bar()
# 添加數(shù)據(jù)
bar_chart.add_xaxis(categories)
bar_chart.add_yaxis("Values", values)
# 設(shè)置標題和標簽
bar_chart.set_global_opts(title_opts=opts.TitleOpts(title="Basic Bar Chart"),
xaxis_opts=opts.AxisOpts(name="Categories"),
yaxis_opts=opts.AxisOpts(name="Values"))
# 反轉(zhuǎn)x和y軸
bar_chart.reversal_axis()
# 渲染圖表
bar_chart.render("basic_bar_chart.html")
1.3 數(shù)值標簽在右側(cè)
- 通過label_opts=LabelOpts(position=“right”)設(shè)置數(shù)值標簽在右側(cè)顯示
from pyecharts import options as opts
from pyecharts.charts import Bar
# 示例數(shù)據(jù)
categories = ['Category A', 'Category B', 'Category C', 'Category D']
values = [25, 40, 30, 50]
# 創(chuàng)建柱狀圖實例
bar_chart = Bar()
# 添加數(shù)據(jù)
bar_chart.add_xaxis(categories)
bar_chart.add_yaxis("Values", values, label_opts=opts.LabelOpts(position="right"))
# 設(shè)置標題和標簽
bar_chart.set_global_opts(title_opts=opts.TitleOpts(title="Basic Bar Chart"),
xaxis_opts=opts.AxisOpts(name="Categories"),
yaxis_opts=opts.AxisOpts(name="Values"))
# 反轉(zhuǎn)x和y軸
bar_chart.reversal_axis()
# 渲染圖表
bar_chart.render("basic_bar_chart.html")
1.4 演示結(jié)果
二 基礎(chǔ)時間線柱狀圖
2.1 創(chuàng)建時間線
- Timeline()-時間線
- 柱狀圖的主要特點,:狀圖描述的是分類數(shù)據(jù),回答的是每一個分類中有多少的問題。同時柱狀圖很難動態(tài)的描述一個趨勢性的數(shù)據(jù). 這里pyecharts為我們提供了一種解決方案-時間線
- 如果說一個Bar、Line對象是一張圖表的話,時間線就是創(chuàng)建一個一維的x軸,軸上每一個點就是一個圖表對象
from pyecharts.charts import Bar, Timeline
from pyecharts.options import LabelOpts
from pyecharts.globals import ThemeType
bar1 = Bar()
bar1.add_xaxis(["中國", "美國", "英國"])
bar1.add_yaxis("GDP", [30, 30, 20], label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2 = Bar()
bar2.add_xaxis(["中國", "美國", "英國"])
bar2.add_yaxis("GDP", [50, 50, 50], label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
bar3 = Bar()
bar3.add_xaxis(["中國", "美國", "英國"])
bar3.add_yaxis("GDP", [70, 60, 60], label_opts=LabelOpts(position="right"))
bar3.reversal_axis()
# 構(gòu)建時間線對象
# timeline = Timeline()
timeline = Timeline({"theme": ThemeType.LIGHT}) #設(shè)置時間線對象
# 在時間線內(nèi)添加柱狀圖對象
timeline.add(bar1, "點1")
timeline.add(bar2, "點2")
timeline.add(bar3, "點3")
# 自動播放設(shè)置
timeline.add_schema(
play_interval=1000, # 自動播放的時間間隔,單位毫秒
is_timeline_show=True, # 是否在自動播放的時候,顯示時間線
is_auto_play=True, # 是否自動播放
is_loop_play=True # 是否循環(huán)播放
)
# 繪圖是用時間線對象繪圖,而不是bar對象了
timeline.render("基礎(chǔ)時間線柱狀圖.html")
2.2 時間線主題設(shè)置取值表
2.3 演示結(jié)果
三 GDP動態(tài)柱狀圖繪制
3.1 需求分析
簡單分析后,發(fā)現(xiàn)最終效果圖中需要:
- GDP數(shù)據(jù)處理為億級
- 有時間軸,按照年份為時間軸的點
- x軸和y軸反轉(zhuǎn),同時每一年的數(shù)據(jù)只要前8名國家
- 有標題,標題的年份會動態(tài)更改
- 設(shè)置了主題為LIGHT
3.2 數(shù)據(jù)文件內(nèi)容
year,GDP,rate
1960,美國,5.433E+11
1960,英國,73233967692
1960,法國,62225478000
1960,中國,59716467625
1960,日本,44307342950
1960,加拿大,40461721692
3.3 列表排序方法
列表.sort(key=選擇排序依據(jù)的函數(shù), reverse=True|False)
文章來源:http://www.zghlxwxcb.cn/news/detail-650448.html
- 參數(shù)key,是要求傳入一個函數(shù),表示將列表的每一個元素都傳入函數(shù)中,返回排序的依據(jù)
- 參數(shù)reverse,是否反轉(zhuǎn)排序結(jié)果,True表示降序,F(xiàn)alse表示升序
my_list = [["a", 33], ["b", 55], ["c", 11]]
# 排序,基于帶名函數(shù)
# def choose_sort_key(element):
# return element[1]
#
# my_list.sort(key=choose_sort_key, reverse=False)
# 使用 sort() 方法,按子列表中的第二個元素排序
my_list.sort(key=lambda x: x[1])
print("升序排序:", my_list)
# 升序排序: [['c', 11], ['a', 33], ['b', 55]]
my_list.sort(key=lambda x: x[1], reverse=True)
print("降序排序:", my_list)
# 降序排序: [['b', 55], ['a', 33], ['c', 11]]
-
key=lambda x: x[1]
Lambda 表達式是一個用于比較排序的函數(shù),它告訴 sort() 方法按照子列表中的第二個元素進行排序
3.4 參考代碼
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
# 讀取數(shù)據(jù)
f = open("c:/1960-2019全球GDP數(shù)據(jù).csv", "r", encoding="GB2312")
data_lines = f.readlines()
# 關(guān)閉文件
f.close()
# 刪除第一條數(shù)據(jù)
data_lines.pop(0) # year,GDP,rate
# 將數(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])
# 創(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 = [] #countrys
y_data = [] #gdps
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() # 創(chuàng)建柱狀圖
x_data.reverse() # 反轉(zhuǎn)國家,使GDP最高的排在最上面
y_data.reverse() # 同步反轉(zhuǎn)GDP數(shù)據(jù)
bar.add_xaxis(x_data)
bar.add_yaxis("GDP(億)", y_data, label_opts=LabelOpts(position="right"))
# 反轉(zhuǎn)x軸和y軸
bar.reversal_axis()
# 設(shè)置每一年的圖表的標題
bar.set_global_opts(
title_opts=TitleOpts(title=f"{year}年全球前8GDP數(shù)據(jù)")
)
timeline.add(bar, str(year)) #時間線添加一個點,和對應(yīng)的bar圖
# 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")
3.5 運行結(jié)果
文章來源地址http://www.zghlxwxcb.cn/news/detail-650448.html
到了這里,關(guān)于探索數(shù)據(jù)之美:初步學習 Python 柱狀圖繪制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!