您的位置:首页 > 编程语言 > Python开发

python matplotlib

2017-11-09 16:30 351 查看
____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

# 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()



subplot绘制多个子图

参考文章:
https://blog.csdn.net/gatieme/article/details/61416645
https://blog.csdn.net/you_are_my_dream/article/details/53439518

子图:就是在一张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()
·



·
其他:
【Matplotlib】详解图像各个部分
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python matplotlib