【Matplotlib】详解图像各个部分
2016-12-01 13:55
253 查看
【Matplotlib】详解图像各个部分
首先一幅Matplotlib的图像组成部分介绍。
在matplotlib中,
整个图像为一个
Figure对象。在Figure对象中可以包含一个或者多个
Axes对象。每个Axes(ax)对象都是一个拥有自己坐标系统的绘图区域。所属关系如下:
下面以一个直线图来详解图像内部各个组件内容:
其中:title为图像标题,Axis为坐标轴, Label为坐标轴标注,Tick为刻度线,Tick Label为刻度注释。各个对象关系可以梳理成以下内容:
图像中所有对象均来自于
Artist的基类。
上面基本介绍清楚了图像中各个部分的基本关系,下面着重讲一下几个部分的详细的设置。
一个"Figure"意味着用户交互的整个窗口。在这个figure中容纳着"subplots"。
当我们调用plot时,matplotlib会调用
gca()获取当前的axes绘图区域,而且
gca反过来调用
gcf()来获得当前的figure。如果figure为空,它会自动调用
figure()生成一个figure,
严格的讲,是生成
subplots(111)。
Figures
Subplots
plt.subplot(221) # 第一行的左图 plt.subplot(222) # 第一行的右图 plt.subplot(212) # 第二整行 plt.show()
注意:其中各个参数也可以用逗号
,分隔开。第一个参数代表子图的行数;第二个参数代表该行图像的列数;
第三个参数代表每行的第几个图像。
另外:
fig, ax = plt.subplots(2,2),其中参数分别代表子图的行数和列数,一共有 2x2 个图像。函数返回一个figure图像和一个子图ax的array列表。
补充:gridspec命令可以对子图区域划分提供更灵活的配置。
Tick Locators
Tick Locators 控制着 ticks 的位置。比如下面:
ax = plt.gca() ax.xaxis.set_major_locator(eval(locator))
一些不同类型的locators:
代码如下:
import numpy as np import matplotlib.pyplot as plt def tickline(): plt.xlim(0, 10), plt.ylim(-1, 1), plt.yticks([]) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['left'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) ax.yaxis.set_ticks_position('none') ax.xaxis.set_minor_locator(plt.MultipleLocator(0.1)) ax.plot(np.arange(11), np.zeros(11)) return ax 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()', ] n_locators = len(locators) size = 512, 40 * n_locators dpi = 72.0 figsize = size[0] / float(dpi), size[1] / float(dpi) fig = plt.figure(figsize=figsize, dpi=dpi) fig.patch.set_alpha(0) for i, locator in enumerate(locators): plt.subplot(n_locators, 1, i + 1) ax = tickline() ax.xaxis.set_major_locator(eval(locator)) plt.text(5, 0.3, locator[3:], ha='center') plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99) plt.show()
所有这些locators均来自于基类matplotlib.ticker.Locator。你可以通过继承该基类创建属于自己的locator样式。同时matplotlib也提供了特殊的日期locator,
位于matplotlib.dates.
点滴积累,点滴生活,丰富研究生生活!
相关文章推荐
- 【Matplotlib】详解图像各个部分
- 【Matplotlib】详解图像各个部分
- 【Matplotlib】详解图像各个部分
- 【matplotlib】详解图像各个部分
- matplotlib animation动画保存(save函数)详解
- python使用matplotlib绘图详解
- Linux下python matplotlib.pyplot在图像上显示中文的问题
- matplotlib模块数据可视化-figure图像
- 【matplotlib】matplotlib画图,读取图像
- Matplotlib官方文档详解
- [置顶] 【python 图像切割】matplotlib读取图像,裁剪图像
- python opencv 使用 Matplotlib显示图像
- matplotlib利用scatter绘制彩色图像:NameError: name 'array' is not defined
- python matplotlib 绘制三次函数图像
- 使用matplotlib绘制图像初步
- 设置全局参数——定制matplotlib(部分翻译)
- 使用matplotlib时pyplot.show()不显示图像的问题
- 基于Python数据可视化利器Matplotlib,绘图入门篇,Pyplot详解
- wxpython和matplotlib显示图像的一个例子
- matplotlib(Python)画柱状图详解