matplotlib 的几种风格 练习

简介:

〇、准备数据

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')
本文转自罗兵博客园博客,原文链接:http://www.cnblogs.com/hhh5460/p/5632601.html ,如需转载请自行联系原作者
相关文章
【moment】moment时间前后一个月,小于当天 或15天内
【moment】moment时间前后一个月,小于当天 或15天内
|
关系型数据库 MySQL 索引
MySQL数据表添加字段的三种方式
MySQL数据表添加字段的三种方式
9126 0
|
NoSQL 关系型数据库 MySQL
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
SpringBoot 集成 SpringSecurity + MySQL + JWT 附源码,废话不多直接盘
312 2
QGS
|
NoSQL 网络协议 Redis
Redis7配置哨兵模式(一主二从三哨兵)
Redis7配置哨兵模式(一主二从三哨兵)
QGS
731 1
|
JavaScript
Nest.js 实战 (七):如何生成 SVG 图形验证码
这篇文章介绍了使用NestJS实现Session验证的图形验证码功能的具体步骤。首先,通过powershell代码安装依赖pnpmaddsvg-captcha。然后,在控制器中使用TypeScript代码引入相关依赖,创建一个图形验证码的接口,当请求该接口时,返回一张随机图片验证码。最后,进行了效果演示。
248 0
Nest.js 实战 (七):如何生成 SVG 图形验证码
|
Ubuntu 网络协议 Linux
在Linux中,如何检查和配置IP地址?
在Linux中,如何检查和配置IP地址?
|
存储 Kubernetes Java
在k8S中,容器内日志是怎么采集的?
在k8S中,容器内日志是怎么采集的?
|
弹性计算 固态存储 大数据
阿里云服务器租用一年多少钱?2024年最新版阿里云服务器租用价格表
阿里云服务器价格亲民,2024年最新优惠中,轻量应用服务器2核2G3M带宽仅82元/年,折合6.8元/月;ECS经济型e实例2核2G3M带宽99元/年,新老用户同享;2核4G5M带宽ECS u1实例199元/年。此外,4核16G10M带宽服务器70元/月起,8核32G10M带宽160元/月起。另有GPU服务器优惠,如gn6v最高配置月费4685.20元。系统盘提供高效云盘、SSD云盘和ESSD云盘等多种选择。续费优惠方面,续费一年享7.5折,最长可达3折。详情请参考官方页面获取最准确的报价与活动信息。