python - Plotly 使用数据指定跟踪颜色的子图
问题描述
我有一个数据垂直剖面的数据框,我想在两个相邻的子图中绘制两个变量(速度和温度)与深度的关系,即 Row1/Col1 Depth v Velocity 和 Row1/Col2 Depth v Temperature。
我希望所有垂直剖面(对于每个变量)都绘制在同一个图表上,并通过它们是哪个剖面进行颜色编码,即 Dip1、Dip2。我不需要控制颜色,只是它们的颜色不同。下面的代码做了我想要的单个数字,但我如何让它们在同一个数字上并排?
这就是我想要创建的,你可以看到不同的 Dip 有不同的颜色。
下面的代码创建了我用来创建上面图像的两个单独的 Web 浏览器图像。
import pandas as pd
import plotly.express as px
data= pd.read_csv('TS_DIP.000',delimiter='\t') #tab delimiter
df=pd.DataFrame(data, columns=['Depth','Temperature','Velocity','Dip No.'])
df['Depth']*=-1 # invert depths - below seabed
fig1 = px.line(df, x="Velocity", y="Depth", color="Dip No.",
title="Velocity Profiles")
fig2 = px.line(df, x="Temperature", y="Depth", color="Dip No.",
title="Temperature Profiles")
fig1.update_layout(xaxis_title='Velocity m/s',yaxis_title='Depth m',template="plotly_dark")
fig2.update_layout(xaxis_title='Temperature C',yaxis_title='Depth m',template="plotly_dark")
fig1.show()
fig2.show()
这是我们从 Dip1 更改为 Dip2 的数据示例
Date Velocity Depth Temperature Dip No.
26/04/2021 1478.824 2.669 7.055 Dip1
26/04/2021 1478.811 1.842 7.053 Dip1
26/04/2021 1478.809 1.366 7.052 Dip1
26/04/2021 1478.810 1.678 7.054 Dip1
26/04/2021 1478.810 2.118 7.052 Dip1
26/04/2021 1478.814 2.263 7.050 Dip1
26/04/2021 1478.813 2.166 7.051 Dip1
26/04/2021 1478.809 1.952 7.052 Dip1
26/04/2021 1478.797 1.811 7.052 Dip1
25/04/2021 1479.753 2.872 7.317 Dip2
25/04/2021 1479.751 2.996 7.312 Dip2
25/04/2021 1479.762 3.531 7.315 Dip2
25/04/2021 1479.774 4.123 7.312 Dip2
25/04/2021 1479.807 4.645 7.315 Dip2
25/04/2021 1479.818 4.987 7.317 Dip2
25/04/2021 1479.827 5.230 7.318 Dip2
25/04/2021 1479.826 5.427 7.320 Dip2
25/04/2021 1479.834 5.632 7.322 Dip2
25/04/2021 1479.843 6.128 7.321 Dip2
25/04/2021 1479.859 6.966 7.324 Dip2
25/04/2021 1479.870 7.957 7.323 Dip2
我已经看过了,fig=make_subplots(rows=1,cols=2)
但是我看到的示例随后使用了 的变体,fig.add_trace(go.Scatter(x=df['Velocity'], y=df['Depth']),row=1, col=1)
并且我似乎无法找到复制color="Dip No."
上面代码中的命令的正确方法,因此所有 Dips 都是相同的颜色。
解决方案
我审查了它以回应您的评论。使用PX(EXPRESS)来简化颜色编码很简单,所以我用它来构建子图。有一个重复的图例,我正在寻找消除它的方法,这符合您的意图吗?
import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
fig1 = px.line(df, x='Velocity', y='Depth', color='Dip No.')
fig2 = px.line(df, x='Temperature', y='Depth', color='Dip No.')
fig = make_subplots(rows=1, cols=2,
column_titles = ['Velocity m/s','Temperature C'],
row_titles = ['Depth m','Depth m'],
shared_yaxes=False)
fig.add_trace(fig1['data'][0], row=1, col=1)
fig.add_trace(fig1['data'][1], row=1, col=1)
fig.add_trace(fig2['data'][0], row=1, col=2)
fig.add_trace(fig2['data'][1], row=1, col=2)
fig.update_layout(template="plotly_dark")
fig.show()
推荐阅读
- java - 当方法被标记为 noRollbackFor 时,@Transactional 类中的 UnexpectedRollbackException 抛出异常
- java - 如何在 Java 中链接来自不同数组的字符串?
- python-3.x - 使用所有作者连接创建数据框
- google-chrome - FFmpeg 在记录 chrome 活动时在一分钟内吃掉所有内存并崩溃
- bash - Bash grep -P 使用文件中的正则表达式列表
- odata - 使用 openUI5 更新 oData v4 实体中的非原始值
- hyperledger-fabric - Hyperledger Fabric:TLS 握手失败并出现错误 tls
- python-3.x - matplotlib.pyplot plot_date 函数在 cftime.datetime 对象上中断
- php - 我需要根据 WHERE IN claus 中的 id 匹配更新表中的值
- python - 尝试使用 sys.path.append() 相对导入超出顶级包