【matplotlib】 模块的简单使用

____tz_zs笔记

https://matplotlib.org

Matplotlib 是一个 Python 2D 绘图库,可以在各种平台上以各种硬拷贝格式和交互式环境生成出版质量数据,专为轻松生成简单而强大的可视化而量身定制。

 

图片读取、显示与保存

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
图片读取、显示与保存
使用matplotlib
"""
import matplotlib.pyplot as plt

img = plt.imread('53788-106.jpg')
print(type(img))  # <class 'numpy.ndarray'>
print(img)
''''' 
[[[255 255 255] 
  [255 255 255] 
  [255 255 255] 
  ...,  
  ...,  
  [163 138 108] 
  [156 131 101] 
  [153 128  98]]] 
'''

plt.imshow(img)
plt.show()

plt.imsave('./1.jpg', img)

·

 

折线图

 

# -*- coding: utf-8 -*-
"""
@author: tz_zs
折线图
"""

import matplotlib.pyplot as plt

plt.plot(['1991', '1992', '1993', '1994'], [33, 54, 13, 67])
plt.show()

plt.plot(['1991', '1992', '1993', '1994'], [33, 54, 13, 67])
plt.xticks(rotation=45)  # 指定旋转一个角度
plt.xlabel('time')
plt.ylabel('y')
plt.title('title')
plt.show()

·

·

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
折线图
"""

import matplotlib.pyplot as plt

plt.plot(['1991', '1992', '1993', '1994'], [33, 54, 13, 67], c='red', label='line1')
plt.plot(['1991', '1992', '1993', '1994'], [11, 13, 12, 14], c='yellow', label='line2')  # label 用于图例

plt.legend(loc='best')  # 图例,loc 指定图例的位置,best 表示让 matplotlib 自动决定最合适的位置

plt.xticks(rotation=45)  # 指定旋转一个角度
plt.xlabel('time')
plt.ylabel('y')
plt.title('title')

plt.show()

·

·

 

条形图

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs

条形图的绘制
"""

import matplotlib.pyplot as plt
import numpy as np

label = ['1991', '1992', '1993', '1994']  # 如果没指定,默认是整数值,如:[0 1 2 3] 这种
bar_heights = [33, 54, 13, 67]

bar_positions = np.arange(4) + 1  # [1 2 3 4]
tick_positions = range(1, 5)

fig, ax = plt.subplots()  # 一般,fig用来控制图的,ax是用来实际画图的
ax.bar(left=bar_positions, height=bar_heights, width=0.5)
'''
left 为条形的位置(每个条目与0的距离)
height 为每个条目的高度
width 为条目的宽度
'''

ax.set_xticks(tick_positions)  # 设置 tick 的位置
ax.set_xticklabels(label)  # 设置 tick 的 label

ax.set_xlabel('xlabel')
ax.set_ylabel('ylabel')
ax.set_title('title')

plt.show()

 

·

 

·

 

 

散点图

 

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""

import matplotlib.pyplot as plt

# x, y分别是x坐标和y坐标的列表
x = [1, 2, 3, 4]
y = [2, 3, 4, 5]
plt.scatter(x, y)
plt.show()

·

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import matplotlib.pyplot as plt
import numpy as np

y = np.random.standard_normal((600, 2))
print(y.shape)

plt.figure(figsize=(7, 7))  # 绘制图形的画板尺寸(约为700像素×700像素)
plt.scatter(y[:, 0], y[:, 1], marker='o')  # marker是指定点标记的形状,marker='o'表示圈
plt.grid(True)  # 表示图形添加网格
plt.xlabel("1st")  # X轴加标签‘1st’
plt.ylabel("2nd")  # 表示给Y轴加标签‘2nd’
plt.title("Scatter Plot")  # 图形加标题‘Scatter Plot’
plt.show()

·

·

scatter函数参数详解

 

直方图

直方图是一种对数据分布情况的图形表示,显示各分组频率或数量分布的情况,易于显示各组之间频率或数量的差别。

需要对数据进行分组,然后统计每个分组内数据的数量。

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(0, 100, 50)  # 数据列表
print(x)
"""
[68 67 16 92 68 26 68 87 60 21 63 29 18 73 49 65 44 19 22 73 12 76 93 35 39
 69  0 18 34 31 29 31 51 32 25 44 32  8 83 93 24 15 78 83 15 25 36 53 24 80]
