我在我的第一个python脚本中使用了它,而且我显然很新,所以请耐心等待。我在这个情节上投入的时间比我要承认的要花更多的时间,以避免在这里问一个问题,但是我没有主意。我已经简化了本文的代码,但问题仍然存在。我需要增加imshow图的间距,以使轴和彩条值清晰可见。显然,实际上所有数据都是不同的,因此需要单独的颜色条比例,并且轴具有负值,因此问题甚至更糟。我曾尝试以多种方式使用wspace = x,但并没有取得真正的成功。
**最上面的代码是借来的,因为开始时颜色条显示不正确或垂直压缩了我的图。
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
def colorbar(mappable):
last_axes = plt.gca()
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
caxr = divider.append_axes("right", size="5%", pad=0.05)
cbar = fig.colorbar(mappable, cax=caxr)
plt.sca(last_axes)
return cbar
np.random.seed(19680801)
grid = np.random.rand(53, 51)
fields = plt.figure(figsize = [15,6])
gs0 = gridspec.GridSpec(1, 2)
gs00 = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[0])
ax0 = plt.Subplot(fields, gs00[:, :])
im0 = ax0.plot(grid[0],grid[1])
ax0.set(xlabel='x', ylabel='y', title='Plot1'),
ax0.grid(),
fields.add_subplot(ax0)
gs01 = gridspec.GridSpecFromSubplotSpec(2, 3, subplot_spec=gs0[1])
ax1 = plt.Subplot(fields, gs01[0, 0])
im1 = ax1.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im1)
ax1.set_title('plot'), ax1.set_xlabel('z'), ax1.set_ylabel('r')
fields.add_subplot(ax1)
ax2 = plt.Subplot(fields, gs01[0, 1])
im2 = ax2.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im2)
ax2.set_title('plot'), ax2.set_xlabel('z'), ax2.set_ylabel('r')
fields.add_subplot(ax2)
ax3 = plt.Subplot(fields, gs01[0, 2])
ax3.set(title='plot', xlabel='z', ylabel = 'r')
im3 = ax3.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im3)
ax3.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax3)
ax4 = plt.Subplot(fields, gs01[1, 0])
im4 = ax4.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im4)
ax4.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax4)
ax5 = plt.Subplot(fields, gs01[1, 1])
im5 = ax5.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im5)
ax5.set(title='$plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax5)
ax6 = plt.Subplot(fields, gs01[1, 2])
im6 = ax6.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im6)
ax6.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax6)
plt.suptitle("GirdSpec Plot")
plt.show()
答案 0 :(得分:0)
在gs01的网格规格中调整了右子图的间距;其次,在gs0的网格规格中将左图的组成比校正为2/5。第三,ax0.set_aspect('equal')
,左边的图形是正方形。
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
def colorbar(mappable):
last_axes = plt.gca()
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
caxr = divider.append_axes("right", size="5%", pad=0.05)
cbar = fig.colorbar(mappable, cax=caxr)
plt.sca(last_axes)
return cbar
np.random.seed(19680801)
grid = np.random.rand(53, 51)
fields = plt.figure(figsize = [15,6])
gs0 = GridSpec(1, 2, width_ratios=(2,5)) # update
gs00 = GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[0])
ax0 = plt.Subplot(fields, gs00[:, :])
im0 = ax0.plot(grid[0],grid[1])
ax0.set(xlabel='x', ylabel='y', title='Plot1')
ax0.grid()
ax0.set_aspect('equal') # update
fields.add_subplot(ax0)
gs01 = GridSpecFromSubplotSpec(2, 3, subplot_spec=gs0[1], wspace=0.8) # update
ax1 = plt.Subplot(fields, gs01[0, 0])
im1 = ax1.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im1)
ax1.set_title('plot'), ax1.set_xlabel('z'), ax1.set_ylabel('r')
fields.add_subplot(ax1)
ax2 = plt.Subplot(fields, gs01[0, 1])
im2 = ax2.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im2)
ax2.set_title('plot'), ax2.set_xlabel('z'), ax2.set_ylabel('r')
fields.add_subplot(ax2)
ax3 = plt.Subplot(fields, gs01[0, 2])
ax3.set(title='plot', xlabel='z', ylabel = 'r')
im3 = ax3.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im3)
ax3.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax3)
ax4 = plt.Subplot(fields, gs01[1, 0])
im4 = ax4.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im4)
ax4.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax4)
ax5 = plt.Subplot(fields, gs01[1, 1])
im5 = ax5.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im5)
ax5.set(title='$plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax5)
ax6 = plt.Subplot(fields, gs01[1, 2])
im6 = ax6.imshow(grid, cmap = 'inferno', interpolation = 'gaussian')
colorbar(im6)
ax6.set(title='plot', xlabel='z', ylabel = 'r')
fields.add_subplot(ax6)
plt.suptitle("GirdSpec Plot")
plt.show()
答案 1 :(得分:0)