整理子图中的标签,使用 fig.add_axes 创建
Sorting out labels in subplots, created with fig.add_axes
我是 python 的新手,目前正在研究 mathplotlib。下面是我的情节代码,显示在底部的图中。
import matplotlib.pyplot as plt
f = plt.figure(figsize=(15, 15))
ax1 = f.add_axes([0.1, 0.5, 0.8, 0.5],
xticklabels=[])
ax2 = f.add_axes([0.1, 0.4, 0.8, 0.1])
ax1.plot(particles[0, :, 0])
ax1.plot(particles[1, :, 0])
ax2.plot(distances[:])
# Prettifying the plot
plt.xlabel("t", fontsize=25)
plt.tick_params( # modifying plot ticks
axis='x',
labelsize=20)
plt.ylabel("x", fontsize=25)
plt.tick_params( # modifying plot ticks
axis='y',
labelsize=20)
# Plot title
plt.title('Harmonic oscillator in ' + str(dim) + 'D with ' + str(num_step) + ' timesteps', fontsize=30)
# Saving the plot
#plt.savefig("results/2D_dif.png")
这两个图的尺寸和位置都符合我的要求,但是如您所见,标签和标题都没有了。我希望具有与底部图相同的标签样式,上图的 y-label 读数为 "x",标题 "Harmonic oscillator ..." 位于第一张图的顶部.
非常感谢您的帮助!
此处 plt
作用于最近创建的轴实例(在本例中为 ax2
)。这就是 ax1
!
字体没有改变的原因
因此,要获得您想要的结果,您需要明确地对 ax1
和 ax2
采取行动。像下面这样的东西应该可以解决问题:
for ax in ax1, ax2:
# Prettifying the plot
ax.set_xlabel("t", fontsize=25)
ax.tick_params( # modifying plot ticks
axis='x',
labelsize=20)
ax.set_ylabel("x", fontsize=25)
ax.tick_params( # modifying plot ticks
axis='y',
labelsize=20)
# Plot title
ax.set_title('Harmonic oscillator in ' + str(dim) + 'D with ' + str(num_step) + ' timesteps', fontsize=30)
我是 python 的新手,目前正在研究 mathplotlib。下面是我的情节代码,显示在底部的图中。
import matplotlib.pyplot as plt
f = plt.figure(figsize=(15, 15))
ax1 = f.add_axes([0.1, 0.5, 0.8, 0.5],
xticklabels=[])
ax2 = f.add_axes([0.1, 0.4, 0.8, 0.1])
ax1.plot(particles[0, :, 0])
ax1.plot(particles[1, :, 0])
ax2.plot(distances[:])
# Prettifying the plot
plt.xlabel("t", fontsize=25)
plt.tick_params( # modifying plot ticks
axis='x',
labelsize=20)
plt.ylabel("x", fontsize=25)
plt.tick_params( # modifying plot ticks
axis='y',
labelsize=20)
# Plot title
plt.title('Harmonic oscillator in ' + str(dim) + 'D with ' + str(num_step) + ' timesteps', fontsize=30)
# Saving the plot
#plt.savefig("results/2D_dif.png")
这两个图的尺寸和位置都符合我的要求,但是如您所见,标签和标题都没有了。我希望具有与底部图相同的标签样式,上图的 y-label 读数为 "x",标题 "Harmonic oscillator ..." 位于第一张图的顶部.
非常感谢您的帮助!
此处 plt
作用于最近创建的轴实例(在本例中为 ax2
)。这就是 ax1
!
因此,要获得您想要的结果,您需要明确地对 ax1
和 ax2
采取行动。像下面这样的东西应该可以解决问题:
for ax in ax1, ax2:
# Prettifying the plot
ax.set_xlabel("t", fontsize=25)
ax.tick_params( # modifying plot ticks
axis='x',
labelsize=20)
ax.set_ylabel("x", fontsize=25)
ax.tick_params( # modifying plot ticks
axis='y',
labelsize=20)
# Plot title
ax.set_title('Harmonic oscillator in ' + str(dim) + 'D with ' + str(num_step) + ' timesteps', fontsize=30)