在一张图中绘制多个熊猫数据框

eli*_*a.b 2 python matplotlib dataframe pandas

我创建了6个不同的数据框,以消除其原始数据框的异常值。现在,我试图在同一图形上绘制所有消除异常值的数据框。

这是我的代码,消除了每个数据帧中的异常值:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.style.use("ggplot")

#---Original DataFrame
x = (g[0].time[:27236])
y = (g[0].data.f[:27236])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf.plot('Time', 'Data')

#---Original DataFrame
x = (q[0].time[:47374])
y = (q[0].data.f[:47374])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf2 = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf2.plot('Time', 'Data')

#---Original DataFrame
x = (w[0].time[:25504])
y = (w[0].data.f[:25504])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf3 = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf3.plot('Time', 'Data')

#---Original DataFrame
x = (e[0].time[:47172])
y = (e[0].data.f[:47172])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf4 = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf4.plot('Time', 'Data')

#---Original DataFrame
x = (r[0].time[:21317])
y = (r[0].data.f[:21317])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf5 = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf5.plot('Time', 'Data')

#---Original DataFrame
x = (t[0].time[:47211])
y = (t[0].data.f[:47211])
df = pd.DataFrame({'Time': x, 'Data': y})

#----Removes the outliers in a given DataFrame and plots a graph
newdf = df.copy()
newdf6 = df[~df.groupby('Data').transform( lambda x: abs(x-x.mean()) > 1.96*x.std()).values]
#newdf6.plot('Time', 'Data')
Run Code Online (Sandbox Code Playgroud)

如果删除注释,newdf.plot()我将能够分别绘制所有图形,但我希望将它们全部绘制在一个图形上。

是的,我已经阅读了http://matplotlib.org/examples/pylab_examples/subplots_demo.html, 但是该链接没有任何示例在一个图表中包含多个图表。

我也读过这篇文章:http : //pandas-docs.github.io/pandas-docs-travis/visualization.html,它具有一些非常好的信息,但是在一个图形中包含多个图的示例使用相同的数据框。我有6个单独的数据框。我想到了解决我的问题的一种方法,就是将所有数据框都写入相同的excel文件,然后从excel中绘制它们,但这似乎过多了,我不需要将这些数据保存到excel文件中。

我的问题是:如何在同一张图中绘制多个熊猫数据框。

在遵循Scott的建议后我的图表 在此处输入图片说明

在此处输入图片说明

该图应该多少看起来像

dur*_*hit 19

我错过了什么吗?通常,我只是对多个数据帧执行此操作:

fig = plt.figure()

for frame in [newdf, newdf2, newdf3, newdf4, newdf5]:
    plt.plot(frame['Time'], frame['Data'])

plt.xlim(0,18000)
plt.ylim(0,30)
plt.show()
Run Code Online (Sandbox Code Playgroud)


Sco*_*ton 7

您需要使用axpandas.dataframe.plot中的参数。

在第一个df.plot上使用,以抓住该轴上的手柄:

ax = newdf.plot() 
Run Code Online (Sandbox Code Playgroud)

然后在随后的绘图上使用ax参数。

newdf2.plot(ax=ax)
...
newdf5.plot(ax=ax)
Run Code Online (Sandbox Code Playgroud)