一幅图包含以下信息:
一个axis中有6个参数可以修改。包括:tick label 、axis label、title、data。
一个figure可以有多个axis
从figure创建开始:
figure, ax_lst = plt.subplots(1, 1)# different with plt.subplot()
fig, axes = plt.subplots(2, 2, figsize=(7, 6))
fig.tight_layout(pad=1.08, h_pad=None, w_pad=None, rect=None) # 自动调整subplot间的参数
pad: 主窗口边缘和子图边缘间的间距,默认为1.08
h_pad, w_pad: 子图边缘之间的间距,默认为
pad_inches
rect: 一个矩形区域,如果设置这个值,则将所有的子图调整到这个矩形区域内。
ax0, ax1, ax2, ax3 = axes.ravel()
ax0.imshow(img[:,:,0],cmap=??)
ax0.set_title("Original image")
或者
fig = plt.figure(figsize=figsize, dpi=dpi)
ax=plt.gca()
往下就是在这个figure里绘图
根据数据绘图:
import numpy as np
# import matplotlib.pyplot as plt or
from matplotlib import pyplot as plt
# Figure代表一个绘制面板,其中可以包涵多个Axes
size = 512, 512 #the size of figure
dpi = 100.0
figsize = size[0] / float(dpi), size[1] / float(dpi)
fig = plt.figure(num=1,figsize=figsize, dpi=dpi,facecolor='red',edgecolor='yellow')
#num: 整型或字符型都可以。如果设置为整型,则该整型数字表示窗口的序号。如果设置为字符型,则该字符串表示窗口的名称。
#设置透明度
fig.patch.set_alpha(alpha=0.5)
plt.subplot(111)
# set the range of the axis
plt.xlim(0,10)
plt.ylim(0,10)
# 获取当前的axes绘图区域
ax=plt.gca()
# set the color of the border
ax.spines['right'].set_color('yellow')
ax.spines['left'].set_color('red')
ax.spines['top'].set_color('black')
ax.spines['bottom'].set_color('blue')
#change the position of the border
ax.spines['bottom'].set_position(('data',5))#之前设定ylim[-1,1],0在[-1,1]之间
#put the axis to the border
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
# plot , x point to y
ax.plot(np.arange(11), np.arange(11))
#-------------------------------------------------
# set the tick label 方法1
plt.yticks(np.arange(0,11,3))
# set the tick label 方法2
locators = [
'plt.NullLocator()',
'plt.MultipleLocator(1.0)',
'plt.FixedLocator([0, 2, 8, 9, 10])',
'plt.IndexLocator(3, 1)',
'plt.LinearLocator(5)',
'plt.LogLocator(2, [1.0])',
'plt.AutoLocator()',
]
locator = locators[5]
ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1))
ax.xaxis.set_major_locator(eval(locator))
#-------------------------------------------------
# set the title
# plt.title('This is title') or
ax.set_title('title')
# 在图中任意位置写text
plt.text(5, 5, locator[4:], ha='center')
# set the label of axis
plt.xlabel('row')
plt.ylabel('column')
# 调整位置,bottom+top=1 left+right=1 往左下移动了
plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99)
plt.show()
plt.annotate('min',xy=(-1.3,a),xytext=(3,40),arrowprops=dict(facecolor='black',shrink=0.05))# 某一点画箭头
plt.legend()#右上角标记
根据图片绘图:
from skimage import data
import matplotlib.pyplot as plt
img = data.astronaut()
plt.figure(num='astronaut', figsize=(8, 8)) # 创建一个名为astronaut的窗口,并设置大小
plt.subplot(2, 2, 1) # 将窗口分为两行两列四个子图,则可显示四幅图片
plt.title('origin image') # 第一幅图片标题
plt.imshow(img[:,:,1], cmap=plt.cm.spring) # 绘制第一幅图片 0R 1G 2B
plt.show() # 在imshow加这一句才能显示图像
plt.axis('off') # 不显示坐标尺寸
or
fig = plt.figure()
ax = fig.add_subplot(131)
p = ax.imshow(hist, interpolation="nearest")
ax.set_title("2D Color Histogram for G and B")
颜色图谱 描述
gray 黑-白
jet 蓝-青-黄-红
bone 黑-白,x线
cool 青-洋红
copper 黑-铜
flag 红-白-蓝-黑
hot 黑-红-黄-白
hsv hsv颜色空间, 红-黄-绿-青-蓝-洋红-红
inferno 黑-红-黄
magma 黑-红-白
pink 黑-粉-白
plasma 绿-红-黄
prism 红-黄-绿-蓝-紫-...-绿模式
viridis 蓝-绿-黄
spring 洋红-黄
summer 绿-黄
autumn 红-橙-黄
winter 蓝-绿
灰度直方图
from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
print(img.shape)
arr=img.flatten()#将二维数组序列化成一维数组。
print(arr.shape)
n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red')
plt.show()
一般来说直方图都是征对灰度图的,如果要画rgb图像的三通道直方图,实际上就是三个直方图的叠加。
from skimage import data
import matplotlib.pyplot as plt
img=data.lena()
ar=img[:,:,0].flatten()
plt.hist(ar, bins=256, normed=1,facecolor='r',edgecolor='r',hold=1)
ag=img[:,:,1].flatten()
plt.hist(ag, bins=256, normed=1, facecolor='g',edgecolor='g',hold=1)
ab=img[:,:,2].flatten()
plt.hist(ab, bins=256, normed=1, facecolor='b',edgecolor='b')
plt.show()
其中,加一个参数hold=1,表示可以叠加