接演前文。


设置属性的方法:

  • 使用对象的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. java--时间日期用法

    转载大神 https://www.cnblogs.com/Mr-Lyu/p/5736152.html https://blog.csdn.net/yf198708/article/details/51 ...

  2. 关于FutureTask的探索

    之前关于Java线程的时候,都是通过实现Runnable接口或者是实现Callable接口,前者交给Thread去run,后者submit到一个ExecutorService去执行. 然后知道了还有个 ...

  3. 077 Combinations 组合

    给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[  [2,4],  [3,4],  [2,3],  [1,2],  [ ...

  4. AD7606笔记

    V1~V8共8个ADC通道: REFIN/OUT:基准电源,可选择内部(REF_SLECT=1)的或者外部的(REF_SLECT=0) VDIRVE:MCU的的VCC,2.3~5V.逻辑电平指的是需要 ...

  5. Redis 基础特性讲解

    目录 1.Redis基础杂项小节 1.是什么 2.能干嘛 3.去哪下 4.Redis启动后基础知识讲解 2.Redis数据类型 1.常用的五大数据类型 2.高级'玩家'才知道的其他数据类型 3.Red ...

  6. 【转载】Ubuntu16.04安装最新版nodejs

    安装最新版nodejs 更新ubuntu软件源 sudo apt-get update sudo apt-get install -y python-software-properties softw ...

  7. java进程占用系统内存高,排查方法

    查看所有内存占用情况 top 定位线程问题(通过命令查看16764 进程的线程情况) ps p -L -o pcpu,pmem,pid,tid,time,tname,cmd 计数 ps p -L -o ...

  8. dubbo服务降级(1)

    1. 在 dubbo 管理控制台配置服务降级 上图的配置含义是:consumer 调用 com.zhang.HelloService 的方法时,直接返回 null,不发起远程调用. 实际操作是:在 z ...

  9. calibre电子书管理软件

    软件介绍: Calibre 是电子书管理软件,支持 Amazon.Apple.Bookeen.Ectaco.Endless Ideas.Google/HTC.Hanlin Song 设备及格式,功能十 ...

  10. Java中类成员变量初始化顺序

    一. 定义处默认初始化vs构造函数中初始化 java中类成员变量支持在声明处初始化,也可以在构造函数中初始化,那么这两者有什么区别呢?看下面例子 public class FieldsInit { p ...