使用 imshow 创建并排图
Create a side by side plot using imshow
我是 Python 的初学者,我在创建多个图时遇到了一些问题。
现在,我正在尝试使用 imshow 并排创建两个图。
我目前所做的尝试如下所示。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
import numpy as np
import h5py as h5
from matplotlib import cm
from matplotlib import rc
data = h5.File('twolevel.h5', 'r')
en_kin = data['1']['energyk']
en_pot = data['2']['energyp']
psiimag = data['3']['psiimag']
psireal = data['3']['psireal']
time = data['3']['t']
latticex = data['3']['x']
latticey = data['3']['y']
dprof = np.power(psireal[:, :, :], 2) + np.power(psiimag[:, :, :], 2)
l = 400
fig = plt.figure()
ax = plt.add_subplot(121)
mdr = ax.imshow(dprof[0, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax.set_ylabel('Y',fontsize='large', fontweight='bold')
ax.set_xlabel('X',fontsize='large', fontweight='bold')
ax.set_title('t = 0', fontsize='large', fontweight='bold')
fig.colorbar(mdr)
ax = plt.add_subplot(122)
mdr = ax.imshow(dprof[1000, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax.set_ylabel('Y',fontsize='large', fontweight='bold')
ax.set_xlabel('X',fontsize='large', fontweight='bold')
ax.set_title('t = 1000', fontsize='large', fontweight='bold')
fig.colorbar(mdr)
plt.tight_layout()
plt.show()
h5 文件的 link 是:https://drive.google.com/drive/folders/1XvGzWIXIgUjWDYcEGHVFw_GHu7NqDdnL?usp=sharing
当我 运行 脚本时,它 returns 显示以下消息:模块 'matplotlib.pyplot' 没有属性 'add_subplot'。
我想知道这样做的正确方法是什么。谁能给我举个例子或给我指明正确的方向?
提前致谢。
matplotlib 文档非常好,有一个lot of examples 如何创建子图。
来自文档:
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
您需要执行以下操作:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(dprof[0, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax2.imshow(dprof[1000, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
plt.show()
我是 Python 的初学者,我在创建多个图时遇到了一些问题。
现在,我正在尝试使用 imshow 并排创建两个图。
我目前所做的尝试如下所示。
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
import numpy as np
import h5py as h5
from matplotlib import cm
from matplotlib import rc
data = h5.File('twolevel.h5', 'r')
en_kin = data['1']['energyk']
en_pot = data['2']['energyp']
psiimag = data['3']['psiimag']
psireal = data['3']['psireal']
time = data['3']['t']
latticex = data['3']['x']
latticey = data['3']['y']
dprof = np.power(psireal[:, :, :], 2) + np.power(psiimag[:, :, :], 2)
l = 400
fig = plt.figure()
ax = plt.add_subplot(121)
mdr = ax.imshow(dprof[0, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax.set_ylabel('Y',fontsize='large', fontweight='bold')
ax.set_xlabel('X',fontsize='large', fontweight='bold')
ax.set_title('t = 0', fontsize='large', fontweight='bold')
fig.colorbar(mdr)
ax = plt.add_subplot(122)
mdr = ax.imshow(dprof[1000, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax.set_ylabel('Y',fontsize='large', fontweight='bold')
ax.set_xlabel('X',fontsize='large', fontweight='bold')
ax.set_title('t = 1000', fontsize='large', fontweight='bold')
fig.colorbar(mdr)
plt.tight_layout()
plt.show()
h5 文件的 link 是:https://drive.google.com/drive/folders/1XvGzWIXIgUjWDYcEGHVFw_GHu7NqDdnL?usp=sharing 当我 运行 脚本时,它 returns 显示以下消息:模块 'matplotlib.pyplot' 没有属性 'add_subplot'。
我想知道这样做的正确方法是什么。谁能给我举个例子或给我指明正确的方向?
提前致谢。
matplotlib 文档非常好,有一个lot of examples 如何创建子图。
来自文档:
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('Horizontally stacked subplots')
ax1.plot(x, y)
ax2.plot(x, -y)
您需要执行以下操作:
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.imshow(dprof[0, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
ax2.imshow(dprof[1000, :, :], extent=[-l, l, -l, l], interpolation='lanczos',cmap='plasma')
plt.show()