9、matplotlib 基础入门

来源:互联网 发布:mac格式化u盘 编辑:程序博客网 时间:2024/06/07 07:02

1. matplotlib 简介

  • matplotlib库中最重要的模块是 matplotlib.pyplot,该模块允许你做出2D图形,功能和MATLAB的作图功能类似。
  • 引入matplotlib.pyplot模块:import matplotlib.pyplot as plt
  • jupyter notebook中使用魔方方法显示图像:%matplotlib inline

2. matplotlib.pyplot 的使用

import matplotlib.pyplot as pltfrom scipy.misc import imread, imsave, imresize  # 通过scipy引入图像读写保存和缩放函数import matplotlib.image as mplimg   # 通过matplotlib引入图片读写函数# 采用 plt.plot() 绘制图像plt.plot(x, y)plt.show()  # You must call plt.show() to make graphics appearplt.plot([1,2,3,4]# 只提供一个list给plot函数时,matplotlib会将它当作y值对待,并自动将x值设为 0 ~ (len(y)-1)# 给图像添加 label、title、lengend、axis、text、gridplt.xlabel('x axis label')plt.ylabel('y axis label')plt.title('Sine and Cosine')plt.legend(['Sine', 'Cosine'])plt.axis([0, 6, 0, 20]) plt.grid(True)plt.text(60, .025, r'$\mu=100,\ \sigma=15$')# text() 命令可以在任意位置添加文本,而 xlabel(),ylabel(),title()只能作用于特定位置。# 把多个图像绘制在一幅图中# Set up a subplot grid that has height 2 and width 1,# and set the first such subplot as active.plt.subplot(2, 1, 1)plt.plot(x, y_sin)       # Make the first plotplt.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')plt.show()              # Show the figure.# 采用 plt.imshow() 显示图像img = imread('assets/cat.jpg')img = mplimg.imread(r"E:\lane.jpg")plt.imshow(img)     # Show the original image# 使用 plt.savefig('figpath.format') 保存图像plt.figure()   # Matplotlib 的图像都位于 Figure 画布中,可以使用 plt.figure 创建一个新画布。plt.figure(figsize=(14, 4))  # 指定图像的大小plt.imshow(img)plt.savefig(r'E:\lane_original.png', dpi = 400, bbox_inches='tight')# 保存的文件格式可以为.png .pdf .jpg 等等,用 dpi 指定图片的分辨率,默认为100# bbox_inches:设置为'tight',则尝试剪除图表周围的空白部分

3. matplotlib中的作图线的属性及设置方式

  • 颜色(color 简写为 c):
蓝色: 'b' (blue)绿色: 'g' (green)红色: 'r' (red)蓝绿色(墨绿色): 'c' (cyan)红紫色(洋红): 'm' (magenta)黄色: 'y' (yellow)黑色: 'k' (black)白色: 'w' (white)灰度表示: e.g. 0.75 ([0,1]内任意浮点数)RGB表示法: e.g. '#2F4F4F' 或 (0.18, 0.31, 0.31)任意合法的html中的颜色表示: e.g. 'red', 'darkslategray'
  • 线型(linestyle 简写为 ls):
实线: '-'虚线: '--'点画线: '-.'点线: ':'点: '.' 
  • 图形标记(标记marker):
圆形: 'o'加号: '+' 叉形: 'x'星型: '*'方形: 's'
  • 图像标记、线宽及透明度
标记大小:markersize 简写为 ms标记表面(内部)颜色:markerfacecolor 简写为 mfc 标记边缘宽度:markeredgewidth 简写为 mew标记边缘颜色:markeredgecolor 简写为 mec线宽:linewidth透明度:alpha,[0,1]之间的浮点数
  • 设置线的属性的几种方式
# 1.直接在plt.plot()中使用'line properties'plt.plot(x, y, 'r--')  # 颜色线型总是组合在一起# 2.运用关键字参数plt.plot(x, y, markersize=15, marker='d', markerfacecolor='g')# 3.混合使用1 和 2plt.plot(x, y, 'r--', markersize=15, marker='d', markerfacecolor='g', linewidth=2)

4. matplotlib 中的绘图函数

  • 线性图
  • 柱状图
  • 直方图
  • 散点图
  • 矩阵绘图
    • 混淆矩阵,三个维度的关系
# 绘制线性图plt.plot(x, y)# 柱状图x = np.arange(5)y1, y2 = np.random.randint(1, 25, size=(2, 5))width = 0.25ax = plt.subplot(1,1,1)ax.bar(x, y1, width, color='r')ax.bar(x+width, y2, width, color='g')ax.set_xticks(x+width)ax.set_xticklabels(['a', 'b', 'c', 'd', 'e'])plt.show()# 绘制直方图plt.hist(np.random.randn(100), bins=10, color='b', alpha=0.3)plt.show()# 绘制散点图x = np.arange(50)y = x + 5 * np.random.rand(50)plt.scatter(x, y)# 矩阵绘图m = np.random.rand(10,10)print mplt.imshow(m, interpolation='nearest', cmap=plt.cm.ocean)plt.colorbar()plt.show()# 显示或者保存完图片后一定要关闭画布,否则将出现图片叠加显示的乱象plt.close()  # closes the current figureplt.close(name)  # where name is a string, closes figure with that labelplt.close('all')  # closes all the figure windows
0 0
原创粉丝点击