Python 在同一图形上以不同采样绘制两个数据帧时间序列(并使用双Y轴)

Python 在同一图形上以不同采样绘制两个数据帧时间序列(并使用双Y轴),python,pandas,matplotlib,Python,Pandas,Matplotlib,我有两个数据帧,看起来像: Temp [Degrees_C] Cond [mS/cm] yyyy-mm-ddThh:mm:ss.sss 2020-01-28 03:00:59 14.553947 19.301285 2020-01-28 08:00:59 14.501740 19.310037 2020-01-28 13:00:59 14.425415 18.531609 2020-01-28

我有两个数据帧,看起来像:

                  Temp [Degrees_C]  Cond [mS/cm]    
yyyy-mm-ddThh:mm:ss.sss             
2020-01-28 03:00:59 14.553947   19.301285   
2020-01-28 08:00:59 14.501740   19.310037   
2020-01-28 13:00:59 14.425415   18.531609   
2020-01-28 18:00:59 14.414717   16.155998   
...
这是:

                    CONDUCTIVITY    Temp [C]
DATE TIME           
2020-01-28 03:00:00     18.240  15.761111
2020-01-28 04:00:00     18.147  15.722222
2020-01-28 05:00:00     17.930  15.722222
2020-01-28 06:00:00     17.873  15.666667
...
我想使用这两个数据集创建一个图,它们应该与日期时间共享相同的x轴,以及两个不同的y轴(一个用于温度,一个用于电导率)。 然而,由于这两种方法的取样方式不同,我不确定如何进行

有什么建议吗


谢谢

此matplotlib文档网页显示了如何使用双y轴打印的简单示例:


你写了一个数据帧,听起来像是用熊猫来处理数据。您可以简单地将数据强制转换为numpy数组,并遵循下面的示例。

要获得两个数据集之间的匹配日期,我将使用。或者,您可以使用,但如果您不确定舍入是否有效,则可能会导致一些问题

要绘制两个y轴,您需要

返回:


主要问题不是双刻度Y轴,而是x轴的不同时间采样。
import pandas as pd
from io import StringIO

str1 = """
2020-01-28 03:00:59, 14.553947,   19.301285   
2020-01-28 08:00:59, 14.501740,   19.310037   
2020-01-28 13:00:59, 14.425415,   18.531609   
2020-01-28 18:00:59, 14.414717,   16.155998   
"""


str2="""
2020-01-28 03:00:00,     18.240,  15.761111
2020-01-28 04:00:00,    18.147,  15.722222
2020-01-28 05:00:00,     17.930,  15.722222
2020-01-28 06:00:00,     17.873,  15.666667
"""

colnames1 = ['date','cond','temp']
colnames2 = ['date','temp', 'cond']
df1 = pd.read_csv(StringIO(str1), header=None, names = colnames1, parse_dates=['date'])
df2 = pd.read_csv(StringIO(str2), header=None, names = colnames2, parse_dates=['date'])

#Offset to even seconds
df1.date = df1.date - pd.DateOffset(seconds=59)

#plot
ax = df1.plot(x='date',y='temp', label='df1', color='k', ls = '--')

#Create second y axis
ax_tw = ax.twinx()
df1.plot(x='date',y='cond', ax = ax_tw, label='df1', color='k', ls='-')
df2.plot(x='date',y='temp', ax= ax, label='df2', color='red', ls='--')
df2.plot(x='date',y='cond', ax = ax_tw,label='df2', color='red', ls ='-')
ax.legend()