标签:data visualization 数据可视化 matplotlib numpy
matplotlib是python里面的一个专业绘图工具库,如果想通过python来绘制漂亮的图形,那么一定少不了它了。
? 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() ****************************************************
**************************************************** # 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*’)
pl.xlabel(’put text here’) pl.ylabel(’put text here’)
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(x1, y1, ’r’) plot(x2, y2, ’g’)
pl.legend((plot1, plot2), (’label1, label2’), ’best’, numpoints=1)
以下是一个示例图:
**************************************************** # 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’)
bins = np.arange(-5., 16., 1.) pl.hist(data, bins, histtype=’stepfilled’)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:data visualization 数据可视化 matplotlib numpy
原文地址:http://blog.csdn.net/frog_in_a_well/article/details/47174463