Plot line charts on same axis

0 votes
asked Sep 11, 2017 by ross-demtschyna

I want to plot all my line plots on the same chart with the same axis. Instead i get several plots. How do you overlay each plot, x axis is time and y is WAP? I have spent several days on this and asked the question several times...if anybody knows the answer...pls share.

grouped = df2.groupby('date')
print(grouped)

grouped.plot(x='time', y='WAP', kind='line')
plt.show()

Plot day1 Plot day2

2 Answers

0 votes
answered Sep 11, 2017 by arda-arslan

To plot two dataframes on one figure, you can do the following:

import random
import pandas as pd
import matplotlib.pyplot as plt

# Create some y vals, here we'll create 10 between 1 and 100 (exclusive)
y1 = random.sample(range(1, 100), 10)
y2 = random.sample(range(1, 100), 10)

# Create dataframes of y vals
df1 = pd.DataFrame(data={'yvals1': y1})
df2 = pd.DataFrame(data={'yvals2': y2})

# Create matplotlib.axes object so df2 can share df1's axis
df1_ax = df1.plot()
df2.plot(ax=df1_ax)

plt.show()

This outputs something like: example output

0 votes
answered Sep 11, 2017 by ross-demtschyna

This worked - thankyou for your help.

fig, ax = plt.subplots(1,1)
df2.groupby("date").plot(x="time", y="WAP", ax=ax)
plt.show()
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...