基本思想
在matplotlib.pyplot中
一个Figure是一个空白的画板
画板上有轴的集合(axes),集合元素包含基本的两个axis ,用来确定一个区域
你所有的操作是去改变这个画板的每一个区域,当然默认就是一个区域了
而子图Subplot实际上是axes的特例(子集) 很显然多搞几个axis,再缩小到特定位置,就是sub了
Most of you are probably familiar with the Subplot, which is just a special case of an Axes that lives on a regular rows by columns grid of Subplot instances. If you want to create an Axes at an arbitrary location, simply use the add_axes() method which takes a list of [left, bottom, width, height] values in 0-1 relative figure coordinates
基本操作
获得当前的figure fig1=plt.figure()
设置你的轴域 ax1 = plt.Axes(fig1,[0.2, 0.2, 0.4, 0.4])
将轴域添加至figurefig1.add_axes(ax1)
将子图添加至figure并保留对象 ax3=fig1.add_subplot(224) #显然subplot没有一般轴域前一步的设置
基本类型
fig1: class 'matplotlib.figure.Figure'
ax1: class 'matplotlib.axes._axes.Axes'
ax3: class 'matplotlib.axes._subplots.AxesSubplot'
patches模块(四个基本的图形)
具体细节参照代码注释
代码示例
#深入了解axes
#一个axes就相当于一个画图区域 分布在大画布fig上
from matplotlib import pyplot as plt
from matplotlib import patches
import numpy as np
import cv2
from pylab import *
fig1=plt.figure(figsize=(8, 5)) # in inches!
ax1 = plt.Axes(fig1,[0.2, 0.2, 0.4, 0.4])#来一个轴,在fig1中
#ax.set_axis_off()#去掉axes
fig1.add_axes(ax1)#添加到fig中
ax2 = plt.Axes(fig1,[0.4, 0.4, 0.4, 0.4])#再来一个轴,在fig1中
fig1.add_axes(ax2)#添加到fig中
print(type(ax1),type(ax2))#观察类型
'''
如果我想直接添加“子图”,如果直接用subplot,貌似会直接画出,并覆盖了前面的画布中非subplot型的axes
可以用fig的add方法直接添加进去,这样不会覆盖
'''
#ax3=plt.subplot(224)#会清除其他的非subplot型的axes
ax3=fig1.add_subplot(224)#不会清除画布,之前的指定位置的axes保留
#plt.subplot(221)#默认会画到当前fig中,但之前的指定位置的axes清除了
ax4=fig1.add_subplot(221)#不会清除画布,之前的指定位置的axes保留
print(type(ax3),type(ax4))#观察类型
#下面分别在ax1,2,3,4 四个画图区域画图
x=np.arange(0,40*pi,0.001)
y=10*np.cos(x)
ax1.plot(x,y,'r--')
ax2.plot(y,x,'b')
ax3.plot(x,y,'g')
ax4.plot(y,x,'y')
#测试一下图片 ax4中
img=cv2.imread(r'E:\my_nut_yun\mycode_python\Image Playground\Chrysanthemum.jpg',0)#灰度模式
ax4.imshow(img,cmap='gray',aspect="auto")
ax4.plot([0,300],[0,300],'y',linewidth=10)#画一条线
ax4.set_title('test image')
# s=ax4.get_title() #获得它的标题 str类型
# print(type(s))
ax5=fig1.add_subplot(443)
mu, sigma = 100, 15
x = mu + sigma * np.random.randn(1000000)# ~N(0,1)
#根据x的范围 将其分成bins份 统计每一份出现的个数 画出hist图
n, bins, patchess=ax5.hist(x, 50, color='g', alpha=0.4,normed=True)#alpha是透明度 normed使得纵坐标是概率
ax5.set_xlabel('Smarts')
ax5.set_ylabel('Probability')
ax5.set_title('Histogram of IQ')
ax5.text(60, .025, r'$\mu=100,\ \sigma=15$')
ax5.grid(True)
binbin=[]
for i in range(len(bins)-1):
binbin.append((bins[i]+bins[i+1])/2)
#print(binbin)#横坐标的新值 取相邻两个的中点
#print(patchess[0]) #rectangles对象 可迭代
ax5.plot(binbin,n,'r--')
patchess[25].set_color('r')
ax5.add_patch(patchess[25])
#测试patches
ax6=fig1.add_subplot(3,3,7)
rec=patches.Rectangle((20,20),20,20,linewidth=3, edgecolor='b', facecolor='r')
circle = patches.Circle((50,50), 10, color = "r")
ellipse = patches.Ellipse((0,0), 18, 24, color= "y") #圆心 长直径 短直径
polygon = patches.RegularPolygon((80,80), 6,20 , color= "b") #正多边形:中心 边数 “半径”
ax6.add_patch(rec)
ax6.add_patch(circle)
ax6.add_patch(ellipse)
ax6.add_patch(polygon)
ax6.set_xlim(0,100)
ax6.set_ylim(0,100)
ax6.axis("equal")
show()