【Python】pyecharts 模块 ③ ( 使用 pyecharts 模块绘制折线图 )

编程入门 行业动态 更新时间:2024-10-09 23:14:16

【Python】pyecharts <a href=https://www.elefans.com/category/jswz/34/1771428.html style=模块 ③ ( 使用 pyecharts 模块绘制折线图 )"/>

【Python】pyecharts 模块 ③ ( 使用 pyecharts 模块绘制折线图 )

文章目录

  • 一、使用 pyecharts 模块绘制折线图
    • 1、折线图绘制过程
    • 2、完整代码示例


pyecharts 画廊网站 : /

  • 在该网站可查看官方示例




一、使用 pyecharts 模块绘制折线图




1、折线图绘制过程


首先 , 导入 折线图 Line 对象 , 该类定义在 pyecharts.charts 中 ;

# 导入 pyecharts 模块中的 折线图 Line 对象
from pyecharts.charts import Line

然后 , 创建 Line 类型 折线图 对象 ;

# 创建 折线图 对象
line = Line()

再后 , 分别 设置 x 轴 和 y 轴 数据 ;

# 设置 x 轴数据
line.add_xaxis(["西城", "东城", "海淀", "朝阳", "昌平"])# 设置 y 轴数据
line.add_yaxis("清北录取人数", [105, 28, 215, 16, 1])

最后 , 调用 Line 对象的 render() 函数 , 生成图标 ;

# 生成图表
line.render()

生成的 折线图 图表 , 会以 HTML 页面的形式展示出来 ;

生成的 HTML 文件名称为 render.html ;


2、完整代码示例


代码示例 :

"""
pyecharts 模块
"""# 导入 pyecharts 模块中的 折线图 Line 对象
from pyecharts.charts import Line# 创建 折线图 对象
line = Line()# 设置 x 轴数据
line.add_xaxis(["西城", "东城", "海淀", "朝阳", "昌平"])# 设置 y 轴数据
line.add_yaxis("清北录取人数", [105, 28, 215, 16, 1])# 生成图表
line.render()

在 该 python 源码的同目录中 , 生成 render.html ,


生成的 html 如下 :

<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Awesome-pyecharts</title><script type="text/javascript" src=".min.js"></script></head>
<body ><div id="f743bf5a10074ed784e9be0fb639863e" class="chart-container" style="width:900px; height:500px; "></div><script>var chart_f743bf5a10074ed784e9be0fb639863e = echarts.init(document.getElementById('f743bf5a10074ed784e9be0fb639863e'), 'white', {renderer: 'canvas'});var option_f743bf5a10074ed784e9be0fb639863e = {"animation": true,"animationThreshold": 2000,"animationDuration": 1000,"animationEasing": "cubicOut","animationDelay": 0,"animationDurationUpdate": 300,"animationEasingUpdate": "cubicOut","animationDelayUpdate": 0,"aria": {"enabled": false},"color": ["#5470c6","#91cc75","#fac858","#ee6666","#73c0de","#3ba272","#fc8452","#9a60b4","#ea7ccc"],"series": [{"type": "line","name": "\u6e05\u5317\u5f55\u53d6\u4eba\u6570","connectNulls": false,"xAxisIndex": 0,"symbolSize": 4,"showSymbol": true,"smooth": false,"clip": true,"step": false,"data": [["\u897f\u57ce",105],["\u4e1c\u57ce",28],["\u6d77\u6dc0",215],["\u671d\u9633",16],["\u660c\u5e73",1]],"hoverAnimation": true,"label": {"show": true,"margin": 8},"logBase": 10,"seriesLayoutBy": "column","lineStyle": {"show": true,"width": 1,"opacity": 1,"curveness": 0,"type": "solid"},"areaStyle": {"opacity": 0},"zlevel": 0,"z": 0}],"legend": [{"data": ["\u6e05\u5317\u5f55\u53d6\u4eba\u6570"],"selected": {}}],"tooltip": {"show": true,"trigger": "item","triggerOn": "mousemove|click","axisPointer": {"type": "line"},"showContent": true,"alwaysShowContent": false,"showDelay": 0,"hideDelay": 100,"enterable": false,"confine": false,"appendToBody": false,"transitionDuration": 0.4,"textStyle": {"fontSize": 14},"borderWidth": 0,"padding": 5,"order": "seriesAsc"},"xAxis": [{"show": true,"scale": false,"nameLocation": "end","nameGap": 15,"gridIndex": 0,"inverse": false,"offset": 0,"splitNumber": 5,"minInterval": 0,"splitLine": {"show": true,"lineStyle": {"show": true,"width": 1,"opacity": 1,"curveness": 0,"type": "solid"}},"data": ["\u897f\u57ce","\u4e1c\u57ce","\u6d77\u6dc0","\u671d\u9633","\u660c\u5e73"]}],"yAxis": [{"show": true,"scale": false,"nameLocation": "end","nameGap": 15,"gridIndex": 0,"inverse": false,"offset": 0,"splitNumber": 5,"minInterval": 0,"splitLine": {"show": true,"lineStyle": {"show": true,"width": 1,"opacity": 1,"curveness": 0,"type": "solid"}}}]
};chart_f743bf5a10074ed784e9be0fb639863e.setOption(option_f743bf5a10074ed784e9be0fb639863e);</script>
</body>
</html>

点击右上角的浏览器选项 ,


这里点击 Chrome 浏览器 , 使用 该浏览器 查看生成的 折线图 ;

生成的 折线图 内容如下 :

更多推荐

【Python】pyecharts 模块 ③ ( 使用 pyecharts 模块绘制折线图 )

本文发布于:2024-02-07 09:13:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   折线图   Python   pyecharts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!