0
votes

I am trying to plot 5 subplots in Python with differing sizes and orientation. I have seen usage of gridspec to alter column width, plt.figure(figsize=[]), etc. but each seems to be slightly off from what I want (e.g. no uniform column width, all one plot, so can't use plt.figure()).

What I currently have is a subplot(3,2,2) with the first 5 plots filled such that it looks like a 3x2 grid without the bottom right filled in. What I would like is a 2x2 grid with a single plot below it. The plot below should also be larger (maybe twice as wide) as the four above it.

This is what I have versus what I would like.

Here is my code (sorry for the line numbers):

plot3 = plt.figure(1)
plt.subplot(321)
#gs = gridspec.GridSpec(1,2,3,4,5,6,width_ratios=[1,1,1,1,2,0])
plt.scatter(0.001*posEncUm[:,0],err[:,0], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,0],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (X), mm', fontsize=7)
plt.ylabel('Laser Error (X), um', fontsize=7)
plt.subplots_adjust(hspace = 1.0, wspace = 0.5)
plt.grid()

plt.subplot(322)
plt.scatter(0.001*posEncUm[:,1],err[:,0], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,1],err[:,0],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (Y), mm', fontsize=7)
plt.ylabel('Laser Error (X), um', fontsize=7)
plt.grid()

plt.subplot(323)
plt.scatter(0.001*posEncUm[:,0],err[:,1], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,0],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (X), mm', fontsize=7)
plt.ylabel('Laser Error (Y), um', fontsize=7)
plt.grid()

plt.subplot(324)
plt.scatter(0.001*posEncUm[:,1],err[:,1], s=1, linewidths=1)
p = np.polyfit(0.001*posEncUm[:,1],err[:,1],1)
plt.title('Slope = {0:4.1f} um/100mm'.format(p[0]*100), fontsize=10)
plt.xlabel('Encoder Position (Y), mm', fontsize=7)
plt.ylabel('Laser Error (Y), um', fontsize=7)
plt.grid()

plt.subplot(325)
plt.quiver(0.001*X,0.001*Y,errX,errY)
plt.grid()
plt.xlabel('Encoder Pos (X), mm')
plt.ylabel('Encoder Pos (Y), mm')
plt.gca().set_aspect('equal', adjustable = 'box')
2

2 Answers

1
votes

This will give what you want, just modify for the specifics. Is this what you were thinking?

import pylab as pl

fig = pl.figure(figsize=(3.25, 4.5))
gs = pl.GridSpec(3, 2)

gs.update(left=0.08, right=0.925,
          top=0.95, bottom=0.05,
          hspace=0.3, wspace=0.1)

# create primary axes
ax0 = pl.subplot(gs[0, 0])
ax1 = pl.subplot(gs[0, 1]) 
ax2 = pl.subplot(gs[1, 0]) 
ax3 = pl.subplot(gs[1, 1])
ax4 = pl.subplot(gs[2, :])

I don't know what you mean by "bottom should be twice as wide as four above it."

0
votes
#https://python-graph-gallery.com/125-small-multiples-for-line-chart/ 

import matplotlib.gridspec as gridspec 
import matplotlib.pyplot as plt


# Initialize the figure
plt.style.use('seaborn-darkgrid')
# create a color palette
palette = plt.get_cmap('Set1')
plt.figure(figsize=(10,10))
plt.suptitle("PLOT TITLE",fontsize=20)
gridspec.GridSpec(3,3)
plt.subplots_adjust(hspace=0.4)

# multiple line plot
num=0
for column in df.drop('ODD COLUMN NAME', axis=1):
    num+=1
    # Find the right spot on the plot

    if num==7:  # adjustment to fit ODD COLUMN
        plt.subplot2grid((3,3),(2,0),colspan=3)
    else:
        plt.subplot(3,3, num)

    # plot every groups, but discreet
    for v in df.drop('ODD COLUMN', axis=1):
        plt.plot(df['ODD COLUMN'], df[v], marker='', color='grey', linewidth=0.6, alpha=0.3)
    # Plot the lineplot
    plt.plot(df['ODD COLUMN'], df[column], marker='', color=palette(num), linewidth=2.4, alpha=0.9, label=column)
    # Same limits for everybody!
    plt.xlim(10,100)
    plt.ylim(1,100)
    # Not ticks everywhere
    if num in range(4) :
        plt.tick_params(labelbottom='off')
    if num not in [1,4,7] :
        plt.tick_params(labelleft='off')
    # Add title
    plt.title(column, loc='left', fontsize=12, fontweight=0, color=palette(num))