什么是pyecharts
Pyecharts是一个基于Python的开源数据可视化库,它旨在让数据可视化变得更加简单、易用和高效。Pyecharts是Echarts的Python封装,Echarts是一个由百度开发的开源数据可视化库,提供了丰富的图表类型和交互功能。通过Pyecharts,用户可以使用简单的Python代码创建各种交互式图表和地图,包括柱状图、折线图、散点图、饼图、地图等。此外,Pyecharts还提供了地图可视化功能,允许用户通过Python代码调用生成动态网页来展示数据,特别是在数据地图的制作方面,Pyecharts展现出了其独特的优势和功能。
官网:pyecharts - A Python Echarts Plotting Library built with love.
导入pyecharts库
如果没有pyechats库先下载:
下载pyecharts库
pip install pyecharts
导入pyecharts库
import pyecharts.options as opt
from pyecharts.charts import *
柱状图,折线图,饼图
柱状图
柱状图 —— bar
基础柱状图
x = ["a","b","c","d","e"] #x轴
y1 = [120,258,180,90,184] #y1值
y2 = [150,280,54,187,124] #y2值
bar = Bar()
bar.add_xaxis(x) #定义x轴值
bar.add_yaxis("y1",y1) #图例y1 值为y1
bar.add_yaxis("y2",y2) #图例y2 值为y2
bar.set_global_opts(title_opts=opt.TitleOpts(title="柱状图"),
xaxis_opts=opt.AxisOpts(
axislabel_opts=opt.LabelOpts(color="#fff")), #x轴标签颜色白色
legend_opts=opt.LegendOpts(
textstyle_opts=opt.TextStyleOpts(color="#fff"))) #图例字体白色
bar.render_notebook() #显示柱状图
#bar.render("路径") 返回HTML文件保存本地
结果:
堆叠柱状图
x = ["a","b","c","d","e"] #x轴
y1 = [120,258,180,90,184] #y1值
y2 = [150,280,54,187,124] #y2值
bar = Bar()
bar.add_xaxis(x) #定义x轴值
# stack值相同的堆叠
bar.add_yaxis("y1",y1,stack="1") #图例y1 值为y1
bar.add_yaxis("y2",y2,stack='1') #图例y2 值为y2
bar.set_global_opts(title_opts=opt.TitleOpts(title="柱状图"),
xaxis_opts=opt.AxisOpts(
axislabel_opts=opt.LabelOpts(color="#fff")), #x轴标签颜色白色
legend_opts=opt.LegendOpts(
textstyle_opts=opt.TextStyleOpts(color="#fff"))) #图例字体白色
bar.render_notebook() #显示柱状图
#bar.render("路径") 返回HTML文件保存本地
结果:
折线图
折线图——line
基础折线图
x = ["2023-01-01","2023-01-02","2023-01-03","2023-01-04","2023-01-05"]
y = [15,24,18,40,22]
line = Line()
line.add_xaxis(x)
line.add_yaxis("f1",y)
line.set_global_opts(title_opts=opt.TitleOpts(title="基础折线图"),
legend_opts=opt.LegendOpts(textstyle_opts=opt.TextStyleOpts(color="#fff")))
line.render_notebook()
堆叠折线图
x = ["2023-01-01","2023-01-02","2023-01-03","2023-01-04","2023-01-05"]
y1 = [15,24,18,40,22]
y2 = [24,18,47,35,19]
line = Line()
line.add_xaxis(x)
line.add_yaxis("f1",y1,stack="1")
line.add_yaxis("f2",y2,stack="1")
line.set_global_opts(title_opts=opt.TitleOpts(title="折线图"),
legend_opts=opt.LegendOpts(textstyle_opts=opt.TextStyleOpts(color="#fff")))
line.render_notebook()
面积图
x = ["2023-01-01","2023-01-02","2023-01-03","2023-01-04","2023-01-05"]
y1 = [15,24,18,40,22]
line = Line()
line.add_xaxis(x)
line.add_yaxis("f1",y1)
line.set_global_opts(title_opts=opt.TitleOpts(title="面积图"),
legend_opts=opt.LegendOpts(textstyle_opts=opt.TextStyleOpts(color="#fff")))
line.set_series_opts(areastyle_opts=opt.AreaStyleOpts(color="red",opacity=0.7)) #面积颜色红色,透明度为0.7默认0
line.render_notebook()
结果
饼图
饼图——pie
饼图
data = [("a",12),("b",28),("c",17),("d",38)]
pie = Pie()
pie.add("a",data_pair=data)
pie.set_global_opts(title_opts=opt.TitleOpts(title="饼图",title_textstyle_opts=opt.TextStyleOpts(color="#fff")),#标题颜色
legend_opts=opt.LegendOpts(textstyle_opts=opt.TextStyleOpts(color="#fff")))#图例颜色
pie.set_series_opts(label_opts=opt.LabelOpts(formatter="{b}:{c}"))#显示标签:数值
pie.render_notebook()
结果
环图
data = [("a",12),("b",28),("c",17),("d",38)]
pie = Pie()
pie.add("a",data_pair=data,radius=["20%","55%"])
pie.set_global_opts(title_opts=opt.TitleOpts(title="环图",title_textstyle_opts=opt.TextStyleOpts(color="#fff")),#标题白色
legend_opts=opt.LegendOpts(textstyle_opts=opt.TextStyleOpts(color="#fff")))#图例显示白色
pie.set_series_opts(label_opts=opt.LabelOpts(formatter="{b}:{c}"))#显示标签:数值
pie.render_notebook()