当前位置: 欣欣网 > 码农

Python制作9种超有趣图表

2024-02-25码农

大家好!今天给你们带来了使用 pyecharts库制作常见的9种图表简单实例,帮我们更好地学习Python可视化。

安装pyecharts:

pip install pyecharts

1.折线图

from pyecharts import options as optsfrom 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="折线图")))#生成htmlline.render("line.html")

2.柱状图

from pyecharts import options as optsfrom 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 optsfrom 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}")))#生成 HTMLpie.render("pie.html")

4.散点图

from pyecharts import options as optsfrom 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="散点图")))#生成 HTMLscatter.render("scatter.html")

5.地图

from pyecharts import options as optsfrom pyecharts.charts import Map#创建 Map 图表map_chart = ( Map() .add("Series 1", [('北京', 100), ('上海', 200), ('广州', 300)], "china") .set_global_opts(title_opts=opts.TitleOpts(title="地图")))#生成 HTMLmap_chart.render("map.html")

6.仪表图

from pyecharts import options as optsfrom pyecharts.charts import Gauge#创建 Gauge 图表gauge = ( Gauge() .add("", [("Series 1", 66.6)]) .set_global_opts(title_opts=opts.TitleOpts(title="仪表盘")))#生成 HTMLgauge.render("gauge.html")

7.关系图

from pyecharts import options as optsfrom 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="水果关系图")))#生成 HTMLgraph.render("fruit_graph.html")

8.日历图

from pyecharts import options as optsfrom 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) #设置最大值 ))#生成 HTMLcalendar.render("calendar.html")

9.液体图

from pyecharts import options as optsfrom pyecharts.charts import Liquid#创建 Liquid 图表liquid = ( Liquid() .add("Series 1", [0.6]) .set_global_opts(title_opts=opts.TitleOpts(title="液体图")))#生成 HTMLliquid.render("liquid.html")