📕
innohub
  • KEEP LEARNING
  • WebInfo
    • JS 部分运算符操作
    • javascript 中的object array
    • 错误处理以及异常捕获
    • JavaScript Bases
    • LaoZi & Confucius
  • PyInfo
    • Python3 输入与输出
    • Python3OS
    • python 修饰器的使用
    • python3 与mysql数据库连接使用实例
    • Format-specifier
    • CGI初学踩坑
    • Django 项目测试
    • Assert-info
    • 使用ngnix和uWSGI配置Django
    • write|SVN​
    • Matplotlib 基本使用
    • 重读 Python 官方文档
    • Python3 Base
    • python3 多线程
    • Python3 函数解析
    • python3 str 对象的基本操作
    • protocl buffers
    • Iterator-Generator
    • Django base
    • Decorator 2
    • Export to wheel or egg-info
    • 三. 运算、数据及逻辑
  • GoInfo
    • Info
      • Select 语句使用
      • First class function
      • Work Pools
      • StructTag
      • Go struct
      • 常用函数
      • Strings操作
      • Golang Bases
      • 数组以及切片
      • 文件操作
      • Golang 中的指针类型
      • Golang Map 类型
      • Reflection
      • 函数闭包
      • 接口
      • Panic and Recover
      • Go中的错误处理
      • 并发
      • defer usage
      • Method in golang
      • Object-oriented Programming
      • Goalng 包管理机制
  • RustInfo
    • Info
      • 包、crate、模块
      • Rust 中的错误处理
      • 智能指针
      • 泛型 generics
      • 数据布局与内存对齐
      • Functions and methods
      • 无畏并发
      • Actix-web 基本使用
      • Got from guessing game
      • 结构体的使用
      • Rust 中的函数式语言功能
      • 集合类型
      • 枚举以及模式匹配
      • Slice 类型
      • 生命周期
      • 测试
      • Rust 中的所有权
      • trait
      • 不安全 Rust
      • Format_print
      • Rust 通用编程概念
      • Macro
  • OS
    • info
      • 内存屏障 -- Part 1
      • 内存屏障 -- Part 2
      • CPU 上下文切换
      • 文件读写与零拷贝
      • ELF 文件
  • MySql
    • info
      • MySql 架构与历史
      • 02-key
  • kubernetes
    • 第二章 k8s 基本概念
    • 第一章 Kubernetes -- 伟大舵手
  • Redis
    • info
      • Redis 总览
      • 02-underline-ds
  • Shell&CInfo
    • GCC 与make的使用
    • C++ 中 malloc 与 new
    • 位运算符操作
    • Base of C macro
    • C 语言中 extern 关键字的用法
  • Distributed
    • info
      • 分布式理论概述
  • Java
    • info
      • Java 内存模型
  • Prometheus
    • Prometheus -- 不灭之火
Powered by GitBook
On this page
  • 1.简单曲线绘制
  • 2. 创建子图
  • 3.设置图例
  • 1. ax.legend() 自动填充
  • 2.给已存在的元素添加标签
  • 3.plt.legend((l1, l2, l3), ('label1', 'label2', 'label3'))
  • 4.显示图片
  • 1.读入图片
  • 2.图片显示

Was this helpful?

  1. PyInfo

Matplotlib 基本使用

Previouswrite|SVN​Next重读 Python 官方文档

Last updated 5 years ago

Was this helpful?

[TOC]

1.简单曲线绘制

import matplotlib.pyplot as plt
import numpy as np

plt.plot([1, 2, 3, 4], 'r')
plot.ylabel('Simple red line')
plot.show()

plot([x], y, [fmt], data=None, **kwargs)

plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

该函数可以绘制点或者曲线,根据给定的(x, y)坐标值,可以在一个图中绘制两条曲线,可以使用

Line2D 的属性作为关键字参数设置更多的特性。

plt.plot(x, y, 'go--', linewidth=2, markersize=12)
plt.plot(x, y, color='green', marker='o', linestyle='dashed', 
           linewidth=2, marksize=12)

