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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45467320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:03:06  来源:igfitidea点击:

plot multiple pandas dataframes in one graph

pythonpandasmatplotlibdataframe

提问by eliza.b

I have created 6 different dataframes that eliminate the outliers of their own original data frames. Now, I'm trying to plot all of the dataframes that eliminate the outliers on the same graph.

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

This is my code that eliminates the outliers in each data frame:

这是我的代码,用于消除每个数据框中的异常值:

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')

If I remove the comment newdf.plot()I will be able to plot all of the graphs separately but I want them all on one graph.

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

And yes, I've already read over http://matplotlib.org/examples/pylab_examples/subplots_demo.htmlbut that link doesn't have any examples with multiple plots in one chart.

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

I have also read this: http://pandas-docs.github.io/pandas-docs-travis/visualization.htmlwhich has some really great information but the examples that have multiple plots in one graph use the same data frame. I have 6 separate dataframes. I've thought of one solution to my problem would be to write all of the dataframes to the same excel file then plot them from excel, but that seems excessive and I don't need this data to be saved to an excel file.

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

My question is this: How can I plot multiple pandas dataframes in the same graph.

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

My graph after following Scott's advice enter image description here

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

enter image description here

在此处输入图片说明

What the graph should more or less look like

图表或多或少应该是什么样子

回答by Scott Boston

You need to use the axparameter in pandas.dataframe.plot.

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

Use on the first df.plot to grab a handle on that axes:

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

ax = newdf.plot() 

then on subsequent plots use the ax parameter.

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

newdf2.plot(ax=ax)
...
newdf5.plot(ax=ax)

回答by durbachit

Am I missing something? Normally, I just do this for multiple dataframes:

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

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()