本文转载自《Matplotlib中多子图绘图时,坐标轴及其label的几种排布方式》。
目录
3、如果 x label和y label 都一样可以只显示一个
1、最普通的
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
fig, axes = plt.subplots(3, 3, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
col.imshow(np.arange(100).reshape((10,10)))
col.set_xlabel('x')
col.set_ylabel('y')
plt.tight_layout()
2、只在最外层坐标轴显示 Label
fig, axes = plt.subplots(3, 3, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
col.imshow(np.arange(100).reshape((10,10)))
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
plt.tight_layout()
3、如果 x label和y label 都一样可以只显示一个
fig, axes = plt.subplots(3, 3, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
col.imshow(np.arange(100).reshape((10,10)))
fig.text(0.5, 0, 'x', ha='center')
fig.text(0, 0.5, 'y', va='center', rotation='vertical')
plt.tight_layout()
4、刻度也只在最外侧显示
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
col.imshow(np.arange(100).reshape((10,10)))
fig.text(0.5, 0, 'x', ha='center')
fig.text(0, 0.5, 'y', va='center', rotation='vertical')
plt.tight_layout()
5、或者Label仍然分开显示
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
col.imshow(np.arange(100).reshape((10,10)))
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
plt.tight_layout()
6、加入 colorbar
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
plt.tight_layout()
7、整个 fig 共用一个 colorbar
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
fig.subplots_adjust(wspace = .1,hspace = 0)
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
# ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
cb = fig.colorbar(im, ax=axes.ravel().tolist())
cb.ax.tick_params()
cb.set_label("colorbar")
# plt.tight_layout() # 使用 tight layout 需要手动调整 colorbar 位置,否则会很难看
plt.show()
8、colorbar 横置
fig.subplots_adjust(wspace = 0,hspace = 0.1)
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
# ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
cb = fig.colorbar(im, ax=axes.ravel().tolist(),orientation='horizontal')
cb.ax.tick_params()
cb.set_label("colorbar")
# plt.tight_layout() # 使用 tight layout 需要手动调整 colorbar 位置,否则会很难看
plt.show()
9、调整 colorbar 位置和尺寸
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,5))
fig.subplots_adjust(wspace = .1,hspace = 0)
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
# ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
fig.subplots_adjust(bottom=0, right=0.9, top=1)
cax = plt.axes([0.92, 0.03, 0.03, 0.95])
cb = fig.colorbar(im, cax=cax)
cb.ax.tick_params()
cb.set_label('colorbar')
# plt.tight_layout() # 使用 tight layout 需要手动调整 colorbar 位置,否则会很难看
plt.show()
10、调整 ticks 显示位置以及 label
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,5))
fig.subplots_adjust(wspace = .1,hspace = 0)
for i, row in enumerate(axes):
for j, col in enumerate(row):
im = col.imshow(np.arange(100).reshape((10,10)))
# ax_cb = fig.colorbar(im, ax=col)
if col.is_last_row():
col.set_xlabel('x')
if col.is_first_col():
col.set_ylabel('y')
col.set_xticks(np.arange(0, 11, 2))
col.set_yticks(np.arange(0, 11, 2))
col.set_xticklabels(np.arange(1, 11, 2))
col.set_yticklabels(np.arange(1, 11, 2))
fig.subplots_adjust(bottom=0, right=0.9, top=1)
cax = plt.axes([0.92, 0.03, 0.03, 0.95])
cb = fig.colorbar(im, cax=cax)
cb.ax.tick_params()
cb.set_label('colorbar')
# plt.tight_layout() # 使用 tight layout 需要手动调整 colorbar 位置,否则会很难看
plt.show()
11、twin x
fig, ax = plt.subplots(figsize=(6,4))
t = np.linspace(0, 2*np.pi, 50, endpoint=False)
sins = np.sin(t)
coss = np.cos(t)
ax.plot(t, sins, 'r', alpha=0.5, lw=0.5, ls='-', marker='+', label='sin')
ax2 = ax.twinx()
ax2.plot(t, coss, 'g', alpha=0.5, lw=0.5, ls='-', marker='+', label='cos')
for tl in ax2.get_yticklabels():
tl.set_color("r")
plt.tight_layout()