Dan
Dan

Reputation: 45

Plotting Multiple Contour Plots in same window

I want to plot multiple contour plots in the same window and I so far have plt.subplots(4, 2, constrained_layout=True). After looking through some documentation, I found out that I have to basically loop through the subplots and plot the graph so how do I do this i.e how do I loop through the subplots? If there is another way of plotting multiple contour plots, please let me know.

Upvotes: 1

Views: 213

Answers (1)

Anirban Saha
Anirban Saha

Reputation: 1780

As an example in this code I am plotting in many subplots:

fig, axs = plt.subplots(ncols=cols, nrows=rows, figsize=(16,20), sharex=False)

# Adding some distance between plots
plt.subplots_adjust(hspace = 0.3)

# Plots counter
i=0
for r in np.arange(0, rows, 1):
    for c in np.arange(0, cols, 1):
        if i >= len(columns): # If there is no more data columns to make plots from
            axs[r, c].set_visible(False) # Hiding axes so there will be clean background
        else:
            # Train data histogram
            hist1 = axs[r, c].hist(train[columns[i]].values,
                                   range=(df[columns[i]].min(),
                                          df[columns[i]].max()),
                                   bins=40,
                                   color="deepskyblue",
                                   edgecolor="black",
                                   alpha=0.7,
                                   label="Train Dataset")
            # Test data histogram
            hist2 = axs[r, c].hist(test[columns[i]].values,
                                   range=(df[columns[i]].min(),
                                          df[columns[i]].max()),
                                   bins=40,
                                   color="palevioletred",
                                   edgecolor="black",
                                   alpha=0.7,
                                   label="Test Dataset")
            axs[r, c].set_title(columns[i], fontsize=14, pad=5)
            axs[r, c].tick_params(axis="y", labelsize=13)
            axs[r, c].tick_params(axis="x", labelsize=13)
            axs[r, c].grid(axis="y")
            axs[r, c].legend(fontsize=13)
                                  
        i+=1

plt.show();

You get the axes using axs[r, c] and plot it inside that.

Upvotes: 0

Related Questions