python - 使用 Plotly 在面积图上添加一条具有适当比例的线
问题描述
我有以下代码:
import pandas as pd
import plotly.express as px
fig = px.area(df, x="Decade", y="Financial_Impact", color="Disaster_Type", title="Financial Impact, World, RCP = 2.6", color_discrete_sequence=["#FDB714", "#009CA7", "#F05023"])
fig.show()
生成以下面积图:
现在我有一个变量,称为C
为每十年提供一个温度 (°C)。我怎样才能添加一条线来显示C
,相对于C
图表右侧的比例?
以下是数据集的前五行:
df.head()
解决方案
import plotly.express as px
from plotly.subplots import make_subplots
subfig = make_subplots(specs=[[{"secondary_y": True}]])
fig = px.area(df, x="Decade", y="Financial_Impact", color="Disaster_Type", color_discrete_sequence=["#FDB714", "#009CA7", "#F05023"])
fig2 = px.line(df, x="Decade",y=df.C)
fig2.update_traces(yaxis="y2",showlegend=True,name='Temperature')
subfig.add_traces(fig.data + fig2.data)
subfig.layout.xaxis.title="Decades"
subfig.layout.yaxis.title="Financial Impact"
subfig.layout.yaxis2.title="°C"
subfig.layout.title="Financial Impact, World, RCP = 2.6"
subfig.show()
推荐阅读
- pdf - 是否可以使用 Apps 脚本在 Google 电子表格中添加分页符?
- php - 在总和(价格)值之后,按 id 删除数组中的相同 ID
- arrays - 在 Octave 上创建两个依赖向量
- c# - 如何在不破坏 ARCore 锚定的情况下直接在另一个游戏对象上方实例化一个移动的游戏对象?
- php - Laravel 不在我的服务器上发送电子邮件,而是在本地主机上发送
- node.js - 尝试使用“--force-fips”标志执行 js 文件时抛出“为 fips 禁用”错误
- python - Python将整列添加到csv文件,而不读取文件内容
- python - 如何使 QTextBrowser 中的文本可选择?
- c# - 停止无限对话循环
- audio - 上传到谷歌云存储时转换音频文件