Matplotlib colorbars 多个 plt.imshow 图
Matplotlib colorbars multiple plt.imshow plots
我正在尝试制作一个大的概览图。为此,我使用 plt.imshow
绘制不同密度的矩阵。问题是颜色条不想和我一起工作。如图所示。所以现在我有两个问题。
- 如何确保所有图(相同颜色)具有相同的颜色条范围?
- 如何在最后两个coloms的中间获得第二个colorbar。
我当前的代码是(要测试你可以将 eval(data)
更改为随机矩阵):
a = 5
b = 4
fig, axs = plt.subplots(a, b , figsize=(8.27, 11.69), tight_layout=True, sharey=True, sharex=True, gridspec_kw = {'wspace':0, 'hspace':0})
data = ['map_sm_', 'map_dens_']
gasf = ['fg10', 'fg20', 'fg30', 'fg50', 'fg70']
dire = ['x', 'z', 'x', 'z']
cm = ['hot', 'viridis']
for col in range(b):
for row in range(a):
ax = axs[row, col]
if col ==0:
ax.set_ylabel('x (kpc)', fontsize=13)
if col<2:
if col == 0:
data = 'map_sm_' + gasf[row] + dire[col]
else:
data = 'map_sm_' + gasf[row] + dire[col]
pcm = ax.imshow(eval(data), norm=mpl.colors.LogNorm(), cmap=cm[0], interpolation='none', extent=[-17.5,17.5,-17.5,17.5])
else:
if col == 2:
data = 'map_dens_' + gasf[row] + dire[col]
else:
data = 'map_dens_' + gasf[row] + dire[col]
pcm2 = ax.imshow(eval(data), norm=mpl.colors.LogNorm(), cmap=cm[2], interpolation='none', extent=[-17.5,17.5,-17.5,17.5])
if row ==4:
if col == 0:
ax.set_xlabel('z (kpc)', fontsize=13)
if col == 1:
ax.set_xlabel('y (kpc)', fontsize=13)
if col == 2:
ax.set_xlabel('z (kpc)', fontsize=13)
if col == 3:
ax.set_xlabel('y (kpc)', fontsize=13)
cb1 = fig.colorbar(pcm, ax=axs[0, :2], location='top', pad=-1.2)
cb2 = fig.colorbar(pcm2, ax=axs[0, 2:4], location='top', pad=- 1.2)
cb1.mappable.set_clim(1E2,1E9)
cb2.mappable.set_clim(1E-5,1E0)
plt.show()
不要在颜色条上设置颜色限制。在 LogNorm 上设置它们。
tight_layout 不能用颜色条做你想做的事。请使用 constrained_layout (https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html)。尽管这可能也不是您想要的,因为您有固定纵横比的轴,所以很难在任何一个方向上都没有 space。
通过 ax=axs[:, :2]
而不是 ax=axs[0, :2]
将所有行指定为拥有颜色条。这有助于获得正确的间距。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
a = 5
b = 4
fig, axs = plt.subplots(a, b , figsize=(8.27, 9.69),
constrained_layout=True, sharey=True,
sharex=True)
cm = ['hot', 'viridis']
for col in range(b):
for row in range(a):
data = np.random.randn(30, 30)
ax = axs[row, col]
if col ==0:
ax.set_ylabel('x (kpc)', fontsize=13)
if col<2:
pcm = ax.imshow(data, norm=mpl.colors.LogNorm(vmin=0.1, vmax=100),
cmap=cm[0],interpolation='none',
extent=[-17.5,17.5,-17.5,17.5])
else:
pcm2 = ax.imshow(data, norm=mpl.colors.LogNorm(vmin=0.2, vmax=200),
cmap=cm[1], interpolation='none',
extent=[-17.5,17.5,-17.5,17.5])
if row ==4:
ax.set_xlabel('z (kpc)', fontsize=13)
cb1 = fig.colorbar(pcm, ax=axs[:, :2], location='top', shrink=0.6)
cb2 = fig.colorbar(pcm2, ax=axs[:, 2:4], location='top', shrink=0.6)
plt.show()
您不妨尝试 axes_grid
(https://matplotlib.org/stable/tutorials/toolkits/axes_grid.html),尽管它也有局限性。
我正在尝试制作一个大的概览图。为此,我使用 plt.imshow
绘制不同密度的矩阵。问题是颜色条不想和我一起工作。如图所示。所以现在我有两个问题。
- 如何确保所有图(相同颜色)具有相同的颜色条范围?
- 如何在最后两个coloms的中间获得第二个colorbar。
我当前的代码是(要测试你可以将 eval(data)
更改为随机矩阵):
a = 5
b = 4
fig, axs = plt.subplots(a, b , figsize=(8.27, 11.69), tight_layout=True, sharey=True, sharex=True, gridspec_kw = {'wspace':0, 'hspace':0})
data = ['map_sm_', 'map_dens_']
gasf = ['fg10', 'fg20', 'fg30', 'fg50', 'fg70']
dire = ['x', 'z', 'x', 'z']
cm = ['hot', 'viridis']
for col in range(b):
for row in range(a):
ax = axs[row, col]
if col ==0:
ax.set_ylabel('x (kpc)', fontsize=13)
if col<2:
if col == 0:
data = 'map_sm_' + gasf[row] + dire[col]
else:
data = 'map_sm_' + gasf[row] + dire[col]
pcm = ax.imshow(eval(data), norm=mpl.colors.LogNorm(), cmap=cm[0], interpolation='none', extent=[-17.5,17.5,-17.5,17.5])
else:
if col == 2:
data = 'map_dens_' + gasf[row] + dire[col]
else:
data = 'map_dens_' + gasf[row] + dire[col]
pcm2 = ax.imshow(eval(data), norm=mpl.colors.LogNorm(), cmap=cm[2], interpolation='none', extent=[-17.5,17.5,-17.5,17.5])
if row ==4:
if col == 0:
ax.set_xlabel('z (kpc)', fontsize=13)
if col == 1:
ax.set_xlabel('y (kpc)', fontsize=13)
if col == 2:
ax.set_xlabel('z (kpc)', fontsize=13)
if col == 3:
ax.set_xlabel('y (kpc)', fontsize=13)
cb1 = fig.colorbar(pcm, ax=axs[0, :2], location='top', pad=-1.2)
cb2 = fig.colorbar(pcm2, ax=axs[0, 2:4], location='top', pad=- 1.2)
cb1.mappable.set_clim(1E2,1E9)
cb2.mappable.set_clim(1E-5,1E0)
plt.show()
不要在颜色条上设置颜色限制。在 LogNorm 上设置它们。
tight_layout 不能用颜色条做你想做的事。请使用 constrained_layout (https://matplotlib.org/stable/tutorials/intermediate/constrainedlayout_guide.html)。尽管这可能也不是您想要的,因为您有固定纵横比的轴,所以很难在任何一个方向上都没有 space。
通过
ax=axs[:, :2]
而不是ax=axs[0, :2]
将所有行指定为拥有颜色条。这有助于获得正确的间距。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
a = 5
b = 4
fig, axs = plt.subplots(a, b , figsize=(8.27, 9.69),
constrained_layout=True, sharey=True,
sharex=True)
cm = ['hot', 'viridis']
for col in range(b):
for row in range(a):
data = np.random.randn(30, 30)
ax = axs[row, col]
if col ==0:
ax.set_ylabel('x (kpc)', fontsize=13)
if col<2:
pcm = ax.imshow(data, norm=mpl.colors.LogNorm(vmin=0.1, vmax=100),
cmap=cm[0],interpolation='none',
extent=[-17.5,17.5,-17.5,17.5])
else:
pcm2 = ax.imshow(data, norm=mpl.colors.LogNorm(vmin=0.2, vmax=200),
cmap=cm[1], interpolation='none',
extent=[-17.5,17.5,-17.5,17.5])
if row ==4:
ax.set_xlabel('z (kpc)', fontsize=13)
cb1 = fig.colorbar(pcm, ax=axs[:, :2], location='top', shrink=0.6)
cb2 = fig.colorbar(pcm2, ax=axs[:, 2:4], location='top', shrink=0.6)
plt.show()
您不妨尝试 axes_grid
(https://matplotlib.org/stable/tutorials/toolkits/axes_grid.html),尽管它也有局限性。