VGB
VGB

Reputation: 497

Plots different columns of different dataframe in one plot as scatter plot

I am trying to plot different columns (longitude & latitude ) from different dataframes in one plot. But they are being plotted in different figures separately.

Here is the code I am using

fig,ax=plt.subplots()

cells_final.plot.scatter(x='lon',y='lat')
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red')
plt.show()

How can I plot this in one single figure?

Upvotes: 1

Views: 674

Answers (3)

VGB
VGB

Reputation: 497

Output

Thanks @William Miller.......!

Upvotes: 0

William Miller
William Miller

Reputation: 10320

Use the axes instance (ax) created by

fig, ax = plt.subplots()

And pass it as the ax parameter of pandas.DataFrame.plot,

fig,ax=plt.subplots()

cells_final.plot.scatter(x='lon',y='lat', ax=ax)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax)
plt.show()

Or if you'd rather have the plots on different subplots in the same figure you can create multiple axes

fig, (ax1, ax2) = plt.subplots(1, 2)

cells_final.plot.scatter(x='lon',y='lat', ax=ax1)
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax2)
plt.show()

Upvotes: 1

YOLO
YOLO

Reputation: 21709

You need specify the axis:

fig,ax=plt.subplots(1,2, figsize=(12, 8))

cells_final.plot.scatter(x='lon',y='lat', ax=ax=[0])
data_rupture.plot.scatter(x='Longitude',y='Latitude',color='red', ax=ax[1])
plt.show()

Upvotes: 0

Related Questions