python matplotlib 基础

简介

matplotlib是python里面的一个专业绘图工具库,如果想通过python来绘制漂亮的图形,那么一定少不了它了。

准备

在开始画图之前需要安装numpy以及matplotlib库,当然python基本库也必不可少,numpy是一个专业的数组,矩阵处理工具。

Python
Numpy- this is the module which does most array and mathematical manip-

ulation
Matplotlib- this is the module you will be using for plotting

You can check these are installed by going to a terminal and typing: 

导入目标库:

$ python
>>> import numpy as np
>>> import pylab as pl

基本绘图

直线或曲线

接下来咱们尝试一下基础的绘图:

****************************************************
# lineplot.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# show the plot on the screen
pl.show()
****************************************************

散点图 scatter plot

****************************************************
# scatterplot.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y as red circles
pl.plot(x, y, ’ro’)
# show the plot on the screen
pl.show()
****************************************************


图形的基本属性

当图形中元素太多,可以通过改变点的颜色,大小,以及形状来区分图中的元素,matplot可以设置下列基本颜色:


pl.plot(x, y, ’r’)
这就能划出一条红色的线。

如果要改变线的形态呢?有以下形态的线:

['-' |'--' |'-.' |':' |'None' |' ' |'']

plot(x,y, ’--’)
划出的就是一条虚线。

以及还能改变marker的形态:

如果我用以下代码,画出的就是蓝色的星型标记:

plot(x,y, ’b*’)

label

画完图当然得有标记,可以用xlabel和ylabel方法分别标记图:

pl.xlabel(’put text here’)
pl.ylabel(’put text here’)

当然还不能缺了title:

pl.title(’Put plot title here’)

如果发现图的比例不太合适,那么咱们可以调节图的横竖轴坐标范围:

pl.xlim(x_low, x_high)
pl.ylim(y_low, y_high)

以下是一个示例:

****************************************************
#lineplotAxis.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# give plot a title
pl.title(’Plot of y vs. x’)
# make axis labels
pl.xlabel(’x axis’)
pl.ylabel(’y axis’)
# set axis limits
pl.xlim(0.0, 7.0)
pl.ylim(0.0, 30.)
# show the plot on the screen
pl.show()
****************************************************


多个图形

在一张图上画多个图形,可以连续的使用plot方法,注意的是为了方便区分图形,可以使用不同的颜色:

plot(x1, y1, ’r’)
plot(x2, y2, ’g’)

Figure legends 

matplot的legend语法如下:

pl.legend((plot1, plot2), (’label1, label2’), ’best’, numpoints=1)

第一个参数是咱们需要标记的图形,如果画面上又多个图形,那么可以用括号括起来,第二个参数是具体的label,就是咱们希望说明给看图的人这个图形表示的是什么,第三个参数表示的是我们希望将图放在什么位置,best表示系统自动放于最佳位置,当然可以自定义为 ‘upper right’, ‘upper left’, ‘center’, ‘lower left’, ‘lower right’. 

以下是一个示例图:

****************************************************
# lineplotFigLegend.py
import numpy as np
import pylab as pl
# Make x, y arrays for each graph
x1 = [1, 2, 3, 4, 5]
y1 = [1, 4, 9, 16, 25]
x2 = [1, 2, 4, 6, 8]
y2 = [2, 4, 8, 12, 16]
# use pylab to plot x and y : Give your plots names
plot1 = pl.plot(x1, y1, ’r’)
plot2 = pl.plot(x2, y2, ’go’)
# give plot a title
pl.title(’Plot of y vs. x’)
# make axis labels
pl.xlabel(’x axis’)
pl.ylabel(’y axis’)
# set axis limits
pl.xlim(0.0, 9.0)
pl.ylim(0.0, 30.)
# make legend
pl.legend([plot1, plot2], (’red line’, ’green circles’), ’best’, numpoints=1)
# show the plot on the screen
pl.show()
****************************************************


直方图

以下是一个画直方图的例子:

****************************************************
# histplot.py
import numpy as np
import pylab as pl
# make an array of random numbers with a gaussian distribution with
# mean = 5.0
# rms = 3.0
# number of points = 1000
data = np.random.normal(5.0, 3.0, 1000)
# make a histogram of the data array
pl.hist(data)
# make plot labels
pl.xlabel(’data’)
pl.show()
****************************************************
这里先用均值为5,方差为3的高斯分布生成了1000个点,再讲这些点做直方图,左图是默认的格式,右图是以下语句:

pl.hist(data, histtype=’stepfilled’)



当然也可以手动的设置直方图中bin的大小多少:

bins = np.arange(-5., 16., 1.)
pl.hist(data, bins, histtype=’stepfilled’)

一张画布多张图

如果我们想要在一张图上表现多个图,可以用subplot:

fig1 = pl.figure(1)
pl.subplot(212)

 
 

正如上图这样,subplot中的前两位表示i*j个图形,这边21表示的是两行一列,最后一位表示的是第几个图形。

pl.subplots_adjust(left=0.08, right=0.95, wspace=0.25, hspace=0.45)
咱们还可以调整画布的空白布局。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值