本文共 1504 字,大约阅读时间需要 5 分钟。
import numpy as npx = np.linspace(0, 5, 10)y = x ** 2
from pylab import *figure()plot(x, y, 'r')xlabel('x')ylabel('y')title('title')show()
subplot(1,2,1)plot(x, y, 'r--')subplot(1,2,2)plot(y, x, 'g*-');
fig = plt.figure()# 不关心位置axes = fig.add_subplot(1, 1, 1)# 关心位置axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1)axes.plot(x, y, 'r')axes.set_xlabel('x')axes.set_ylabel('y')axes.set_title('title');
fig = plt.figure()# 不关心位置axes1 = fig.add_subplot(2, 1, 1)axes2 = fig.add_subplot(2, 1, 2)# 关心位置axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axesaxes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes# main figureaxes1.plot(x, y, 'r')axes1.set_xlabel('x')axes1.set_ylabel('y')axes1.set_title('title')# insertaxes2.plot(y, x, 'g')axes2.set_xlabel('y')axes2.set_ylabel('x')axes2.set_title('insert title')
fig, axes = plt.subplots()axes.plot(x, y, 'r')axes.set_xlabel('x')axes.set_ylabel('y')axes.set_title('title')
1)单行,或者单列
fig, axes = plt.subplots(nrows=1, ncols=2)for ax in axes: ax.plot(x, y, 'r') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('title')
2)多行多列
fig, axes = plt.subplots(nrows=3, ncols=2, sharex=True)# 此处不能用 for ax in axes:for i in range(6): axes[i//2, i%2].plot(x, y, 'r') axes[i//2, i%2].set_xlabel('x') axes[i//2, i%2].set_ylabel('y') axes[i//2, i%2].set_title('title')
转载地址:http://vuzgl.baihongyu.com/