简体   繁体   中英

Scatter plot with subplot in seaborn

I am new to python visualizations. I am trying to use draw two scatter plots side by side using the follow code, but couldn't.

Also, can someone please provide me some good tutorials for seaborn/matplotlib. I peaked into their documentation and its daunting

plt.figure(figsize = (16, 12))
ax = plt.subplot(1,2,1)
sns.relplot(x="total_bill", y="tip", data=tips, ax= ax);
ax = plt.subplot(1,2,2)
sns.scatterplot(x="total_bill", y="tip", data=tips);

I get two plots, one above the another. The first plot is of good size, but the second plot below is not of the size as first and has very small x axis length

You didn't specify the ax parameter properly. Give this a try:

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

ax1.set_title('Latitute')
sns.scatterplot(x='price', y='lat', data=df, ax=ax1)

ax2.set_title('Longitude')
sns.scatterplot(x='price', y='long', data=df, ax=ax2)

You seemed to have left out your second ax parameter. Try:

plt.figure(figsize = (16, 12))
ax = plt.subplot(1,2,1)
sns.relplot(x="total_bill", y="tip", data=tips, ax= ax);
ax = plt.subplot(1,2,2)
sns.scatterplot(x="total_bill", y="tip", data=tips, ax= ax);
#Somthing like this should work
import numpy as np
import matplotlib.pyplot as plt

x1 = [1, 2, 3, 4, 5]
x2 = [1, 2, 3, 4, 5]
y1 = [1, 8, 27, 36, 125]
y2 = [1, 4, 9, 16, 25]

fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3))
axes[0].plot(x1, y1)
axes[1].plot(x2, y2)
fig.tight_layout()
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM