matplotlib.pyplot介绍
来源:互联网 发布:linux命令自动补全 编辑:程序博客网 时间:2024/06/07 12:21
matplotlib.pyplot介绍
matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表。
matplotlib.pyplot是命令行式函数的集合,每一个函数都对图像作了修改,比如创建图形,在图像上创建画图区域,在画图区域上画线,在线上标注等。
下面简单介绍一下pyplot的基本使用:
(1)使用plot()函数画图
plot()为画线函数,下面的小例子给ploy()一个列表数据[1,2,3,4],matplotlib假设它是y轴的数值序列,然后会自动产生x轴的值,因为python是从0作为起始的,所以这里x轴的序列对应为[0,1,2,3]。
import matplotlib.pyplot as pltplt.plot([1,2,3,4])plt.ylabel('some numbers') #为y轴加注释plt.show()
plot()还可以接受x,y成对的参数,还有一个可选的参数是表示线的标记和颜色,plot函数默认画线是蓝色实线,即字符串'b-',你可以选择自己喜欢的标记和颜色。
import matplotlib.pyplot as pltplt.plot([1,2,3,4], [1,4,9,16], 'ro')plt.axis([0, 6, 0, 20])plt.show()
axis()函数给出了形如[xmin,xmax,ymin,ymax]的列表,指定了坐标轴的范围。这对于数值的处理来说非常有用,比如给出一个numpy数组(arrays),下面小例子给出了不同的线。
import numpy as npimport matplotlib.pyplot as plt# evenly sampled time at 200ms intervalst = np.arange(0., 5., 0.2)# red dashes, blue squares and green trianglesplt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')plt.show()
(2)线的属性
可以用不同方式对线的属性进行设置:
用参数的关键字
plt.plot(x, y, linewidth=2.0)通过这种方式修改线宽
使用Line2D实例的set方法
plot函数返回一个线的列表,比如line1,line2 = plot(x1,y1,x2,y2)。由于我们只有一条直线,对于长度为1的列表(list),我们可以用逗号,来得到列表第一个元素
line, = plt.plot(x,y,'-')line.set_antialiased(False) #关闭抗锯齿像素
使用pyplot的setp()命令
还可以用setp()命令来进行设置,该命令可以对一个列表或者单个对象进行设置,并且提供了matlab式的使用方法
lines = plt.plot(x1, y1, x2, y2)# use keyword argsplt.setp(lines, color='r', linewidth=2.0)# or MATLAB style string value pairsplt.setp(lines, 'color', 'r', 'linewidth', 2.0)
Line2D的属性
(3)多个图像
pyplot和MATLAB一样,都有当前图像和当前坐标的概念,所有命令都是对当前的坐标进行设置。
gca()返回当前的坐标实例(a matplotlib.axes.Axes instance),gcf()返回当前图像(matplotlib.figure.Figure instance)。
下面的小例子是产生两个子图像。
import numpy as npimport matplotlib.pyplot as pltdef f(t):return np.exp(-t) * np.cos(2*np.pi*t)t1 = np.arange(0.0, 5.0, 0.1)t2 = np.arange(0.0, 5.0, 0.02)plt.figure(1)plt.subplot(211)plt.plot(t1, f(t1), 'bo', t2, f(t2), 'k')plt.subplot(212)plt.plot(t2, np.cos(2*np.pi*t2), 'r--')plt.show()
figure()命令时可选的,因为figure(1)是默认创建的。
subplot()命令会指定一个坐标系,默认是subplot(111),111参数分别说明行的数目numrows,列的数目numcols,第几个图像fignum(fignum的范围从1到numrows*numcols)。
subplot(211)指定显示两行,每行一图,接下来为第一幅图像。
可以用clf()来清空当前图像,用cla()来清空当前坐标。
(4)为图像做文本说明
text()命令可以用于在任意位置添加文本,而xlabel(),ylabel(),title()用来在指定位置添加文本。所有的text()命令返回一个matplotlib.text.Text实例,也可以通过关键字或者setp()函数对文本的属性进行设置。
import numpy as npimport matplotlib.pyplot as pltmu, sigma = 100, 15x = mu + sigma * np.random.randn(10000)# the histogram of the datan, bins, patches = plt.hist(x, 50, normed=1, facecolor='g', alpha=0.75)plt.xlabel('Smarts')plt.ylabel('Probability')plt.title('Histogram of IQ')plt.text(60, .025, r'$\mu=100,\ \sigma=15$')plt.axis([40, 160, 0, 0.03])plt.grid(True)plt.show()
转载请注明作者Jason Ding及其出处
Github主页(http://jasonding1354.github.io/)
CSDN博客(http://blog.csdn.net/jasonding1354)
简书主页(http://www.jianshu.com/users/2bd9b48f6ea8/latest_articles)- 【python】matplotlib.pyplot介绍
- matplotlib.pyplot介绍
- matplotlib.pyplot介绍
- matplotlib.pyplot
- matplotlib.pyplot
- python matplotlib.pyplot importerror
- matplotlib.pyplot.imshow
- matplotlib.pyplot画图
- matplotlib.pyplot入门
- Python---matplotlib.pyplot
- matplotlib.pyplot.py
- 学习matplotlib--pyplot教程
- 【python】matplotlib.pyplot入门
- import matplotlib.pyplot 错误
- matplotlib.pyplot.figure
- matplotlib.pyplot.subplot
- matplotlib入门之Pyplot
- matplotlib.pyplot.hist(柱状图)
- 图像分割 1.基于阈值的算法
- 初识Vue.js
- 微信小程序开发文档详细讲解
- LeetCode 42. Trapping Rain Water
- 1
- matplotlib.pyplot介绍
- STM32学习之路-AIRCR寄存器PRIGROUP位的配置<NIVC
- C#三层架构详解--以系统登录为例
- 2
- oracle11.2.0.4 impdp导入报错:ORA-39083,ORA-02304
- linux 下面如何安装Eigen
- 合并石子(三重dp)
- 心少朴-新
- 字符串间最短距离(动态规划)