基本参考于standford大学cs231n课程笔记cs231n-python numpy tutorial
略有删减与补充,欢迎交流指正
这一部分主要介绍最基本的数据可视化操作和图像操作,将会用到matplotlib这个包
绘图
先导入要用的包
import matplotlib.pyplot as plt
import numpy as np
具体操作参考代码及注释
1.最简单的折线图绘制
x = np.arange(0, 3*np.pi, 0.1)
y = np.sin(x)
plt.plot(x,y)
plt.show()
2.进一步完善
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin) #绘制X轴
plt.plot(x, y_cos)#绘制Y轴
plt.xlabel('x axis label')#设置X轴名称
plt.ylabel('y axis label')#设置Y轴名称
plt.title('Sine and Cosine')#设置图标标题
plt.legend(['Sine', 'Cosine'])#设置图表符号说明
plt.show() #展示
3.利用subplot函数绘制多张图
# Compute the x and y coordinates for points on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
#设置一个高度为2 长度为1的图表表格
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
基本的图像操作
img = imread('11.jpg') #读入图片 一个三维矩阵
img_tinted = img * [1, 0.95, 0.9] #对G、B俩维进行细微改变
# Show the original image
plt.subplot(1, 2, 1)
plt.imshow(img)
# Show the tinted image
plt.subplot(1, 2, 2)
# A slight gotcha with imshow is that it might give strange results
# if presented with data that is not uint8. To work around this, we
# explicitly cast the image to uint8 before displaying it.
plt.imshow(np.uint8(img_tinted))
plt.show()
在cs231n后面的课程中将会用到以上基本操作。numpy非常强大!课程笔记只是提了一些基本的操作。