为 hist2d 子图添加一个颜色条并使它们相邻 [英] Adding one colorbar for hist2d subplots and make them adjacent

查看:54
本文介绍了为 hist2d 子图添加一个颜色条并使它们相邻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力调整情节,我一直在努力.我面临两个问题:

  1. 图应该是相邻的,并且有 0 个 wspace 和 hspace.我将两个值都设置为零,但图之间仍然有一些空格.
  2. 我希望所有子图都有一个颜色条(它们的范围都相同).现在,代码向最后一个子图添加了一个颜色条,因为我知道它需要 hist2D 的第三个返回值.

这是我目前的代码:

def plot_panel(pannel_plot):图, ax = plt.subplots(3, 2, figsize=(7, 7), gridspec_kw={'hspace': 0.0, 'wspace': 0.0}, sharex=True, sharey=True)fig.subplots_adjust(wspace=0.0)ax = ax.flatten()最小 = 0ymin = 0xmax = 0.19ymax = 0.19hist2_num = 0h =[]对于 i, j in zip(pannel_plot['x'].values(), pannel_plot['y'].values()):h = ax[hist2_num].hist2d(i, j, bins=50, norm=LogNorm(vmin=1, vmax=5000), range=[[xmin, xmax], [ymin, ymax]])ax[hist2_num].set_aspect('equal', 'box')ax[hist2_num].tick_params(axis='both', top=False, bottom=True, left=True, right=False,标签大小=10,方向=in")ax[hist2_num].set_xticks(np.arange(xmin, xmax, 0.07))ax[hist2_num].set_yticks(np.arange(ymin,ymax,0.07))hist2_num += 1fig.colorbar(h[3],orientation='vertical',fraction=.1)plt.show()

以及相应的结果:

data = np.vstack([np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)])从 mpl_toolkits.axes_grid1 导入 ImageGridfig = plt.figure(figsize=(4, 6))grid = ImageGrid(fig, 111, # 类似于 subplot(111)nrows_ncols=(3, 2), # 创建 2x2 轴网格axis_pad=0.1, # 以英寸为单位的轴之间的填充.cbar_mode="单",cbar_location="顶部",cbar_pad=0.1)对于网格中的斧头:h = ax.hist2d(data[:, 0], data[:, 1], bins=100)fig.colorbar(h[3], cax=grid.cbar_axes[0],orientation='horizo​​ntal')grid.cbar_axes[0].xaxis.set_ticks_position('top')

I am struggling with tweaking a plot, I have been working on. I am facing to two problems:

  1. The plots should be adjacent and with 0 wspace and hspace. I set both values to zero but still there are some spaces between the plots.
  2. I would like to have one colorbar for all the subplots (they all the same range). Right now, the code adds a colorbar to the last subplot as i understand that it needs the third return value of hist2D.

Here is my code so far:

def plot_panel(pannel_plot):
fig, ax = plt.subplots(3, 2, figsize=(7, 7), gridspec_kw={'hspace': 0.0, 'wspace': 0.0}, sharex=True, sharey=True)
fig.subplots_adjust(wspace=0.0)
ax = ax.flatten()
xmin = 0
ymin = 0
xmax = 0.19
ymax = 0.19
hist2_num = 0
h =[]
for i, j in zip(pannel_plot['x'].values(), pannel_plot['y'].values()):
    h = ax[hist2_num].hist2d(i, j, bins=50, norm=LogNorm(vmin=1, vmax=5000), range=[[xmin, xmax], [ymin, ymax]])
    ax[hist2_num].set_aspect('equal', 'box')
    ax[hist2_num].tick_params(axis='both', top=False, bottom=True, left=True, right=False,
                              labelsize=10, direction='in')
    ax[hist2_num].set_xticks(np.arange(xmin, xmax, 0.07))
    ax[hist2_num].set_yticks(np.arange(ymin, ymax, 0.07))
    hist2_num += 1

fig.colorbar(h[3], orientation='vertical', fraction=.1)
plt.show()

And the corrsiponding result:

Result

I would be glad for any heads up that i am missing!

解决方案

You can use ImageGrid, which was designed to make this kind of things easier

data = np.vstack([
    np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
    np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])


from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure(figsize=(4, 6))
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(3, 2),  # creates 2x2 grid of axes
                 axes_pad=0.1,  # pad between axes in inch.
                 cbar_mode="single",
                 cbar_location="right",
                 cbar_pad=0.1
                )
for ax in grid:
    h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='vertical')

or

data = np.vstack([
    np.random.multivariate_normal([10, 10], [[3, 2], [2, 3]], size=100000),
    np.random.multivariate_normal([30, 20], [[2, 3], [1, 3]], size=1000)
])


from mpl_toolkits.axes_grid1 import ImageGrid

fig = plt.figure(figsize=(4, 6))
grid = ImageGrid(fig, 111,  # similar to subplot(111)
                 nrows_ncols=(3, 2),  # creates 2x2 grid of axes
                 axes_pad=0.1,  # pad between axes in inch.
                 cbar_mode="single",
                 cbar_location="top",
                 cbar_pad=0.1
                )
for ax in grid:
    h = ax.hist2d(data[:, 0], data[:, 1], bins=100)
fig.colorbar(h[3], cax=grid.cbar_axes[0], orientation='horizontal')
grid.cbar_axes[0].xaxis.set_ticks_position('top')

这篇关于为 hist2d 子图添加一个颜色条并使它们相邻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