Python最美数据可视化折线图

展现多个系列的数据,一般习惯使用柱状图或折线图.本文使用个人比较喜爱的Pyecharts库,绘制呈现多个系列数据的普通折现图(line chart)、堆叠图(stack chart)、面积堆叠图(stack area chart).

环境准备

首先安装Pyecharts,我使用的是最新版本:1.9.1,团队介绍会在明年发布2.0版本.

其次,导入以下模块:

import pyecharts.options as opts
from pyecharts.charts import Line
from pyecharts.commons.utils import JsCode

optionsLine都比较常见,JsCodePyecharts直接与echarts转化的中间对象,我比较常用它来创建颜色渐变的效果,让图形更富有表现力.

数据

为了书写方便,数据选取尽量简单,如下一个x轴,三个系列:

x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
y_data1 = [140, 232, 101, 264, 90, 340, 250]
y_data2 = [120, 282, 111, 234, 220, 340, 310]
y_data3 = [320, 132, 201, 334, 190, 130, 220]

普通折现图

我们先来看看绘制结果,如下所示,线条堆叠现象严重,不够清晰.

普通折线图

普通折线图

堆叠折线图

为了解决线条堆叠问题,就有了堆叠折线图,有意思的是,堆叠折线图并不堆叠.

如下所示,三个系列折线图完全被分离开:

堆叠折线图

堆叠折线图

上面折线图,点与点之间的过渡是通过线段连接,其实还可以通过平滑的曲线过渡.

这在Pyecharts中,只需设置is_smooth参数为True就行:

.add_yaxis(
        y_axis=y_data1,
        is_smooth=True
        )

这样就绘制出了平滑过渡的折线图:

平滑过渡的折线图

面积堆叠图

先看下我绘制的面积堆叠图,可以看到它与上面平滑过渡的折线图的相比,填充了颜色,一下就能吸引我们的眼球.

面积堆叠图

面积堆叠图

这是怎么做到的?其实,相比于折线图,它只是配置上了areastyle_opts,非常方便!

areastyle_opts=opts.AreaStyleOpts(opacity=1)

当然,上面填充的颜色没有渐变效果,要想添加也非常简单.

通过上面说到的JsCode,很容易添加上,并且代码基本是固定的,复用性强.

最终实现的颜色渐变效果如下:

面积堆叠图

面积堆叠图

完整代码

Pyecharts绘制

下面是完整代码

# encoding: utf-8

"""

@file: area_graph.py

@desc:

@author: zhenguo

@time: 2021/12/12

"""

import pyecharts.options as opts

from pyecharts.charts import Line

from pyecharts.commons.utils import JsCode



x_data = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]

y_data1 = [140, 232, 101, 264, 90, 340, 250]

y_data2 = [120, 282, 111, 234, 220, 340, 310]

y_data3 = [320, 132, 201, 334, 190, 130, 220]



(

    Line()

        .add_xaxis(xaxis_data=x_data)

        .add_yaxis(

        series_name="品类 1",

        color='#80FFA5',

        y_axis=y_data1,

        is_smooth=True,

        stack='总量',

        is_symbol_show=False,

        label_opts=opts.LabelOpts(is_show=False),

        linestyle_opts=opts.LineStyleOpts(width=0),

        areastyle_opts=opts.AreaStyleOpts(opacity=1,

                                          color=JsCode(

                                              """new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                                              {

                                                offset: 0,

                                                color: 'rgba(128, 255, 165)'

                                              },

                                              {

                                                offset: 1,

                                                color: 'rgba(1, 191, 236)'

                                              }

                                            ])"""

                                          )

                                          ),

    ).add_yaxis(

        series_name="品类 2",

        color='#00DDFF',

        y_axis=y_data2,

        is_smooth=True,

        stack='总量',

        is_symbol_show=False,

        label_opts=opts.LabelOpts(is_show=False),

        linestyle_opts=opts.LineStyleOpts(width=0),

        areastyle_opts=opts.AreaStyleOpts(opacity=1,

                                          color=JsCode(

                                              """new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                                              {

                                                offset: 0,

                                                color: 'rgba(0, 221, 255)'

                                              },

                                              {

                                                offset: 1,

                                                color: 'rgba(77, 119, 255)'

                                              }

                                            ])"""

                                          )

                                          ),

    ).add_yaxis(

        series_name="品类 3",

        color='#37A2FF',

        y_axis=y_data3,

        is_smooth=True,

        stack='总量',

        is_symbol_show=False,

        label_opts=opts.LabelOpts(is_show=False, position="top"),

        linestyle_opts=opts.LineStyleOpts(width=0),

        areastyle_opts=opts.AreaStyleOpts(opacity=1,

                                          color=JsCode(

                                              """new echarts.graphic.LinearGradient(0, 0, 0, 1, [

                                              {

                                                offset: 0,

                                                color: 'rgba(55, 162, 255)'

                                              },

                                              {

                                                offset: 1,

                                                color: 'rgba(116, 21, 219)'

                                              }

                                            ])"""

                                          )

                                          ),

    ).set_global_opts(

        tooltip_opts=opts.TooltipOpts(is_show=True,

                                      trigger='axis',

                                      axis_pointer_type='cross'),

        yaxis_opts=opts.AxisOpts(type_="value", name="销量"),

        xaxis_opts=opts.AxisOpts(boundary_gap=False, type_="category"),

    )

        .render("stacked_area_chart.html")

)

 
- 感谢大家的关注和支持!想了解更多Python编程精彩知识内容,请关注我的 微信公众号:python小胡子,有最新最前沿的的python知识和人工智能AI与大家共享,同时,如果你觉得这篇文章对你有帮助,不妨点个赞,并点击关注.动动你发财的手,万分感谢!!!

- 原创文章不易,求点赞、在看、转发或留言,这样对我创作下一个精美文章会有莫大的动力!

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python中可以使用matplotlib和pandas模块来实现数据可视化折线图折线图常用于展示时间序列数据的趋势。在matplotlib模块中,可以使用plot方法来绘制折线图。当然,也可以使用pandas模块的plot方法来绘制折线图。 下面是使用pandas模块绘制折线图的具体代码示例: ```python import pandas as pd import matplotlib.pyplot as plt # 设置绘图风格 plt.style.use('ggplot') # 处理中文乱码 plt.rcParams['font.sans-serif'] = ['Microsoft YaHei'] # 读取数据 data = pd.read_excel(r'weather.xlsx') # 统计每月的平均最高气温 data = data.pivot_table(index='month', columns='year', values='high') # 绘制折线图 data.plot(kind='line', style=['-', '--', ':']) # 修改坐标轴标签 plt.xlabel('月份') plt.ylabel('气温') # 添加图形标题 plt.title('每月平均最高气温波动趋势') # 显示图形 plt.show() ``` 通过以上代码,可以绘制出每月平均最高气温的折线图,其中每年的数据使用不同的线条样式进行区分。这样可以更直观地展示出数据的变化趋势。 更多关于使用matplotlib模块绘制折线图的详细信息,可以参考Matplotlib的官方文档。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python数据可视化的例子——折线图(line)](https://blog.csdn.net/weixin_48615832/article/details/108466201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [001 Python数据可视化折线图](https://blog.csdn.net/qixinxiangshicheng/article/details/129480075)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

python茶水实验室

你的关注,是我创作的最大动力.

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值