[英]Matplotlib (GridSpec) - Subplot axis labels being cut-off? Tried `tight_layout`
I'm using GridSpec to organise subplots. 我正在使用GridSpec来组织子图。 I have a shared
colorbar
for all the plots. 我对所有图都有一个共享的
colorbar
。
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). 在线上的所有建议似乎都指出,
tight_layout()
是解决轴标签被切断的问题的方法,但是,这似乎在这里不起作用(除非它以我不知道的另一种形式出现)。
I have also tried using the rect
parameter of tight_layout
for fig
, plt
, and gs
. 我已经使用也尝试
rect
的参数tight_layout
的fig
, plt
和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. 这将生成下面的图像。
As commented, setting the bottom
parameter to a larger value, eg bottom=0.3
would give you more space to accomodate the axes decorators. 如前所述,将
bottom
参数设置为较大的值(例如, bottom=0.3
将为您提供更多空间来容纳轴装饰器。
In addition it may be useful to make the figure a little bit taller (eg 3 inch instead of 2) in order not to shrink the plots too much. 另外,使图形稍微高一点(例如3英寸而不是2英寸)可能会很有用,以免将绘图缩小得太多。
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. 不幸的是,在jupyter中,
%matplotlib inline
后端始终使用bbox_inches = "tight"
设置来创建其映像。 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")
. 在这里,一个选择是使用
fig.add_axes([0,0,1,1]).axis("off")
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.