您现在的位置是:首页 >技术杂谈 >100天精通Python(可视化篇)——第91天:Pyecharts绘制各种折线图实战网站首页技术杂谈
100天精通Python(可视化篇)——第91天:Pyecharts绘制各种折线图实战
简介100天精通Python(可视化篇)——第91天:Pyecharts绘制各种折线图实战
专栏导读
??本文已收录于《100天精通Python从入门到就业》:本专栏专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html
- 优点:订阅限时9.9付费专栏进入千人全栈VIP答疑群,作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会)
- 专栏福利:简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等
1. 基本流程
Pyecharts是一个Python的可视化库,可以帮助用户轻松绘制各种类型的图表,包括折线图。下面是Pyecharts绘制折线图的步骤及其说明:
- 导入所需的模块
在Python代码中引入所需的模块,如下所示:
from pyecharts import options as opts
from pyecharts.charts import Line
- 准备数据
准备要展示的数据,以列表的形式存储,如下所示:
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data = [120, 200, 150, 80, 70, 110, 130]
- 创建折线图实例
创建一个折线图实例,并设置其基本属性,如下所示:
line_chart = Line()
line_chart.set_global_opts(title_opts=opts.TitleOpts(title="折线图示例"))
- 添加数据
将准备好的数据添加到折线图实例中,如下所示:
line_chart.add_xaxis(xaxis_data=x_data)
line_chart.add_yaxis(series_name="销售额", y_axis=y_data)
- 渲染图表
使用render()方法将折线图渲染成HTML文件或在Jupyter Notebook中显示,如下所示:
line_chart.render_notebook()
line_chart.render("line_chart.html")
- 结果展示
最终的折线图将展示在网页或Jupyter Notebook中,如下所示:
2. 多条折线图
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True)
.add_yaxis(series_name="y2线",y_axis=y_data2)
.set_global_opts(title_opts=opts.TitleOpts(title="多折线重叠"))
)
# 渲染图表
line.render_notebook()
运行结果:
3. 添加最小值最大值平均值
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True)
.add_yaxis(series_name="y2线",y_axis=y_data2, markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
))
.set_global_opts(title_opts=opts.TitleOpts(title="多折线重叠"))
)
# 渲染图表
line.render_notebook()
运行结果:
4. 竖线提示信息
通过tooltip_opts=opts.TooltipOpts(trigger="axis")
设置竖线提示信息:
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True)
.add_yaxis(series_name="y2线",y_axis=y_data2, markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
))
.set_global_opts(title_opts=opts.TitleOpts(title="多折线重叠"),tooltip_opts=opts.TooltipOpts(trigger="axis"))
)
# 渲染图表
line.render_notebook()
运行结果:
5. 阶梯图
通过is_step=True
设置阶梯图:
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True,is_step=True)
.add_yaxis(series_name="y2线",y_axis=y_data2,is_step=True, markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
))
.set_global_opts(title_opts=opts.TitleOpts(title="阶梯图"),tooltip_opts=opts.TooltipOpts(trigger="axis"))
)
# 渲染图表
line.render_notebook()
运行结果:
6. 平滑曲线折线图
通过is_smooth=True
参数设置平滑曲线图:
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True,is_smooth=True)
.add_yaxis(series_name="y2线",y_axis=y_data2, is_smooth=True, markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
))
.set_global_opts(title_opts=opts.TitleOpts(title="平滑曲线折线图"),tooltip_opts=opts.TooltipOpts(trigger="axis"))
)
# 渲染图表
line.render_notebook()
运行结果:
7. 面积折线图
通过areastyle_opts=opts.AreaStyleOpts(opacity=0.5)
设置面积折线图:
import pyecharts.options as opts
from pyecharts.charts import Line
# 数据
x_data = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
y_data1 = [120, 200, 150, 80, 70, 110, 130]
y_data2 = [90, 150, 200, 120, 100, 80, 110]
line=(
# 创建折线图实例
Line()
# 将准备好的两组数据添加到折线图实例中
.add_xaxis(xaxis_data=x_data)
.add_yaxis(series_name="y1线",y_axis=y_data1,symbol="arrow",is_symbol_show=True,is_smooth=True,
areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.add_yaxis(series_name="y2线",y_axis=y_data2, is_smooth=True, markpoint_opts=opts.MarkPointOpts(
data=[
opts.MarkPointItem(type_="max", name="最大值"),
opts.MarkPointItem(type_="min", name="最小值"),
]
),
markline_opts=opts.MarkLineOpts(
data=[opts.MarkLineItem(type_="average", name="平均值")]
),
areastyle_opts=opts.AreaStyleOpts(opacity=0.5))
.set_global_opts(title_opts=opts.TitleOpts(title="面积折线图"),tooltip_opts=opts.TooltipOpts(trigger="axis"))
)
# 渲染图表
line.render_notebook()
运行结果:
8. 堆积图
from pyecharts.charts import Bar
from pyecharts import options as opts
# 准备数据
x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
y1_data = [820, 932, 901, 934, 1290, 1330, 1320]
y2_data = [800, 900, 650, 750, 1200, 1320, 1150]
y3_data = [500, 600, 700, 800, 900, 1000, 1100]
y4_data = [400, 500, 600, 700, 800, 900, 1000]
y5_data = [300, 400, 500, 600, 700, 800, 900]
line=(Line(init_opts=opts.InitOpts(width='1000px',height='600px'))
.add_xaxis(x_data)
.add_yaxis("line1",y1_data,areastyle_opts=opts.AreaStyleOpts(opacity=0.5),stack="stack0")#填充颜色 并且stack实现堆叠
.add_yaxis("line2",y2_data,areastyle_opts=opts.AreaStyleOpts(opacity=0.5),stack="stack0")
.add_yaxis("line3",y3_data,areastyle_opts=opts.AreaStyleOpts(opacity=0.5),stack="stack0")
.add_yaxis("line4",y4_data,areastyle_opts=opts.AreaStyleOpts(opacity=0.5),stack="stack0")
.add_yaxis("line5",y5_data,areastyle_opts=opts.AreaStyleOpts(opacity=0.5),stack="stack0")
)
# 渲染图表
line.render_notebook()
运行结果:
9. 双横坐标折线图
import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode
js_formatter = """function (params) {
console.log(params);
return '降水量 ' + params.value + (params.seriesData.length ? ':' + params.seriesData[0].data : '');
}"""
x_data1 = ["2022-1", "2022-2", "2022-3", "2022-4", "2022-5", "2022-6", "2022-7", "2022-8", "2022-9", "2022-10",
"2022-11", "2022-12", ]
x_data2 = ["2023-1", "2023-2", "2023-3", "2023-4", "2023-5", "2023-6", "2023-7", "2023-8", "2023-9", "2023-10",
"2023-11", "2023-12", ]
y_data1 = [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
y_data2 = [3.9, 5.9, 11.1, 18.7, 48.3, 69.2, 231.6, 46.6, 55.4, 18.4, 10.3, 0.7]
line = (
Line()
.add_xaxis(x_data1)
.extend_axis(x_data2
,
xaxis=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#6e9ef1")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
)
.add_yaxis(
series_name="2022 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
color="#d14a61",
y_axis=y_data1,
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.add_yaxis(
series_name="2023 降水量",
is_smooth=True,
symbol="emptyCircle",
is_symbol_show=False,
color="#6e9ef1",
y_axis=y_data2,
label_opts=opts.LabelOpts(is_show=False),
linestyle_opts=opts.LineStyleOpts(width=2),
)
.set_global_opts(
legend_opts=opts.LegendOpts(),
tooltip_opts=opts.TooltipOpts(trigger="none", axis_pointer_type="cross"),
xaxis_opts=opts.AxisOpts(
type_="category",
axistick_opts=opts.AxisTickOpts(is_align_with_label=True),
axisline_opts=opts.AxisLineOpts(
is_on_zero=False, linestyle_opts=opts.LineStyleOpts(color="#d14a61")
),
axispointer_opts=opts.AxisPointerOpts(
is_show=True, label=opts.LabelOpts(formatter=JsCode(js_formatter))
),
),
yaxis_opts=opts.AxisOpts(
type_="value",
splitline_opts=opts.SplitLineOpts(
is_show=True, linestyle_opts=opts.LineStyleOpts(opacity=1)
),
),
)
)
line.render_notebook()
运行结果:
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。