当一些具有颜色条而另一些没有颜色条时,如何对齐不同子图的轴?
import numpy as np
import matplotlib.pyplot as plt
data1 = np.random.random([15,15])
data2 = np.random.random(15)
fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2)
for ax in [ax1, ax2, ax4, ax5, ax6]:
plt.sca(ax)
plt.pcolormesh(data1)
plt.colorbar()
plt.sca(ax3)
plt.plot(data2)
我想在ax3
的左侧添加空白,以便与其他图形对齐。
答案 0 :(得分:1)
这是到目前为止(基于https://stackoverflow.com/a/54473867/2383070的最佳做法)。
我仍然想知道是否有更简单的方法,例如简单地在轴的左侧添加空格。
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
data1 = np.random.random([15,15])
data2 = np.random.random(15)
fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2)
for ax in [ax1, ax2, ax4, ax5, ax6]:
im1 = ax.pcolormesh(data1, cmap='magma')
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=.05)
plt.colorbar(im1, cax=cax)
im2 = ax3.plot(data2)
divider2 = make_axes_locatable(ax3)
cax2 = divider2.append_axes("right", size="5%", pad=.05)
cax2.remove()
答案 1 :(得分:0)
这是constrained_layout
设计要做的事情:https://matplotlib.org/tutorials/intermediate/constrainedlayout_guide.html
fig, [[ax1, ax2], [ax3, ax4], [ax5, ax6]] = plt.subplots(3,2,
constrained_layout=True)