3

I need to plot 6 subplots of my data and currently I am having problems with the spacing. My plot needs to have a title with Sample indices on top of each subplot. My problem is that the plot visualization is dificult due to unproper spacing. A reproducible example is below:

import matplotlib.pyplot as pp
import matplotlib.pyplot as plt
from numpy import random
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 1)
    pp.plot(x1, label="Class 1", color='red')
    pp.plot(x2, label="Class 2", color='blue')
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    pp.title("Sample 7")
    
    
    
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 2)
    pp.plot(x1, color='red')
    pp.plot(x2, color='blue')
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    pp.title("Sample 2")
    
    
    
    
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 3)
    pp.plot(x1, color='red')
    pp.plot(x2, color='blue')
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    pp.title("Sample 3")
    
    
    
    
    
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 4)
    pp.plot(x1, color='red')
    pp.plot(x2, color='blue')
    pp.title("Sample 9")
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    
    
    
    
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 5)
    pp.plot(x1, color="red")
    pp.plot(x2, color="blue")
    pp.title("Sample 11")
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    
    
    
    x1 = random.randint(2000, size=(125))
    x2 = random.randint(1000, size=(125))
    plt.subplot(2, 3, 6)
    pp.plot(x1, color="red")
    pp.plot(x2, color="blue")
    pp.title("Sample 13")
    leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
    plt.show() 

I'd like to know what changes do I need to add/make to make the data values axes have proper spacing and be shown without overlapping other subplots and instead of having those empty labels on the bottom of each subplot, replace it with a single label for every subplot on the top outside the plot

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
xnok
  • 299
  • 2
  • 6
  • 35

1 Answers1

1

I think you are forgetting to use tight_layout() to take care of these issues. Just add it before plotting. Copying the last lines of code:

plt.subplot(2, 3, 6)
pp.plot(x1, color="red")
pp.plot(x2, color="blue")
pp.title("Sample 13")
leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)
plt.tight_layout()
plt.show() 

Ouputting:

enter image description here

Celius Stingher
  • 17,835
  • 6
  • 23
  • 53
  • That is great, I thought I would have to re-write the whole thing. Is there a way to get rid of that bubble of empty lables above the letters 'pl' in the subplots? – xnok Aug 14 '22 at 22:44
  • Sure, just remove this part of the code for each subplot except the first one: `leg = pp.legend(loc='lower center', bbox_to_anchor=[0.5, 1.1], ncol=2)` – Celius Stingher Aug 14 '22 at 22:47
  • Thank so much! I am not sure if this outside the scope of the question, but is there any reasonable way to add the class legends anywhere properly without shrinking the subplots? – xnok Aug 15 '22 at 00:06
  • 1
    You can take the legend out of the layout with `plt.legend(...).set_in_layout(False)`. You might also try `fig = plt.figure(layout='constrained')` rather than relying on tight_layout. – Jody Klymak Aug 15 '22 at 00:26