pythonmatplotlib

How to avoid matplot subplot from overlapping?


Code:

import pandas as pd
import matplotlib.pyplot as plt

maxRow = 23

fig, axes = plt.subplots(maxRow, 1)

for x in range(maxRow):
    axes[x].plot(1,1)

plt.show()

Running it shows all the plot overlapping like this. enter image description here

Can I dynamically space the graphs so they are not overlapping?

Edit 1: Something that looks like this but with 20 more below it. enter image description here


Solution

  • If you want that many plots organized that way on a normal sized screen your only choice is to just make them really small. You can accomplish that by decreasing the dpi and increasing the size so that matplotlib will try to jam it in smaller.

    Here is some example code demonstrating my suggestion:

    import matplotlib.pyplot as plt
    
    maxRow = 23
    fig, axes = plt.subplots(maxRow, 1, dpi=20, figsize=(50, 50))
    for x in range(maxRow):
        axes[x].plot(1, 1)
    plt.show()
    

    Which yields the following plot which doesn't have overlapping plots (but does have very tiny text): image with small but not overlapping plots

    The parameters are tunable so you can adjust until it matches your expectations.


    After a little toying with it I was able to get the most readable version I could using the following code (any bigger and it either wouldn't fit on my screen or would overlap):

    import matplotlib.pyplot as plt
    
    maxRow = 23
    fig, axes = plt.subplots(maxRow, 1, dpi=50, figsize=(20, 30))
    fig.tight_layout()
    for x in range(maxRow):
        axes[x].plot(1, 1)
    plt.show()
    

    Which looks like the following: image with also small but not overlapping plots