python学习(17)--Matplotlib
2017-06-15 18:01
218 查看
Matplotlib是一个画图库,这一节我们提供了简明的教程,使用类似于MATLAB.
运行后得到如下图:
![](https://img-blog.csdn.net/20170615175607757?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA3NzQ2OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
我们还可以添加标题,轴坐标等:
得到如下图:
![](https://img-blog.csdn.net/20170615175743102?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA3NzQ2OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
更多信息可以查看此文档
得到如下图:
![](https://img-blog.csdn.net/20170615175854321?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzA3NzQ2OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
更多信息请查看此文档
结果如图:
画图
matplotlib最重要的函数就是plot,可以让你画出2D的图。例如:import numpy as np import matplotlib.pyplot as plt # Compute the x and y coordinates for points on a sine curve x = np.arange(0, 3 * np.pi, 0.1) y = np.sin(x) # Plot the points using matplotlib plt.plot(x, y) plt.show() # You must call plt.show() to make graphics appear.
运行后得到如下图:
我们还可以添加标题,轴坐标等:
import numpy as np import matplotlib.pyplot as plt # 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) plt.plot(x, y_cos) plt.xlabel('x axis label') plt.ylabel('y axis label') plt.title('Sine and Cosine') plt.legend(['Sine', 'Cosine']) plt.show()
得到如下图:
更多信息可以查看此文档
子图
你也可以画不同类型的子图,例如:import numpy as np import matplotlib.pyplot as plt # 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) # 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) # 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()
得到如下图:
更多信息请查看此文档
图片
可以使用imshow函数来展示图片。例如:import numpy as np from scipy.misc import imread, imresize import matplotlib.pyplot as plt img = imread('assets/cat.jpg') img_tinted = img * [1, 0.95, 0.9] # 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()
结果如图:
相关文章推荐
- python学习笔记(1)之如何在python3.x下安装PIL、numpy以及matplotlib
- 读书笔记--python数据可视化--001_matplotlib学习
- python科学计算学习二:matplotlib绘图,图标注释(2)
- 机器学习之旅-重要的Python库:matplotlib
- python 数据可视化 matplotlib学习二 : 对折线图使用颜色映射
- Python 学习之三:NumPy,SciPy,Matplotlib教程
- Python可视化学习(1):Matplotlib的配置
- Python学习(十一)——matplotlib与可视化
- 【python学习笔记】12:用matplotlib绘制3D函数图像
- python数据分析学习笔记-Numpy-Matplotlib-Pandas
- Python--Matplotlib学习1
- python科学计算学习二:matplotlib绘图(极坐标 3D绘图等)(3)
- Python 模块--matplotlib安装和学习
- python 制作图表 matplotlib - 千月的python linux 系统管理指南学习笔记(18)
- 机器学习实战python环境搭建以及numpy和matplotlib安装遇到的各种问题(一)
- Python-学习笔记(三)——开发环境搭建(numpy,scipy,matplotlib)
- 【利用python进行数据分析-学习记录】python-matplotlib中Basemap插件的安装
- python 学习5--matplotlib画图实践
- [python]matplotlib的快速学习
- python画图——matplotlib.plot 学习