2

I'am trying to create two plots - one under another with seaborn!
My code:

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(22,8))
p1 = sns.relplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax1)
p2 = sns.relplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax2)

But this creates 4 axes instead of 2! Look:

enter image description here

I give up getting read of these extra 2 axeses - need help.
Here's code to create data:

df ={'label': {0: 'top_5',
  1: 'first_page',
  2: 'win_ratecard',
  4: 'switched_off',
  5: 'top_5',
  6: 'first_page',
  7: 'win_ratecard',
  9: 'switched_off',
  10: 'top_5',
  11: 'first_page'},
 'report_date': {0: Timestamp('2018-08-21 00:00:00'),
  1: Timestamp('2018-08-21 00:00:00'),
  2: Timestamp('2018-08-21 00:00:00'),
  4: Timestamp('2018-08-22 00:00:00'),
  5: Timestamp('2018-08-22 00:00:00'),
  6: Timestamp('2018-08-22 00:00:00'),
  7: Timestamp('2018-08-22 00:00:00'),
  9: Timestamp('2018-08-23 00:00:00'),
  10: Timestamp('2018-08-23 00:00:00'),
  11: Timestamp('2018-08-23 00:00:00')},
 'sns_codes': {0: 0, 1: 0, 2: 0, 4: 1, 5: 1, 6: 1, 7: 1, 9: 2, 10: 2, 11: 2},
 'triad_quantity': {0: 9,
  1: 204,
  2: 214,
  4: 20,
  5: 5,
  6: 191,
  7: 230,
  9: 21,
  10: 2,
  11: 98}}
 data_2 = pd.DataFrame(df)
Ladenkov Vladislav
  • 1,247
  • 2
  • 21
  • 45
  • 1
    Why are you doing that? `relplot` is a figure-level function, it will create its own `Figure` instance and potentially several subplots. What's the point in asking it to plot to a particular axes? It doesn't seem to make sense. – Stop harming Monica Aug 28 '18 at 11:29
  • Does this answer your question? [seaborn is not plotting within defined subplots](https://stackoverflow.com/questions/63895392/seaborn-is-not-plotting-within-defined-subplots) – Trenton McKinney Nov 29 '21 at 16:01
  • The existing solutions on this page no longer work. sns.relplot is a figure-level plot. See the previous linked duplicate. – Trenton McKinney Nov 29 '21 at 16:01

2 Answers2

3

Below is a possible solution to get rid of the additional unwanted empty plots. The problem was that when you call sns.relplot, relplot returns a class:FacetGrid object. This can be seen here. But since you pass ax1 and ax2 for plotting, these FacetGrids which are assigned the variables p1 and p2 appear as blank plots. To get rid of these just add the following lines

plt.close(p1.fig)
plt.close(p2.fig) 
Sheldore
  • 37,862
  • 7
  • 57
  • 71
3

relplot is a figure-level function, so it will create a figure. If you want to put your lineplots in existing matplotlib axes, without creating the extraneous figures, use seaborn's lineplot function, which is an axes-level function:

fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(22,8))
p1 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax1)
p2 = sns.lineplot(x="sns_codes", y="triad_quantity", hue="label", data=data_2, kind="line", ax=ax2)

The two plots you've given as an example seem to do the same thing, but if you're trying to do multiple plots that vary along some dimension represented as a column in your dataframe, you can don't have to specify the subplots yourself. You can use seaborn to do this using sns.replot, with a row (facet) parameter specifying row="a_column_on_which_your_plots_vary". See the seaborn tutorial for an illustration.

EEE
  • 501
  • 6
  • 13