Categories
Python Answers

How to improve subplot size/spacing with many subplots in Python matplotlib?

Spread the love

To improve subplot size/spacing with many subplots in Python matplotlib, we can use the subplots_adjust method.

For instance, we write

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as tic

fig = plt.figure()

x = np.arange(100)
y = 3.*np.sin(x*2.*np.pi/100.)

for i in range(5):
    temp = 510 + i
    ax = plt.subplot(temp)
    plt.plot(x,y)
    plt.subplots_adjust(hspace = .001)
    temp = tic.MaxNLocator(3)
    ax.yaxis.set_major_locator(temp)
    ax.set_xticklabels(())
    ax.title.set_visible(False)

plt.show()

to call plt.subplots_adjust with the hspace argument to adjust the horizontal spacing.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *