matplotlib 的几种风格 练习
2016-07-01 12:02
525 查看
〇、准备数据
import numpy as np x = np.linspace(0, 5, 10) y = x ** 2
一、matlab风格的API
1.单图from pylab import * figure() plot(x, y, 'r') xlabel('x') ylabel('y') title('title') show()
2.多子图
subplot(1,2,1) plot(x, y, 'r--') subplot(1,2,2) plot(y, x, 'g*-');
二、matplotlib面向对象风格的API:
1.两步走:先创建figure实例、接着创建axes实例
a.单图fig = plt.figure() # 不关心位置 axes = fig.add_subplot(1, 1, 1) # 关心位置 axes = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # left, bottom, width, height (range 0 to 1) axes.plot(x, y, 'r') axes.set_xlabel('x') axes.set_ylabel('y') axes.set_title('title');
b.多子图
fig = plt.figure() # 不关心位置 axes1 = fig.add_subplot(2, 1, 1) axes2 = fig.add_subplot(2, 1, 2) # 关心位置 axes1 = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # main axes axes2 = fig.add_axes([0.2, 0.5, 0.4, 0.3]) # inset axes # main figure axes1.plot(x, y, 'r') axes1.set_xlabel('x') axes1.set_ylabel('y') axes1.set_title('title') # insert axes2.plot(y, x, 'g') axes2.set_xlabel('y') axes2.set_ylabel('x') axes2.set_title('insert title')
2.一步走:同时创建figure、axes实例
a.单图(不关心位置)fig, axes = plt.subplots() axes.plot(x, y, 'r') axes.set_xlabel('x') axes.set_ylabel('y') axes.set_title('title')
b.多子图(不关心位置)
1)单行,或者单列
fig, axes = plt.subplots(nrows=1, ncols=2) for ax in axes: ax.plot(x, y, 'r') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('title')
2)多行多列
fig, axes = plt.subplots(nrows=3, ncols=2, sharex=True) # 此处不能用 for ax in axes: for i in range(6): axes[i//2, i%2].plot(x, y, 'r') axes[i//2, i%2].set_xlabel('x') axes[i//2, i%2].set_ylabel('y') axes[i//2, i%2].set_title('title')
相关文章推荐
- PHP web 安全(个人整理版本)
- FTP配置参数
- laravel发送邮件
- MapReduce基础开发之五分布式下载ftp文件到本地再迁移到hdfs
- PHP读书笔记(5)-结构语句
- PHP领域类型Java中ibatis的API
- Zend Studio For Eclipse 6.1 注册码
- PHP四种传参方式
- php投票系统之增加与删除投票(管理员篇)
- php使用parse_url和parse_str解析URL
- Yii中处理前后台登录新方法
- So Easy-Ntp嵌入式软件移植
- Editplus批量修改编码格式
- PHP日期时间函数之strtotime()
- SD-关于信用释放时的ATP检查的逻辑(note:1324433)
- PHP读书笔记_运算符详解
- yii2框架-yii2的面包屑导航(十八)
- PHP框架CI之源码解读
- laravel框架初次使用
- php学习第二天_特殊类型