Python 画子图
add_axes 方法
注意的点
- 如何创建一个子图
- 共享坐标轴的方法
- 把子图放到一个列表里,画图的时候用for循环统一画图。
import numpy as np
import math
import matplotlib.pyplot as plt
x=np.linspace(0,10,100)
y=np.sin(x)
fig=plt.figure()
#每一个子图的宽度
width=0.3
#每一个子图的高度
height=0.3
#左下角距离左侧的距离
left=0.05
#左下角距离底部的距离
bottom=0.1
#创建子图,创建上面一排3个子图
fig_0w=fig.add_axes([left,bottom+height,width,height])
fig_1w=fig.add_axes([left+width,bottom+height,width,height],sharey=fig_0w)
fig_2w=fig.add_axes([left+2*width,bottom+height,width,height],sharey=fig_0w)
#创建子图,创建下面一排3个子图
fig_0v=fig.add_axes([left,bottom,width,height])
fig_1v=fig.add_axes([left+width,bottom,width,height])
fig_2v=fig.add_axes([left+2*width,bottom,width,height])
#把所有的图放到一个列表里,这样可以方便后来统一画图
fig_list=[[fig_0w,fig_1w,fig_2w],
[fig_0v,fig_1v,fig_2v]]
#画三种情况下w-k图
for i in range(3):
ax=fig_list[0][i]
#画图
ax.plot(x,y)
for i in range(3):
ax=fig_list[1][i]
ax.plot(x,y)
plt.show()
运行的结果图片