关于上面这张图的效果,请移步:python seaborn
from matplotlib import pyplot as plt plt.style.use('fivethirtyeight') fig=plt.figure() ax=fig.add_subplot(1,1,1) plt.text(0.5,0.5,'Figure',ha='center',va='center',size=20,alpha=0.5) # 注:这里的0.5代表x,y轴上面特定的点坐标,ha和va则代表水平和垂直,ha和va可以确保数据绝对居中 plt.show() xax=ax.xaxis yax=ax.yaxis
Python中插入图片
在相应的目录下面放上文件:
import numpy as np from PIL import Image plt.figure() plt.xticks([]),plt.yticks([]) im=np.array(Image.open('M.jpg')) plt.imshow(im) plt.show()
绘制子图
plt.figure() plt.plot([0,1],[0,1]) plt.show()
绘制1*2的子图
绘制1*2的子图,类似于列向量:
# 1*2 子图 plt.subplot(2,1,1) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'subplot(2,1,1)',ha='center',va='center',size=20,alpha=0.5) plt.subplot(2,1,2) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'subplot(2,1,2)',ha='center',va='center',size=20,alpha=0.5) plt.show()
plt.subplot(1,2,1) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'subplot(2,1,1)',ha='center',va='center',size=20,alpha=0.5) plt.subplot(1,2,2) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'subplot(2,1,2)',ha='center',va='center',size=20,alpha=0.5) plt.show()
绘制2*2的子图
## 2*2子图 plt.style.use('seaborn') fig,axes=plt.subplots(nrows=2,ncols=2) for i,ax in enumerate(axes.flat): print(i) print(ax) plt.show()
plt.style.use('seaborn') fig,axes=plt.subplots(nrows=2,ncols=2) for i,ax in enumerate(axes.flat): ax.set(xticks=[],yticks=[]) s='subplot(2,2)'+str(i)+')' ax.text(0.5,0.5,s,ha='center',va='center',size=20,alpha=0.5) plt.show()
绘制不规则子图
不规则的子图绘制:
## 不规则的子图绘制 import matplotlib.gridspec as gridspec G=gridspec.GridSpec(3,3) ax1=plt.subplot(G[0,:]) plt.show()
## 不规则的子图绘制 import matplotlib.gridspec as gridspec G=gridspec.GridSpec(3,3) ax1=plt.subplot(G[0,:]) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'Axes 1',ha='center',va='center',size=20,alpha=0.5) ax2=plt.subplot(G[1,:-1]) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'Axes 2',ha='center',va='center',size=20,alpha=0.5) ax3=plt.subplot(G[1:,-1]) plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'Axes 3',ha='center',va='center',size=20,alpha=0.5) ax4=plt.subplot(G[-1,0]) # 表示倒数第一行和第一列 plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'Axes 4',ha='center',va='center',size=20,alpha=0.5) ax5=plt.subplot(G[-1,1]) # 表示倒数第一行和第二列 plt.xticks([]),plt.yticks([]) plt.text(0.5,0.5,'Axes 5',ha='center',va='center',size=20,alpha=0.5) plt.show()
绘制图中代码
fig,ax=plt.subplots() # 同时生成图和坐标系 ax.set(xticks=[],yticks=[]) s='Style 1\n\nfig,ax=plt.subplots()\nax.plot()' ax.text(0.5,0.5,s,ha='center',va='center',size=20,alpha=0.5)
fig=plt.figure() # 先生成图再生成坐标系 ax=fig.add_subplot(1,1,1) ax.set(xticks=[],yticks=[]) s='Style 2\n\nfig,ax=plt.subplots()\nax.plot()' ax.text(0.5,0.5,s,ha='center',va='center',size=20,alpha=0.5)