"""
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  # 分组边界
plt.hist(x=x, bins=bins)
plt.xlabel('x')
plt.ylabel('y')
plt.title('title')
plt.show()

·

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs

直方图
"""

import numpy as np
import matplotlib.pyplot as plt

x = np.random.randint(0, 100, 50)  # 数据列表
print(x)
"""
[68 67 16 92 68 26 68 87 60 21 63 29 18 73 49 65 44 19 22 73 12 76 93 35 39
 69  0 18 34 31 29 31 51 32 25 44 32  8 83 93 24 15 78 83 15 25 36 53 24 80]
"""
bins = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]  # 分组边界
plt.hist(x=x, bins=bins)
# plt.hist(x=x, bins=bins, range=(0, 100))
# plt.hist(x=x, bins=8, range=(10, 90))
'''
bins表示分成多少组,如:bins=8,表示分成8组
rang表示取值的范围,这之外的值不予统计显示
'''
plt.xlabel('x')
plt.ylabel('y')
plt.title('title')
plt.show()

fig = plt.figure(figsize=(5, 20))
ax1 = fig.add_subplot(4, 1, 1)
ax2 = fig.add_subplot(4, 1, 2)
ax3 = fig.add_subplot(4, 1, 3)
ax4 = fig.add_subplot(4, 1, 4)

ax1.hist(x=x, bins=bins)  # bins表示分组
ax1.set_title('demo1')

ax2.hist(x=x, bins=10, range=(0, 100))  # bins表示分成多少组,如:bins=10,表示分成10组
ax2.set_title('demo2')

ax3.hist(x=x, bins=8, range=(10, 90))  # rang表示取值的范围,这之外的值不予统计显示
ax3.set_title('demo3')

ax4.hist(x=x, bins=bins, range=(0, 100))
ax4.set_title('demo3')
ax4.set_ylim(-1, 10)  # 设置y轴的显示范围

plt.show()

·

·

·

 

subplot绘制多个子图

 

一、

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs

添加子图
"""

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(6, 4))  # 一个画图的区间,可以使用figsize指定区域的大小形状
ax1 = fig.add_subplot(2, 2, 1)  # 添加子图
ax2 = fig.add_subplot(2, 2, 2)
# 如果一个位置没有指定添加子图,将会会空在那里
ax3 = fig.add_subplot(2, 2, 4)

ax1.plot(['1991', '1992', '1993', '1994'], [33, 54, 13, 67])
ax2.plot(['1991', '1992', '1993', '1994'], [11, 13, 12, 14])

ax3.plot(['1991', '1992', '1993', '1994'], [33, 54, 13, 67], c='red', label='line1')
ax3.plot(['1991', '1992', '1993', '1994'], [11, 13, 12, 14], c='b', label='line2')  # label 用于图例

plt.legend(loc='best')  # 图例,loc 指定图例的位置,best 表示让 matplotlib 自动决定最合适的位置

plt.show()

·

·

 

二、

参考文章:

 

子图:就是在一张figure里面生成多张子图。

Matplotlib对象简介

  •  FigureCanvas  画布
  •  Figure        图
  •  Axes          坐标轴(实际画图的地方)

·

# -*- coding: utf-8 -*-
"""
@author: tz_zs
"""
import numpy as np
import matplotlib.pyplot as plt

square = np.zeros((32, 32))

square[5:10, 5:10] = 1
plt.subplot(221)
plt.imshow(square)

square[15:20, 5:10] = 1
plt.subplot(222)
plt.imshow(square)

square[5:10, 15:20] = 1
plt.subplot(223)
plt.imshow(square)

square[15:20, 15:20] = 1
plt.subplot(224)
plt.imshow(square)
plt.show()

·

·

 

colors

Named colors in matplotlib

 

 

 

 

其他:

【Matplotlib】详解图像各个部分

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值