接演前文。


设置属性的方法:

  • 使用对象的set_*方法,单独设置每个属性;或使用plt.setp同时设置多个属性
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt # set range 0~5, step = 0.1
x = np.arange(0, 5, 0.1)
# plot return a list. only get first element as 'line'
line, = plt.plot(x, x*x)
# set_antialiased = True, smoother
line.set_antialiased(False)
plt.show() # lines has 2 lines.
lines = plt.plot(x, np.sin(x), x, np.cos(x))
plt.setp(lines, color="r", linewidth=2.0)
plt.show()

  同时,可以使用get_*或者plt.getp来获取对象的属性值。

print line.get_linewidth()
# getp only operates on one object each time
print plt.getp(lines[1])
# given parameter
print plt.getp(lines[0], "color")
# get current figure object. It's returned by plt.plot.
cur = plt.gcf()
# although cur is a list, cannot be indexed in getp.
plt.getp(cur)

  Figure对象有一个axes属性,值为AxesSubplot对象列表,即图中的每个子图。获得当前的子图应使用

plt.gca()

  要在Figure对象中放多个子图(轴,axes),应使用subplot(nRows, nCols, plotNum)。图的编号从1开始,从左到右,从上到下。如果这三个参数都小于10,

可以写成整数。subplot在编号为plotNum的区域中创建子图,新的会覆盖旧的。另外,可以用图中的配置按钮配置left.right.top等几个参数。

  举个栗子咯,下面的代码会创建3*2的图,设置指定背景颜色。

plt.figure(1)
for idx, color in enumerate("rgbyck"):
plt.subplot(320+idx+1, axisbg=color)
plt.show()

  如果需要某个轴占据整行:

plt.subplot(221)
plt.subplot(222)
# 2*1, so position 2 is the whole second row space
plt.subplot(212)

可以通过修改配置文件调整缺省值,批量修改属性。相关方法见网页。

http://old.sebug.net/paper/books/scipydoc/matplotlib_intro.html#id5


matplotlib API含三层,backend_bases.FigureCanvas,backend_bases.Renderer和artist.Artist。一般只使用高层的Artist。

它分为两种,简单类型为标准绘图元件,如Line2D、Rectangle之类的,容器类型则是将简单的合成一个整体,如Figure、Axis等。

一般流程:创建Figure对象--->创建Axes--->创建简单类型的Artist


Artist对象的所有属性都通过相应的 get_* 和 set_* 函数进行读写

一次设置多个属性,set函数:fig.set(alpha=0.5, zorder=2)

属性列表:

alpha : 透明度,值在0到1之间,0为完全透明,1为完全不透明
animated : 布尔值,在绘制动画效果时使用
axes : 此Artist对象所在的Axes对象,可能为None
clip_box : 对象的裁剪框
clip_on : 是否裁剪
clip_path : 裁剪的路径
contains : 判断指定点是否在对象上的函数
figure : 所在的Figure对象,可能为None
label : 文本标签
picker : 控制Artist对象选取
transform : 控制偏移旋转
visible : 是否可见
zorder : 控制绘图顺序

figure对象和Axes对象都有patch属性作为其背景,它的值是一个Rectangle对象。注意,刚设置完patch的颜色后,要用.canvas.draw()更新。

fig = plt.figure(1)
# left, bottom, width, height. relative to figure object. 0-1
ax = fig.add_axes([0.15, 0.5, 0.7, 0.3])
# draw a line
lines = ax.plot([1,2,3],[1,2,1])
ax.set_xlabel("time")
fig.patch.set_color("g")
fig.canvas.draw()
fig.show() 

Figure对象

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_axes([0.1, 0.1, 0.7, 0.3])
# subplot and axes are added to Figure.axes
# don't operate on Figures.axes directly, use
# add_subplot, add_axes, delaxes etc.
# But iteration is ok.
for ax in fig.axes: ax.grid(True)
plt.show()  

在Figure对象中画两条线:

from matplotlib.lines import Line2D
fig = plt.figure()
# 缺省的坐标系统是像素点,但可以通过transform来修改坐标系(缺省是左下角原点(0,0),右上角(1,1))。
# 此处两条线用的都是fig的,它们所在的figure对象也是fig
# Line2D([xdata],[ydata]...)
lin1 = Line2D([0,1], [0,1], transform=fig.transFigure, figure=fig, color="r")
lin2 = Line2D([0,1], [1,0], transform=fig.transFigure, figure=fig, color="g")
# 将两条线添加到fig.lines属性中
fig.lines.extend([lin1, lin2])
plt.show()  

包含Artist对象的属性:

axes : Axes对象列表
patch : 作为背景的Rectangle对象
images : FigureImage对象列表,用来显示图片
legends : Legend对象列表
lines : Line2D对象列表
patches : patch对象列表
texts : Text对象列表,用来显示文字

Axes容器

fig = plt.figure()
ax = fig.add_subplot(111)
# axes对象ax有patch属性作为背景。笛卡尔坐标时,patch为Rectangle对象;极坐标时,为Circle对象。
# 设置背景为蓝色。
ax.patch.set_facecolor("blue")
# fig.show() 会闪退
plt.show()  

一般不对Axes所带的lines或patch直接操作


Axis容器

包括坐标轴上的刻度线、刻度文本、坐标网格以及坐标轴标题等内容。刻度包括主刻度和副刻度,分别通过Axis.get_major_ticks和Axis.get_minor_ticks方法获得。

每个刻度线都是一个XTick或者YTick对象,它包括实际的刻度线和刻度文本。为了方便访问刻度线和文本,Axis对象提供了get_ticklabels和get_ticklines方法。

# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt fig = plt.figure()
sub1 = fig.add_subplot(1,2,1)
sub1.plot([1,2,3], [4,5,6]) xAxis = fig.gca().xaxis
# values of ticks on x-axis
print xAxis.get_ticklocs()
# show ticklabels. but now, it's empty
print [x.get_text() for x in xAxis.get_ticklabels()]
# set ticklabels
for label in xAxis.get_ticklabels():
label.set_color("red")
label.set_rotation(45)
label.set_fontsize(16)
# get minor ticklines
# get_ticklines(minor=True)
for line in xAxis.get_ticklines():
line.set_color("green")
line.set_markersize(25)
line.set_markeredgewidth(3)
plt.show()

关于刻度的定位和文本格式的东西都在matplotlib.ticker中定义,程序中使用到如下两个类:

MultipleLocator : 以指定值的整数倍为刻度放置刻度线

FuncFormatter : 使用指定的函数计算刻度文本,他会传递给所指定的函数两个参数:刻度值和刻度序号

# 主刻度为pi/4
ax.xaxis.set_major_locator( MultipleLocator(np.pi/4) ) # 主刻度文本用pi_formatter函数计算
ax.xaxis.set_major_formatter( FuncFormatter( pi_formatter ) ) # 副刻度为pi/20
ax.xaxis.set_minor_locator( MultipleLocator(np.pi/20) )

  

  

python-matplotlib-lec1的更多相关文章

  1. python matplotlib 中文显示参数设置

    python matplotlib 中文显示参数设置 方法一:每次编写代码时进行参数设置 #coding:utf-8import matplotlib.pyplot as pltplt.rcParam ...

  2. python matplotlib plot 数据中的中文无法正常显示的解决办法

    转发自:http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...

  3. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题

    ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...

  4. 使用Python matplotlib做动态曲线

    今天看到“Python实时监控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也学习如何使用Python ma ...

  5. python matplotlib 中文显示乱码设置

    python matplotlib 中文显示乱码设置 原因:是matplotlib库中没有中文字体.1 解决方案:1.进入C:\Anaconda64\Lib\site-packages\matplot ...

  6. Python - matplotlib 数据可视化

    在许多实际问题中,经常要对给出的数据进行可视化,便于观察. 今天专门针对Python中的数据可视化模块--matplotlib这块内容系统的整理,方便查找使用. 本文来自于对<利用python进 ...

  7. 转:使用 python Matplotlib 库 绘图 及 相关问题

     使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc/article/details/9714163 Matplotlib ...

  8. python+matplotlib 绘制等高线

    python+matplotlib 绘制等高线 步骤有七: 有一个m*n维的矩阵(data),其元素的值代表高度 构造两个向量:x(1*n)和y(1*m).这两个向量用来构造网格坐标矩阵(网格坐标矩阵 ...

  9. 安装python Matplotlib 库

    转:使用 python Matplotlib 库 绘图 及 相关问题  使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc ...

  10. python matplotlib.pyplot 散点图详解(1)

    python matplotlib.pyplot散点图详解(1) 一.创建散点图 可以用scatter函数创建散点图 并使用show函数显示散点图 代码如下: import matplotlib.py ...

随机推荐

  1. 算法设计与分析-HomeWork

    ex1(p20) 代码如下: import random def Darts(n): k=0 i=1 while i<=n: x=random.uniform(0,1) #y=random.un ...

  2. 自定义view(14)使用Path绘制复杂图形

    灵活使用 Path ,可以画出复杂图形,就像美术生在画板上画复杂图形一样.程序员也可以用代码实现. 1.样板图片 这个是个温度计,它是静态的,温度值是动态变化的,所以要自定义个view.动态显示值,温 ...

  3. B. Game of the Rows

    B. Game of the Rows time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计

    unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计 最近做玩票性质的游戏项目,客户端技术是 unity3d 和 android. ...

  5. MyBatis学习总结(二)---实例

    为了对MyBatis有个初步了解,现做一个简单的增.删.改.查实例.了解涉及的文件与相关作用. MySql创建friends表,以下是表的sql语句 DROP TABLE IF EXISTS `fri ...

  6. 安装vs2013提示必须安装ie10的解决办法

    虽说应该直接安装ie10,但试了下并不是很顺利,找到如下解决办法,亲测通过. 新建bat文件,内容如下,右键以管理员身份运行,vs即可正常安装. @ECHO OFF :IE10HACK REG ADD ...

  7. AngularJS(三):重复HTML元素、数据绑定

    本文也同步发表在我的公众号“我的天空” 重复HTML元素 在前端的页面编写中,我们会经常遇到重复HTML元素,譬如绘制表格.菜单等,如以下代码显示一个简单的li列表: <body>    ...

  8. 牛客NOIP提高组(二)题解

    心路历程 预计得分:100 + 40 + 30 = 170 实际得分:100 + 30 + 0 = 130 T2有一个部分分的数组没开够RE了. T3好像是思路有点小问题.. 思路没问题,实现的时候一 ...

  9. 学JS的书籍

    1.JavaScript DOM 编程艺术 [说明] 这本书最大的特点就是简明易懂,循序渐进,适合初学者,非常容易上手. 计划:三天读完 读书总结:待写 2.Javascript权威指南 特点是权威. ...

  10. ABAP接口用法

    1.定义接口INTERFACE intf [PUBLIC].   [components] ENDINTERFACE. 2.注意点: 2.1.接口中所定义的所有东西默认都是公共的,所以不用也不能写PU ...