Parameters:\

  • x, y : 给定的点的坐标,默认会将这些点连成曲线,使用参数 'b-' 即蓝色的直线 x坐标可以省略,默认为0-n-1

  • fmt: 指定点或者连线的模式,可用的颜色以及连线的格式如下

    character

    color

    'b'

    blue

    'g'

    green

    'r'

    red

    'c'

    cyan(天蓝)

    'm'

    magenta(紫)

    'y'

    yellow

    'k'

    black

    'w'

    white

    character

    description

    ‘-’

    直线

    ‘--’

    虚线

    '-.'

    短线点相连

    ‘:’

    小圆点

import matplotlib.pyplot as plt
ply.plot([1,2,3,4], [1,4,9,16])
plt.aixs([0, 6, 0, 20])  #  axis()函数接受四个参数,指定横轴以及纵轴的范围

## 控制曲线属性
#使用关键字直接设置属性
plt.plot(x, y, linewidth=2)
#使用setp()命令
line = plt.plot(x, y)
plt.setp(line, color='g', linewidth=2.0)

2. 创建子图

  • plt.subplots((nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=

    None, gridspec_kw=None, **fig_kw)`

使用该函数创建子图布局,返回figure对象,以及一个numpy.ndarray,可以根据位置制定相应的子图对象

nrows, ncols:子图的行列布局】

sharex, sharey指定是否共享坐标轴可以的选项{'none', 'all'm, 'row', 'col'}

squeeze:true时返回的坐标对象的多余维度被压缩

subplots_kw:构建每个子图的关键字的字典

gridspec_kw: 构建布局的关键字

**fig_kw: 所有需要传递给plt.figure() 的关键字参数

import matplotlib.pyplot as plt 
import numpy as np

np.random.seed(189832)    #使用随机数种子,确保每次运行得到的随机数相同
data = np.random.randn(2, 100)  #生成2*100的二维正态分布的随机数

fig, axs = plt.subplots(2q, 2, figsize=(5, 5))
axs[0, 0].hist(data[0])
axs[1, 0].scatter(data[0], data[1])
axs[0, 1].plot(data[0], data[1])
axs[1, 1].hist2d(data[0], data[1])

plt.show()

#创建极坐标系
  >>> fig, axes = plt.subplots(2, 2, subplot_kw=dict(polar=True))
  >>> axes[0, 0].plot(x, y)
  >>> axes[1, 1].scatter(x, y)
  • plt.subplot(nrows, ncols, index, **kwargs)

    在当前的表格中创建一个新的坐标图并且返回,其虚拟位置在index(1-n),当函数猎术以及坐标值都小于10时,可以采用三位数字表示三个参数

注意:创建子图时会覆盖之前创建的重叠的子图

一个figure中只有一个plot时相当于调用了 plt.subplot(111)

创建子图后,如果需要设置每个子图的属性,可以通过

axs[i, j].set_title()设置标题

axs[i, j].set_xlabel() set_ylabel()设置横纵轴标签

Creating a subplot will delete any pre-existing subplot that overlaps
       with it beyond sharing a boundary::
import matplotlib.pyplot as plt
# plot a line, implicitly creating a subplot(111)
plt.plot([1,2,3])
 # now create a subplot which represents the top plot of a grid
 # with 2 rows and 1 column. Since this subplot will overlap the
 # first, the plot (and its axes) previously created, will be removed
plt.subplot(211)
plt.plot(range(12))
plt.subplot(212, facecolor='y') # creates 2nd subplot with yellow background

3.设置图例

通过设置子图,生成了坐标轴对象AxesSuplot ,利用该对象可以设置相应的图形,再调用plt.legend() 进行图样的设置,可以采用三种方式进行调用

1. ax.legend() 自动填充

被加入到图例中的元素被自动检测,通过对每一个设置了label的图形进行检测,直接调用该函数,不需要多余的参数,ax是生成的子图对象(Axessubplot)

f = plt.figure("Legend test")
ax = f.subplot(111)
line = ax.plot([1, 2, 3], label='Line')
ax.legend()
f.show()

2.给已存在的元素添加标签

ax.plot([1, 2, 3])
ax.legend(['A simple line'])
#可以通过这种方式给一个已经在坐标轴上存在的图像设置标签
#这种方式不提倡使用,因为每个元素及其标签的关系仅靠顺序简单维护,容易引起歧义

3.plt.legend((l1, l2, l3), ('label1', 'label2', 'label3'))

对标签进行详细定义,为了对每一个元素的标记进行完全控制,可以元素以及标签以可迭代的序列的方式传入,元素一般为.Artist(lines, patches) 的序列对象,通过参数 handles 传入 ;相对应的标签是string序列

其他参数:

  • loc: 整数或者字符串,用于确定图例的位置

    Location string

    Location code

    'best'

    0

    'upper right'

    1

    'upper left'

    2

    'lower left'

    3

    'lower right'

    4

    'right'

    5

    'center left'

    6

    'center right'

    7

    'lower center '

    8

    'upper center'

    9

    'center'

    10

  • ncol: 图例的列数,默认是1

  • prop: 设置图例字体

  • fontsizse: 整数,浮点数,或者{'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'}设置字体大小

  • fancybox: 确定是否使用圆边 bool

  • shadow: 是否添加图例的阴影

  • framealpha: 设置背景透明度 也可以使用leg.get_frmae().set_alpha(0.5)

  • facecolor: 设置背景色

  • mode: 可以设置为‘expand’ None 设置水平展开

>>> f1 = plt.figure('test')
>>> ax = f1.subplot(2, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Figure' object has no attribute 'subplot'
>>> ax = f1.subplots(2, 1)
>>> ax
array([<matplotlib.axes._subplots.AxesSubplot object at 0x7efec3fe7c50>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x7efec3879f98>],
      dtype=object)
>>> ax[0].set_title('t1')
Text(0.5,1,'t1')
>>> ax[0].set_xlabel('x1')
Text(0.5,0,'x1')
>>> ax[0].set_ylabel('y1')
Text(0,0.5,'y1')
>>> a = ax[0].plot(t1, f(t1), 'bo', t2, f(t2), 'k')
>>> ax[0].legend(a, 'line t1')
<matplotlib.legend.Legend object at 0x7efec2eb0cc0>
>>> ax[1].set_title('t2')
Text(0.5,1,'t2')
>>> ax[1].set_xlabel('x2')
Text(0.5,0,'x2')
>>> ax[1].set_ylabel('y2')
Text(0,0.5,'y2')
>>> b = ax[1].plot(t2, np.cos(np.pi*2*t2), 'r--', label='line t2')
>>> ax[1].legend()
<matplotlib.legend.Legend object at 0x7efec3fe7630>
>>> f1.show()

4.显示图片

1.读入图片

可以使用pillow库将图片读入,并且转换为np.array对象,也可以使用matplotlib.image 进行读入,但是只可以读取png文件,可以直接获得相应的arrays

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img = mpimg.imread('../../doc/_static/stinkbug.png')
print(img)

###利用pillow
>>> img = Image.open('wall.png')
>>> img
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=2000x1325 at 0x7F9D4D26F710>
>>> import numpy as np
>>> img = np.array(img)
>>> img

2.图片显示

imshow(X, #类似与array的对象,其shape可以是(m,n)(m,n,3)(m,n,4),或者是PIL.Image
       cmap=None, # 设置颜色参数
       norm=None, # 
       aspect=None, # 方向参数,【auto|equal|标量】
       interpolation=None, # 
       alpha=None, # 标量,指定透明度
       vmin=None,  # 
       vmax=None, 
       origin=None, 
       extent=None, # 指定图片在坐标系中的位置(left,right,bottom,topqq)
       shape=None, 
       filternorm=1, 
       filterrad=4.0, 
       imlim=None, 
       resample=None, 
       url=None, 
       hold=None, 
       data=None, 
       **kwargs)

可以通过对数组进行预处理而进行数据分析,对于一个三(RDB)、四(RGBA)通道图片而言,可以仅获取单一通道的数据进行分析

img[:,:,0][0] # 获取某一通道的数据



imgplot = plt.imshow(lum_img)  # plt.imshow(, cmap='...')
imgplot.set_cmap('nipy_spectral') # 类似红外成像 'hot'->暖色调
plt.colorbar()
  • 可以使用直方图进行色彩分析

    imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))

You can also specify the clim using the returned object

fig = plt.figure()
a = fig.add_subplot(1, 2, 1)
imgplot = plt.imshow(lum_img)
a.set_title('Before')
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
a = fig.add_subplot(1, 2, 2)
imgplot = plt.imshow(lum_img)
imgplot.set_clim(0.0, 0.7)
a.set_title('After')
plt.colorbar(ticks=[0.1, 0.3, 0.5, 0.7], orientation='horizontal')
matplotlib 图库链接