大家好!今天给你们带来了使用 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")