大家好!今天給你們帶來了使用 pyecharts庫制作常見的9種圖表簡單例項,幫我們更好地學習Python視覺化。
安裝pyecharts:
pip install pyecharts
1.折線圖
from pyecharts import options as opts
from pyecharts.charts import Line
#建立 Line 圖表
line = (
Line()
.add_xaxis(['2020', '2021', '2022', '2023','2024'])
.add_yaxis('Series 1', [1, 3, 2, 4, 5])
.set_global_opts(title_opts=opts.TitleOpts(title="折線圖"))
)
#生成html
line.render("line.html")
2.柱狀圖
from pyecharts import options as opts
from pyecharts.charts import Bar
#建立 Bar 圖表
bar = (
Bar()
.add_xaxis(['A', 'B', 'C', 'D', 'E'])
.add_yaxis('Series 1', [5, 9, 3, 7, 2])
.set_global_opts(title_opts=opts.TitleOpts(title="柱狀圖"))
)
#生成HTML
bar.render("bar.html")
3.餅圖
from pyecharts import options as opts
from pyecharts.charts import Pie
#建立 Pie 圖表
pie = (
Pie()
.add("", [('A', 10), ('B', 20), ('C', 30), ('D', 40), ('E', 50)])
.set_global_opts(title_opts=opts.TitleOpts(title="餅圖"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
#生成 HTML
pie.render("pie.html")
4.散點圖
from pyecharts import options as opts
from pyecharts.charts import Scatter
#建立 Scatter 圖表
scatter = (
Scatter()
.add_xaxis([1, 2, 3, 4, 5])
.add_yaxis('Series 1', [5, 9, 3, 7, 2])
.set_global_opts(title_opts=opts.TitleOpts(title="散點圖"))
)
#生成 HTML
scatter.render("scatter.html")
5.地圖
from pyecharts import options as opts
from pyecharts.charts import Map
#建立 Map 圖表
map_chart = (
Map()
.add("Series 1", [('北京', 100), ('上海', 200), ('廣州', 300)], "china")
.set_global_opts(title_opts=opts.TitleOpts(title="地圖"))
)
#生成 HTML
map_chart.render("map.html")
6.儀表圖
from pyecharts import options as opts
from pyecharts.charts import Gauge
#建立 Gauge 圖表
gauge = (
Gauge()
.add("", [("Series 1", 66.6)])
.set_global_opts(title_opts=opts.TitleOpts(title="儀表盤"))
)
#生成 HTML
gauge.render("gauge.html")
7.關系圖
from pyecharts import options as opts
from pyecharts.charts import Graph
#建立 Graph 圖表
nodes = [
{"name": "蘋果", "symbol": "circle", "symbol_size": 60},
{"name": "香蕉", "symbol": "circle", "symbol_size": 60},
{"name": "橙子", "symbol": "circle", "symbol_size": 60},
{"name": "葡萄", "symbol": "circle", "symbol_size": 60},
{"name": "桃子", "symbol": "circle", "symbol_size": 60}
]
links = [{"source": "蘋果", "target": "香蕉"}, {"source": "香蕉", "target": "橙子"}, {"source": "橙子", "target": "葡萄"}, {"source": "葡萄", "target": "桃子"}]
graph = (
Graph()
.add("", nodes, links)
.set_global_opts(title_opts=opts.TitleOpts(title="水果關系圖"))
)
#生成 HTML
graph.render("fruit_graph.html")
8.行事曆圖
from pyecharts import options as opts
from pyecharts.charts import Calendar
#建立 Calendar 圖表
data = [
("2022-01-01", 1),
("2022-02-14", 1),
("2022-03-08", 2),
("2022-04-01", 1)
]
calendar = (
Calendar(init_opts=opts.InitOpts(width="1000px", height="600px"))
.add("", data, calendar_opts=opts.CalendarOpts(range_="2022"))
.set_global_opts(
title_opts=opts.TitleOpts(title="行事曆圖"),
visualmap_opts=opts.VisualMapOpts(max_=2) #設定最大值
)
)
#生成 HTML
calendar.render("calendar.html")
9.液體圖
from pyecharts import options as opts
from pyecharts.charts import Liquid
#建立 Liquid 圖表
liquid = (
Liquid()
.add("Series 1", [0.6])
.set_global_opts(title_opts=opts.TitleOpts(title="液體圖"))
)
#生成 HTML
liquid.render("liquid.html")