pyecharts 是一款非常好用的,基于 python 的,数据可视化工具包,那么如何来使用 pyecharts ,下面就来简单介绍一下 pyecharts 的使用方法,具体安装过程可以参考
pyecharts 分为 v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容
v0.5.X版本 | v1 版本 |
---|---|
支持Python2.7,3.4+,且已不再维护 | 支持Python3.6 + 是目前的首选 |
1. 导入全局配置项和图表类型
from pyecharts import options as opts
from pyecharts.charts import [name]
2. 添加或导入数据
x_data = ["1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009"]
y_data = [0.32,0.32,0.32,0.32,0.33,0.33,0.34,0.37,0.37,0.37,0.37,0.39,0.41,0.42,0.44]
3. 初始化图表(以Line为例),配置全局设置 ,添加xy轴数据
Line()
.set_global_opts( title_opts=opts.TitleOpts(title="Line-折线图", subtitle="Chale"),)
.add_xaxis(xaxis_data = x_data)
.add_yaxis( y_axis = y_data)
4. 导出
.render("html/折线图.html")
render方法会在指定目录生成一个html文件,在浏览器打开可查看图例
下面有几个例题可以更详细的理解这个流程
"""
@desc:
@author: Chale
@software: PyCharm on 2020/6/5 9:54
"""
import pyecharts.options as opts
from pyecharts.charts import Line
#原始数据
x_data = ["1995","1996","1997","1998","1999","2000","2001","2002","2003","2004","2005","2006","2007","2008","2009"]
y_data = [0.32,0.32,0.32,0.32,0.33,0.33,0.34,0.37,0.37,0.37,0.37,0.39,0.41,0.42,0.44]
#折线图
(
#初始化
Line()
#全局配置
.set_global_opts(
tooltip_opts=opts.TooltipOpts(is_show=True),
xaxis_opts=opts.AxisOpts(type_="category"),
title_opts=opts.TitleOpts(title="Line-折线图", subtitle="Chale"),
yaxis_opts=opts.AxisOpts(
type_="value",
axistick_opts=opts.AxisTickOpts(is_show=True),
splitline_opts=opts.SplitLineOpts(is_show=True),
)
)
#xy轴配置
.add_xaxis(xaxis_data=x_data)
.add_yaxis(
series_name="美国1995年-2009年邮费折线图",
y_axis=y_data,
symbol="emptyCircle",
is_symbol_show=True,
label_opts=opts.LabelOpts(is_show=True),
)
#导出
.render("./html/basic_line_chart.html")
)
#阶梯图
c = (
Line()
.set_global_opts(title_opts=opts.TitleOpts(title="Line-阶梯图", subtitle="Chale"))
.add_xaxis(xaxis_data=x_data)
.add_yaxis("美国1995年-2009年邮费阶梯图", y_axis=y_data, is_step=True)
.render("./html/line_step.html")
)
2000年-2010年热狗大胃王比赛前三名成绩的堆叠柱形图、极坐标系-堆叠柱状图(南丁格尔玫瑰图):
数据文件:hot-dog-places.csv
可以通过 pandas 来导入 csv 文件
"""
@desc:
@author: Chale
@software: PyCharm on 2020/6/5 10:57
"""
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Polar
#导入csv文件
hot_dog = pd.read_csv('file/hot-dog-places.csv')
#xy轴赋值
x = hot_dog.columns.values.tolist()
y1 = hot_dog.values[0:1][0].tolist()
y2 = hot_dog.values[1:2][0].tolist()
y3 = hot_dog.values[2:3][0].tolist()
#堆叠柱形图
c = (
Bar()
#设置x轴
.add_xaxis(x)
#设置堆叠数据
.add_yaxis("Fst", y1, stack="stack1")
.add_yaxis("Sec", y2, stack="stack1")
.add_yaxis("Trd", y3, stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="Bar-堆叠柱形图", subtitle="Chale"))
.render("html/堆叠柱形图.html")
)
#玫瑰图
c = (
Polar()
.add_schema(angleaxis_opts=opts.AngleAxisOpts(data=x, type_="category"))
.add("A", y1, type_="bar", stack="stack0")
.add("B", y2, type_="bar", stack="stack0")
.add("C", y3, type_="bar", stack="stack0")
.set_global_opts(title_opts=opts.TitleOpts(title="Polar-AngleAxis", subtitle="Chale"))
.render("html/南丁格尔玫瑰图.html")
)
"""
@desc:
@author: Chale
@software: PyCharm on 2020/6/5 11:30
"""
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie
#导入csv文件
vote_result = pd.read_csv('file/vote_result.csv')
#xy轴赋值
x = vote_result['感兴趣的领域'].tolist()
y= vote_result['票数'].tolist()
c = (
Pie()
.add("", [list(z) for z in zip(x,y)])
#设置饼图各部分颜色
.set_colors(["#3FA7D6", "#F3CA40", "#F2A541", "#F3752B", "#9bf6ff", "#D78A76", "#0CCE6B"])
.set_global_opts(title_opts=opts.TitleOpts(title="投票结果", subtitle="Chale"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("html/饼图.html")
)
d = (
Pie()
.add(
"",
[list(z) for z in zip(x, y)],
#设置圆环宽度和大小
radius=["40%", "75%"],
)
.set_global_opts(
title_opts=opts.TitleOpts(title="环形饼图", subtitle="Chale"),
legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
)
.set_colors(["#3FA7D6", "#F3CA40", "#F2A541", "#F3752B", "#9bf6ff", "#D78A76", "#0CCE6B"])
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
.render("html/环形饼图.html")
)
"""
@desc:
@author: Chale
@software: PyCharm on 2020/6/5 12:00
"""
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Bar
#导入csv文件
approval_rate = pd.read_csv('file/approval_rate.csv')
#xy轴赋值
x = approval_rate['政治举措'].tolist()
y1 = approval_rate['支持'].tolist()
y2 = approval_rate['反对'].tolist()
y3 = approval_rate['不发表意见'].tolist()
c = (
Bar()
#设置x轴
.add_xaxis(x)
#设置y轴堆叠数据
.add_yaxis("支持", y1, stack="stack1")
.add_yaxis("反对", y2, stack="stack1")
.add_yaxis("不发表意见", y3, stack="stack1")
.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
.set_global_opts(title_opts=opts.TitleOpts(title="奥巴马的政治举措民意调查结果", subtitle="Chale"))
.render("html/堆叠柱形图aobama.html")
)
以上就是pyecharts在数据可视化中的应用中比较基础的部分