0

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

tjt
  • 620
  • 2
  • 7
  • 17
  • 1
    `relplot` is a figure-level function. It does not take an axes as input. Use `scatterplot` instead. – ImportanceOfBeingErnest Aug 09 '19 at 19:43
  • thanks @ImportanceOfBeingErnest that worked. can you please elaborate if possible on the differences between figure level and axes level – tjt Aug 09 '19 at 20:30
  • 1
    See https://stackoverflow.com/questions/23969619/plotting-with-seaborn-using-the-matplotlib-object-oriented-interface also https://seaborn.pydata.org/introduction.html#figure-level-and-axes-level-functions finally https://stackoverflow.com/questions/54959764/seaborn-factorplot-generates-extra-empty-plots-below-actual-plot – ImportanceOfBeingErnest Aug 09 '19 at 20:38
  • thanks @ImportanceOfBeingErnest is there any easy way to know if a plot is axes level or fig level – tjt Aug 09 '19 at 22:22
  • 1
    Unfortunately, not. In some cases the documentation mentions it though. E.g. in the [relplot doc](https://seaborn.pydata.org/generated/seaborn.relplot.html#seaborn.relplot) it says *"Figure-level interface for drawing relational plots onto a FacetGrid."* – ImportanceOfBeingErnest Aug 09 '19 at 22:54
  • @ImportanceOfBeingErnest thank you. IS there any easy way to start learning about all this ? the various attributes, various plots and how they should be structured, differences and relations between matplotlib and seaborn. I am familiar with python code, but haven't delved much into the visualizations so far and starting to learn them now. – tjt Aug 09 '19 at 22:57
  • Actually in the second link from my second comment it says *"The way you can tell whether a function is “figure-level” or “axes-level” is whether it takes an ax= parameter. You can also distinguish the two classes by their output type: axes-level functions return the matplotlib axes, while figure-level functions return the FacetGrid."* – ImportanceOfBeingErnest Aug 09 '19 at 22:59

3 Answers3

4

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)
Sahil Tah
  • 300
  • 1
  • 3
  • 9
1

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);
  • what @Importance said above worked. even with the ax option, it didn't work with rel – tjt Aug 09 '19 at 20:31
0
#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()
Arpith S
  • 111
  • 4
  • 15
  • Works for normal plotting with `matplotlib` but not when using `seaborn`. – BenT Aug 09 '19 at 19:17
  • @ArpithS can you please explain what fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(5, 3)) returns and the usage, does it return a figure and an axis ? – tjt Aug 09 '19 at 20:32
  • @BenT what's the difference ? why doesn't it work with seaborn, can you please elaborate – tjt Aug 09 '19 at 21:52