In [1]:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
%matplotlib inline
最普通的也是最丑的
In [2]:
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()
只在最外层坐标轴显示 Label
In [3]:
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()
如果 x label y label 都一样可以只显示一个
In [4]:
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()
刻度也只在最外侧显示
In [5]:
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()
或者Label仍然分开显示
In [6]:
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()
加入 colorbar
In [7]:
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()
整个 fig 共用一个 colorbar
In [8]:
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()
colorbar 横置
In [9]:
fig, axes = plt.subplots(3, 3, sharex=True, sharey=True, figsize=(6,6))
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()
调整 colorbar 位置和尺寸
In [10]:
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()
调整 ticks 显示位置以及 label
最后一个 ax 有点问题
In [11]:
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()
twin x¶
In [12]:
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()