28

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.

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.

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

I have also read this: http://pandas-docs.github.io/pandas-docs-travis/visualization.html which 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.

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

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

enter image description here

What the graph should more or less look like

4 Answers 4

48

You need to use the ax parameter in pandas.dataframe.plot.

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

ax = newdf.plot() 

then on subsequent plots use the ax parameter.

newdf2.plot(ax=ax)
...
newdf5.plot(ax=ax)
Sign up to request clarification or add additional context in comments.

3 Comments

That sort of worked. It put all of my plots into one graph but it jumbled up all of my data. I'll post a jpg of what that made my data look like and what it should look like in my original question.
Is all the data the same scale? Maybe it makes sense to use multiple plots or at least multiple y-axis.
Well in the second jpg I posed of what it should look like the data is sharing both the x/y axes. So that is what I want I just don't need to separate the plots like in the example here linkwhere three different plots are sharing both x/y axes. and multiple plots wouldn't work for me because all of this data is under the same parameter and I would like to keep it all together.
29

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

1 Comment

This is very nice. However, sometimes one just do df.plot(x='x', y='y', kind='scatter'), as an example, and that is where, if I understand correct, the question came from.
2

The answer 26 is very good solution. I've tried on my dataframe, if the x column is Date, then need little change, for instance,

              Date    Key  Confirmed   Deaths
14184   2020-02-12  US_TX        1.0      0.0
14596   2020-02-13  US_TX        2.0      0.0
15007   2020-02-14  US_TX        2.0      0.0
15418   2020-02-15  US_TX        2.0      0.0
15823   2020-02-16  US_TX        2.0      0.0
...            ...    ...        ...      ...
270228  2020-11-07  US_TX   950549.0  19002.0
271218  2020-11-08  US_TX   956234.0  19003.0
272208  2020-11-09  US_TX   963019.0  19004.0
273150  2020-11-10  US_TX   973970.0  19004.0
274029  2020-11-11  US_TX   985380.0  19544.0
              Date    Key  Confirmed   Deaths
21969   2020-03-01  US_NY        1.0      0.0
22482   2020-03-02  US_NY        1.0      0.0
23014   2020-03-03  US_NY        2.0      0.0
23555   2020-03-04  US_NY       11.0      0.0
24099   2020-03-05  US_NY       22.0      0.0
...            ...    ...        ...      ...
270218  2020-11-07  US_NY   530354.0  33287.0
271208  2020-11-08  US_NY   533784.0  33314.0
272198  2020-11-09  US_NY   536933.0  33343.0
273140  2020-11-10  US_NY   540897.0  33373.0
274019  2020-11-11  US_NY   545718.0  33398.0
import pandas as pd
from matplotlib import pyplot as plt

firstPlot = firstDataframe.plot(x='Date') # where the 'Date' is the column with date.

secondDataframe.plot(x='Date', ax=firstPlot)

...
plt.show()

Comments

0

You can use the ax and fig, like this:

from matplotlib import pyplot as plt
fig, ax = plt.subplots()
df1.plot(ax=ax)
df2.plot(ax=ax)
fig.show()
fig.savefig("plot_of_df")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.