You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib子图无法显示低分辨率数据问题求助

双Y轴时序图低分辨率数据不显示问题解决

问题背景

使用Matplotlib绘制共享X轴的双Y轴子图,展示两组时序数据:

  • stw:每60秒一个数据点,空值为nan
  • Trim corrected:每秒一个数据点

但低分辨率的stw数据完全无法显示,仅能看到Trim corrected的数据;当更换为同分辨率的sog和stw数据时,两组数据均可正常显示。

初始未成功代码

fig, ax = plt.subplots(figsize=(15,5))

ax.plot(df1['Time corrected'],df1['stw'], color = 'green')
ax.tick_params(axis='y', labelcolor='green')

ax1=ax.twinx()

ax1.plot(df1['Time corrected'], df1['Trim corrected'], color= 'purple')
ax1.tick_params(axis='y', labelcolor='purple')

运行后仅显示Trim corrected的数据,stw无任何显示。

验证测试

1. 同分辨率数据测试

将Trim corrected替换为同分辨率的sog数据,两组数据均可正常显示:

import pandas as pd
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(15,5))

ax.plot(df1['Time corrected'],df1['stw'], color = 'green')
ax.tick_params(axis='y', labelcolor='green')

ax1=ax.twinx()

ax1.plot(df1['Time corrected'], df1['sog'], color= 'purple')
ax1.tick_params(axis='y', labelcolor='purple')

2. 单独绘制stw数据

使用df.plot方法单独绘制stw数据,可正常显示:

import pandas as pd
import matplotlib.pyplot as plt

df1.plot(figsize= (15,5),
          kind= 'line',
          color= 'green',     
          x= 'tsp',
          y= 'stw', 
          sharex= False,
          title= 'Speed over Time',
          ylabel= 'Speed Through Water [kn]',
          xlabel= 'Time [UTC]',
          grid = True)
plt.rcParams['figure.dpi'] = 200
plt.show()

成功解决方案(Seaborn实现)

通过Seaborn库实现两组数据的正常展示,代码如下:

df1 = df1.rename({'Trim corrected':'Pitch Angle [°]'}, axis = 'columns')
df1 = df1.rename({'stw':'Speed through Water [kn]'}, axis = 'columns')
df1 = df1.rename({'Time corrected':'Time [UTC]'}, axis = 'columns')

import seaborn as sns

fig, ax = plt.subplots(figsize=(15,5))
ax1= sns.lineplot(x=df1['Time [UTC]'],y=df1.loc[:,'Pitch Angle [°]'], color='purple', label='Pitch Angle [°]', ax = ax)
ax1.set_ylim(-1.5,2.2)
ax2 = ax1.twinx()

sns.lineplot(x=df1['Time [UTC]'],y=df1['Speed through Water [kn]'], color='green', ax = ax2,linewidth=3, label= 'Speed Through Water [kn]') 
plt.rcParams['figure.dpi'] = 200

ax.legend()

运行后可同时展示低分辨率的stw和高分辨率的Trim corrected数据。

内容的提问来源于stack exchange,提问作者johannesv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:22:46