在S.O.上找到。以下解决方案来绘制多个数据帧:熊猫图只在多个数据帧之间重叠
AX = df1.plot()
df2.plot(AX = AX)
但如果我只是想绘制哪里是什么,他们交叠?
假设df1索引是跨越24小时的时间戳,而df2索引也是在df1的24小时内跨越12小时的时间戳(但与df1不完全相同)。
如果我只想绘制两个数据帧覆盖的12小时。什么是简单的方法来做到这一点?
在S.O.上找到。以下解决方案来绘制多个数据帧:熊猫图只在多个数据帧之间重叠
AX = df1.plot()
df2.plot(AX = AX)
但如果我只是想绘制哪里是什么,他们交叠?
假设df1索引是跨越24小时的时间戳,而df2索引也是在df1的24小时内跨越12小时的时间戳(但与df1不完全相同)。
如果我只想绘制两个数据帧覆盖的12小时。什么是简单的方法来做到这一点?
有多种方式来实现这一目标。下面的代码片段显示了两个例子。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Make up time data for 24 hour period and 12 hour period
times1 = pd.date_range('1/30/2016 12:00:00', '1/31/2016 12:00:00', freq='H')
times2 = pd.date_range('1/30/2016 12:00:00', '1/31/2016 00:00:00', freq='H')
# Put time into DataFrame
df1 = pd.DataFrame(np.cumsum(np.random.randn(times1.size)), columns=['24 hrs'],
index=times1)
df2 = pd.DataFrame(np.cumsum(np.random.randn(times2.size)), columns=['12 hrs'],
index=times2)
# Method 1: Filter first dataframe according to second dataframe's time index
fig1, ax1 = plt.subplots()
df1.loc[times2].plot(ax=ax1)
df2.plot(ax=ax1)
# Method 2: Set the x limits of the axis
fig2, ax2 = plt.subplots()
df1.plot(ax=ax2)
df2.plot(ax=ax2)
ax2.set_xlim(times2.min(), times2.max())
一般的回答一般问题:
你有三个选择:
xlim
keyword。要绘制DF1,其指数位于DF2的索引范围内的只有一部分,你可以做这样的事情:
AX = df1.loc [df2.index。 min():df2.index.max()]。plot()
可能还有其他方法可以做到这一点,但这是我首先想到的。
祝你好运!