1.绘制单图、多图、图中图
1.1基本概念
fig 画布
axes 画布上绘制的坐标轴(包含x轴和y轴或z轴),一个fig上可以绘制多个axes
axis:x轴、y轴、z轴
fig-->axes-->xaxis(yaxis)-->tick-->tick label
-->label
--> title
-->data
1.2绘制图片的几种方式
1.2.1预先绘制子图的个数
#fig,axes=plt.subplots(row,line,sharex=bool,sharey=bool,figsize=(12,8))
row,line这是行列 ,即子图个数,如果不设置,默认为1,则默认是1*1的图片
sharex、sharey 是否共享x轴/y轴
figsize=(12,8)设置画布大小
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
%matplotlib inline #直接显示,不需要show
x=np.linspace(0.0,5.0,100)
y1=np.sin(np.pi*x)
y2=np.cos(np.pi*x)
fig,axes=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,8))
axes[0][0].plot(x,y1,color="pink",label="sin")
axes[0][1].plot(x,y2,color="green",label="cos")
axes[0][0].legend()
axes[0][1].legend()
使用flatten循环画图
fig,axes=plt.subplots(3,3,sharex=True,sharey=True,figsize=(12,8))
color=["yellow","blue","green","pink","red","skyblue","orange","black","blue"]
for ax,co in zip(axes.flatten(),color):
ax.plot(x,y1,color=co)
1.2.2边画边绘制子图
1.
fig=plt.figure(num="name",figsize=(12,8),facecolor="gray",edgecolor="blue")
ax=plt.subplot(row,line,location)
fig=plt.figure(num="name",figsize=(6,6),facecolor="gray",edgecolor="blue")
ax1=plt.subplot(2,2,1)
ax1.plot(x,y2,color="green")
ax2=plt.subplot(2,2,4)
ax2.plot(x,y1,color="blue")
plt.savefig("./3.png")
2
fig=plt.figure()
ax1=fig.add_subplot(221)
fig=plt.figure()
ax1=fig.add_subplot(221)
ax1.plot(x,y2)
ax2=fig.add_subplot(224)
ax2.plot(x,y1)
注:子图放置的位置,如果最后的没有覆盖前面的则会显示,如果覆盖,则前面的不会显示
1.2.3绘制图中图
fig=plt.figure()
ax1=fig.add_axes([left,bottom,width,hight])
ax2=fig.add_axes([left,bottom,width,hight])
fig=plt.figure()
ax1=fig.add_axes([0.1,0.1,0.8,0.8])
ax2=fig.add_axes([0.15,0.5,0.3,0.3])
ax1.plot(range(10),range(10))
ax2.plot(x,y2)
fig相关设置
plt.subplots_adjust(wspace=0.5,hspace=0.5) 设置子图的 间距
plt.suptitile(title,x,y,figsize) 设置总标题