Reputation: 334
I'm using GridSpec to organise subplots. I have a shared colorbar
for all the plots.
All suggestions online seem to point out that tight_layout()
is the way to fix issues with axis labels cutting off, however this doesn't seem to be working here (unless it comes in another form which I am unaware of).
I have also tried using the rect
parameter of tight_layout
for fig
, plt
, and gs
.
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
import matplotlib.gridspec as gridspec
import matplotlib.colors
from mpl_toolkits.mplot3d import Axes3D
gs = gridspec.GridSpec(1,7,hspace=0.05,wspace=0.5, width_ratios=[1,1,1,1,1,1,0.1])
figure(num=None, figsize=(18, 2), dpi=80, facecolor='w', edgecolor='k')
data = np.random.rand(3,6,224,5)
for i in range(6):
ax = plt.subplot(gs[0, i], projection='3d')
p = ax.scatter(data[0,i,:,0], data[0,i,:,1], data[0,i,:,2], c=data[0,i,:,4], marker='o')
title("Case " + str(i+1))
ax.set_xlabel('Batch Size', linespacing=3)
ax.set_ylabel('Window Size', linespacing=3)
ax.set_zlabel('Neurons', linespacing=3)
ax.xaxis.labelpad=20
ax.yaxis.labelpad=20
ax.zaxis.labelpad=10
cbar = plt.subplot(gs[0,6])
colorbar(p, cax=cbar, label='RMSE')
plt.show()
This generates the image below.
Upvotes: 3
Views: 4362
Reputation: 339430
As commented, setting the bottom
parameter to a larger value, e.g. bottom=0.3
would give you more space to accomodate the axes decorators.
In addition it may be useful to make the figure a little bit taller (e.g. 3 inch instead of 2) in order not to shrink the plots too much.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
from mpl_toolkits.mplot3d import Axes3D
gs = gridspec.GridSpec(1,7,hspace=0.05,wspace=0.5, bottom=0.3,
left=0.02, right=0.95, width_ratios=[1,1,1,1,1,1,0.1])
fig = plt.figure(figsize=(18, 3), dpi=80, facecolor='w', edgecolor='k')
data = np.random.rand(3,6,224,5)
for i in range(6):
ax = plt.subplot(gs[0, i], projection='3d')
p = ax.scatter(data[0,i,:,0], data[0,i,:,1], data[0,i,:,2],
c=data[0,i,:,4], marker='o')
ax.set_title("Case " + str(i+1))
ax.set_xlabel('Batch Size', linespacing=3)
ax.set_ylabel('Window Size', linespacing=3)
ax.set_zlabel('Neurons', linespacing=3)
ax.xaxis.labelpad=20
ax.yaxis.labelpad=20
ax.zaxis.labelpad=10
cbar = plt.subplot(gs[0,6])
fig.colorbar(p, cax=cbar, label='RMSE')
# This is only needed for jupyter
fig.add_axes([0,0,1,1]).axis("off")
plt.show()
Unfortunately in jupyter the %matplotlib inline
backend always creates its images with a the bbox_inches = "tight"
setting. So a workaround is to create some element in the figure that ensures that the "tight" area is large enough. Here, an option is to use fig.add_axes([0,0,1,1]).axis("off")
.
Upvotes: 3