-1

I'm doing some EDA on a dataset I have using seaborn primarily. However, I'd like to plot these graphs in a single kernel. I think I'm meant to use matplotlib to achieve this. I've done 3 separate sns.countplot graphs, but I'm trying to show them in one single kernel/output.

I've tried using the following code but I'm still not entirely sure how it works:

fig, axes = plt.subplots(1, 3, figsize=(16,8))

ax = sns.countplot(y = 'loan_status', data = df, order = df['loan_status'].value_counts().iloc[:6].index)

ax = sns.countplot(y = 'loan_status', data = df, order = df['loan_status'].value_counts().iloc[2:9].index)

ax = sns.distplot(df['loan_amnt'], bins=50)
some_programmer
  • 3,268
  • 4
  • 24
  • 59
softmax55
  • 578
  • 2
  • 7
  • 21
  • Please include minimal code for the plots you are trying to make so that it is easier for posters to test your question. – Todd Burus Aug 03 '19 at 14:06
  • Possible duplicate of [How to make several plots on a single page using matplotlib?](https://stackoverflow.com/questions/1358977/how-to-make-several-plots-on-a-single-page-using-matplotlib) – Mihai Chelaru Aug 03 '19 at 14:10
  • Note, if you only want three plots, then you can use `fig, axes = plt.subplots(1, 3, figsize=(16,8))` or `fig, axes = plt.subplots(3, 1, figsize=(16,8))` depending on if you'd like them laid out horizontally or vertically, respectively. Again, having the code you'd like to plot would be helpful here. – Todd Burus Aug 03 '19 at 14:14
  • sorry for not posting the code, i've just added – softmax55 Aug 03 '19 at 14:19
  • Okay. So, is your code working and you just want to know how it works, or is it not working at all? Did you include a `plt.show()` command? – Todd Burus Aug 03 '19 at 14:21
  • Hey Todd, the code works but it doesn't return three separate graphs. instead, it returns one really incorrect graph. If I run each line separately then they work fine. I'm just trying to get all three graphs in one window – softmax55 Aug 03 '19 at 14:26
  • Change your fig, axes call to `fig, [ax1, ax2, ax3] = plt.subplots(1, 3. figsize(16,8))` and then rename each individual plot with ax1, ax2, ax3. Does that help? – Todd Burus Aug 03 '19 at 14:33
  • Please format the code - select it and type `ctrl-k`: [Formatting posts](https://stackoverflow.com/help/formatting) ... [Formatting help](https://stackoverflow.com/editing-help) – wwii Aug 03 '19 at 14:59
  • If you'ld like to try with Matplotlib, browse throught the [gallery](https://matplotlib.org/gallery/index.html#subplots-axes-and-figures) to find the features you want then look at the example code to see how it was accomplished. – wwii Aug 03 '19 at 15:02

2 Answers2

2

Do you mean something like this?

enter image description here

Here is a simple example:

import numpy as np
import matplotlib.pyplot as plt

# Some random data to plot
M = np.random.rand(3,100,100)

fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(16,8))

for i, ax in enumerate(axes.flatten()):
    ax.imshow(M[i])

# OR
# axes[0].imshow(M[0])
# axes[1].imshow(M[1])
# axes[2].imshow(M[2])

plt.show()
Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
1

Try this.

fig, [ax1, ax2, ax3] = plt.subplots(1, 3, figsize=(16,8))

     ax1 = sns.countplot(y = 'loan_status', data = df, order = df['loan_status'].value_counts().iloc[:6].index)

     ax2 = sns.countplot(y = 'loan_status', data = df, order = df['loan_status'].value_counts().iloc[2:9].index)

     ax3 = sns.distplot(df['loan_amnt'], bins=50)

fig.tight_layout()

plt.show()
Todd Burus
  • 963
  • 1
  • 6
  • 20
  • No problem. Please remember to participate in the community by marking the most helpful solution to your question. – Todd Burus Aug 05 '19 at 04:19