详细的参考链接:更详细的:https://www.cnblogs.com/zhizhan/p/5615947.html

图像、子图、坐标轴以及记号

Matplotlib中图像的意思是打开的整个画图窗口,【图像】里有所谓的【子图】,子图的位置是有坐标网格确定的,而【坐标轴】的位置却不受任何限制,可以放在图像中的任何位置

上篇中是使用隐式的方法来绘制图像以及坐标轴,当我们使用plot()时,matplotlib调用gca()函数以及gcf()函数获取当前的坐标轴和图像,如果获取不到图像,则会调用figure函数来创建一个--准确来说,是使用subplot(1,1,1,)创建一个只用一个子图的图像。在快速绘图中,这样是很方便的,我们也可以显示的控制图像、子图、坐标轴及记号。

图像

  当我们运行程序,会打开一个以figure*命名的窗口,这一整个窗口就是【图像】,图像的编号从1开始,和MATLAB的风格保持一致,不与Python从0开始编号的风格。

参数 默认值 描述
num 1 图像的数量
figsize figure.figsize 图像的长和宽(英寸)
dpi figure.dpi 分辨率(点/英寸)
facecolor figure.facecolor 绘图区域的背景颜色
edgecolor figure.edgecolor 绘图区域边缘的颜色
frameon True 是否绘制图像边缘

我们可以在源文件中指明这些默认值,不过一般除了图像数量这个参数,其他一般很少修改。

可以使用close()这个命令关闭图像,这个图像有几个可选参数

close()#关闭当前窗口
close()#传入当前实例或者图像编号关闭指定窗口
close(all)关闭全部窗口  

  使用如下语句,关闭失败

#plt.draw()
plt.show()
time.sleep(1)
plt.close('all')

  

(不理解),和其他对象一样,你也可以使用setp或者set_something来设置图像的属性

 

子图

  你可以使用【子图】来将图样(plot)放置在均匀的坐标网格中,使用subplot函数,需要指明网格的行列数量,以及你需要将【图样】放置在哪一个区域中。另外还可以使用功能同样强大的gridspec()来实现同样的功能。

  使用subplot绘制没有嵌套的网格:

#绘制3行4列一共12个网格,按照先从左到右,后从上到下的顺序,左上角为1,右下角为12
plt.subplot(3,4,3)#将图样放在第三个中

  

  使用gridspec()绘制嵌套坐标网格

from pylab import *
import matplotlib.gridspec as gridspec

G = gridspec.GridSpec(3, 3)

axes_1 = subplot(G[0, :])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 1',ha='center',va='center',size=24,alpha=.5)

axes_2 = subplot(G[1,:-1])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 2',ha='center',va='center',size=24,alpha=.5)

axes_3 = subplot(G[1:, -1])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 3',ha='center',va='center',size=24,alpha=.5)

axes_4 = subplot(G[-1,0])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 4',ha='center',va='center',size=24,alpha=.5)

axes_5 = subplot(G[-1,-2])
xticks([]), yticks([])
text(0.5,0.5, 'Axes 5',ha='center',va='center',size=24,alpha=.5)

#plt.savefig('../figures/gridspec.png', dpi=64)
show()

  效果:

Python 绘图与可视化 matplotlib(下)

坐标轴axes

  我们先来看什么是Figure和Axes对象。在matplotlib中,整个图像为一个Figure对象。在Figure对象中可以包含一个,或者多个Axes对象。每个Axes对象都是一个拥有自己坐标系统的绘图区域。其逻辑关系如下:

Python 绘图与可视化 matplotlib(下)

  其中Axis是确定坐标范围的:

plt.axis([xmin, xmax, ymin, ymax])
上面例子里的axis()命令给定了坐标范围。

  axes的官方文档:

  • axes() by itself creates a default full subplot(111) window axis.(axes()会单独创建一个默认的窗口)
  • axes(rect, axisbg=’w’) where rect = [left, bottom, width, height] in normalized (0, 1) units. axisbg is the background color for the axis, default white.(其中rect=[left,bottom,width,height]是标量范围为0-1,,axisbgsi 这个axis的背景色)
  • axes(h) where h is an axes instance makes h the current axis. An Axes instance is returned.(返回一个Axes实例,其中的h是当前axis?)

rect=[左, 下, 宽, 高] 规定的矩形区域,rect矩形简写,这里的数值都是以figure大小为比例,因此,若是要两个axes并排显示,那么axes[2]的左=axes[1].左+axes[1].宽,这样axes[2]才不会和axes[1]重叠。

 

  这个坐标轴的意思并不是坐标系中的那个坐标轴,而是一种防止图样位置的一种定位方式,坐标轴表示的定位可以放在图像的任何位置,如果你想要在一副大图中添加一副小图,就可以使用这样的方法。

import matplotlib.pyplot as plt

plt.axes([0.1,0.1,.8,.8])
plt.xticks([]), plt.yticks([])
plt.text(0.6,0.6, 'axes([0.1,0.1,.8,.8])',ha='center',va='center',size=20,alpha=.5)

axes([0.2,0.2,.3,.3])
xticks([]), yticks([])
text(0.5,0.5, 'axes([0.2,0.2,.3,.3])',ha='center',va='center',size=16,alpha=.5)

plt.savefig("../figures/axes.png",dpi=64)
show()

  效果:

Python 绘图与可视化 matplotlib(下)

plt.axes([0,0,1,1])
#plt.axes([a,b,c,d])四个都是标量,0-1
#a代表图像左边与窗口边缘的距离为窗口宽的a倍
#b-----上边
#c宽度
#d高度为窗口高的d倍

  

Python 绘图与可视化 matplotlib(下)

 

记号

  matplotlib中记号中的各个细节都是可以由用户自己配置的。Ticks Locators指定在哪些位置放置记号、Ticks Formatters来调整记号的样式。主要的记号和次要的记号可以以不同的方式来显示、默认情况下、每个记号都是隐藏的、也就是说默认情况下、次要记号的列表都是空的 NullLocator

Tick Locators

下面有为不同需求设计的一些 Locators。

 

类型 说明
NullLocator No ticks.
Python 绘图与可视化 matplotlib(下)
IndexLocator Place a tick on every multiple of some base number of points plotted. 
Python 绘图与可视化 matplotlib(下)
FixedLocator Tick locations are fixed. 
Python 绘图与可视化 matplotlib(下)
LinearLocator Determine the tick locations. 
Python 绘图与可视化 matplotlib(下)
MultipleLocator Set a tick on every integer that is multiple of some base. 
Python 绘图与可视化 matplotlib(下)
AutoLocator Select no more than n intervals at nice locations. 
Python 绘图与可视化 matplotlib(下)
LogLocator Determine the tick locations for log axes. 
Python 绘图与可视化 matplotlib(下)

这些 Locators 都是 matplotlib.ticker.Locator 的子类,你可以据此定义自己的 Locator。以日期为 ticks 特别复杂,因此 Matplotlib 提供了 matplotlib.dates 来实现这一功能。

  

相关文章: