plotly绘制双y轴折线图并定制样式

Background

  • 数据分析离不开数据的可视化。Python 中的可视化依赖库有很多非常优秀的可视化神器。例如较流行的有pandas,matplotlib,Seaborn、plotly和Altair等。
  • 经过简单对比,最终选用plotly来实现目前的可视化需求。附 plotly官方文档地址
  • 这里主要介绍下折线图的绘制与定制。

1、最终效果

是可交互式折线图。

在这里插入图片描述

2、源码

import plotly.graph_objects as go
import numpy as np
import pandas as pd


def fig_line(title, lx, dy):
    """绘制折线图
    :param title 标题
    :param lx x轴数据 []
    :param dy y轴数据 {fyt: [], syt: []}
    """
    if lx and dy:
        fyt = list(dy.keys())[0]
        syt = list(dy.keys())[1]
        line1 = go.Scatter(x=lx, y=dy[fyt], showlegend=True, line=dict(color='blue', width=1), connectgaps=True, mode='lines', opacity=0.9, name=fyt)
        line2 = go.Scatter(x=lx, y=dy[syt], showlegend=True, line=dict(color='red', width=1), connectgaps=True, mode='lines', opacity=0.9, name=syt, yaxis='y2')
        layout = go.Layout(
            # 设置标题
            # title=title,
            # 设置y轴label及范围
            yaxis=dict(title=fyt, range=[0, 2]),
            # 设置fig大小
            height=500,
            width=200,
            margin=dict(
                autoexpand=True,
                l=10,
                r=10,
                t=30,
                b=30,
            ),
            # 设置次y轴label及范围
            yaxis2=dict(title=syt, range=[0, 50], overlaying='y', side='right'),
            # 设置背景色
            # plot_bgcolor='gray',
            # 设置图例信息,x、y可以控制位置
            legend=dict(x=0.80, y=0.98, font=dict(size=12, color='black')))
        fig = go.Figure(data=[line1, line2], layout=layout)
        fig.show()


def main():
    """主函数"""
    lx = ['2022-01', '2022-02', '2022-03', '2022-04', '2022-05', '2022-06', '2022-07', '2022-08', '2022-09', '2022-10', '2022-11', '2022-12']
    y1 = [0.919, 0.888, 0.163, 0.995, 0.723, 0.192, 0.069, 0.803, 0.645, 0.527, 0.675, 0.419]
    y2 = [20, 18, 15, 2, 8, 39, 26, 19, 26, 40, 5, 5]
    dy = {'应力(MPa)': y1, '温度(℃)': y2}
    title = '应力随温度变化情况'
    fig_line(title, lx, dy)


if __name__ == '__main__':
    main()
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Plotly是一个强大的数据可视化工具库,可以用来动态生成折线图。使用Plotly创建动态折线图的步骤如下: 1. 导入所需的库和模块。首先,您需要导入Plotly库和相关的模块。可以使用以下代码导入: ```python import plotly.graph_objs as go from plotly.offline import plot ``` 2. 创建数据。创建包含折线图数据的列表。例如,您可以创建一个包含x轴和y轴数据的列表: ```python x = [1, 2, 3, 4, 5] y = [10, 15, 7, 12, 9] ``` 3. 创建折线图布局。使用`go.Layout()`函数创建折线图的布局,并设置标题、轴标签等参数。例如: ```python layout = go.Layout( title='动态生成折线图', xaxis=dict(title='X轴'), yaxis=dict(title='Y轴') ) ``` 4. 创建折线图轨迹。使用`go.Scatter()`函数创建折线图的轨迹,并设置x轴和y轴数据。例如: ```python trace = go.Scatter( x=x, y=y, mode='lines', name='折线图' ) ``` 5. 组合数据和布局。将轨迹和布局组合在一起,创建一个数据列表。例如: ```python data = [trace] ``` 6. 绘制折线图。使用`plot()`函数将数据和布局传递给Plotly库,并生成折线图。例如: ```python fig = go.Figure(data=data, layout=layout) plot(fig, filename='动态折线图.html') ``` 这样,您就可以使用Plotly动态生成折线图了。您可以将生成的图表保存为HTML文件,并在浏览器中查看。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [python折线图样式_用PythonPlotly画出炫酷的数据可视化(含各类图介绍)](https://blog.csdn.net/weixin_39607240/article/details/110109686)[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: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WaiSaa

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值