您的位置:首页 > 编程语言 > Python开发

python使用matplotlib画图

2017-06-15 10:20 579 查看

python使用matplotlib画图

matplotlib库是python最著名的画图库。它提供了一整套和matlab类似的命令API。十分适合交互式地进行制图。

先介绍了怎样使用matplotlib进行柱状图的绘制

使用matplotlib进行柱状图的绘制仅仅须要三个步骤


第一:导入matplotlib包

第二:调用bar函数进行设置

第三:调用show( )将其显示就可以



当中bar函数提供了很多參数,比如left、height、width、label、yerr等等,以下进行一一说明;

left:柱形的左边缘的位置

height:柱形的高度

width:柱形的宽度

label:标注

(1)调用bar函数时,使用left、height属性

import matplotlib.pyplot as plt
#left:柱形的左边缘的位置,我们指定为0.3。那么当前柱形的左边缘的x值就是0.3
#height:柱形的高度。也就是y轴的值了
plt.bar(left=0.3,height=1)
plt.show()




(2)调用bar函数时,使用left、height、width属性

import matplotlib.pyplot as plt
#除了能够设置左边界。高度,还能够设置宽度width
plt.bar(left=0.3,height=2,width=2)
plt.show()


(3)调用bar函数时,left、height、width属性能够设置多个值。但必须长度一致

import matplotlib.pyplot as plt
#left、height、width能够设置多个值,可是,这三个的长度假设要设置的话,必须长度要一致
plt.bar(left=(0.2,1),height=(2,1),width=(0.2,0.5))
#plt.bar(left=(0.2,1,2),height=(2,1),width=(0.2,0.5))报错,长度不一致
plt.show()




(4)以下是对图形进行一些表示:比如x轴、y轴的含义、标题、说明等

import matplotlib.pyplot as plt
#plt.xlabel(u'性别')  #中文不能显示,会乱码
plt.xlabel('sex')
plt.ylabel('num')
plt.xticks((0.2,1),('male','female'))#为每一个bar进行说明,前面的位置。后面的对应位置的说明
##plt.xticks的使用方法和我们前面说到的left,height的使用方法差点儿相同。\
##假设你有几个bar,那么就是几维的元组。第一个是文字的位置,第二个是详细的文字说明。
##只是这里有个问题,非常显然我们指定的位置有些“偏移”。最理想的状态应该在每一个矩形的中间。

##你能够更改(0,1)=>( (0.2+0.2)/2 ,(1+0.5)/2 )只是这样比較麻烦。
#我们能够通过直接指定bar方法里面的align="center"就能够让文字居中了。
plt.bar(left=(0.2,1),height=(2,1),width=(0.2,0.5),align='center',label="wu",xerr=0.0000,yerr=0.000001)#yerr能够使得顶部留有一定的空间
plt.title('wojiushimogui') #标题
plt.legend(loc = 'upper right')#图例

plt.show()




此库中的plot( )函数,与matlab中的plot函数基本一样

matplotlib.pyplot.plot(*args, **kwargs)

參数的说明:args is a variable length argument, allowing for multiple x, y pairs with an optional format string. (意思是:參数是一个可变长度參数,同意多个x,y对与一个可选的格式字符串。)

For example, each of the following is legal:


plot(x, y) # plot x and y using default line style and color

plot(x, y, ‘bo’) # plot x and y using blue circle markers

plot(y) # plot y using x as index array 0..N-1

plot(y, ‘r+’) # ditto, but with red plusses



看一个简单的样例

import matplotlib.pyplot as plt
L=[x*x for x in range (100)]
for i in range(100):
plt.plot(i,L[i],'bo')

plt.show()




实际上遇到的源码作为样例贴上:用到的就是上面plot函数的一个简单的使用方法。

# show your cluster only available with 2-D data
#centroids为k个类别。当中保存着每一个类别的质心
#clusterAssment为样本的标记,第一列为此样本的类别号。第二列为到此类别质心的距离
def showCluster(dataSet, k, centroids, clusterAssment):
numSamples, dim = dataSet.shape
if dim != 2:
print ("Sorry! I can not draw because the dimension of your data is not 2!")
return 1

mark = ['or', 'ob', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']
if k > len(mark):
print ("Sorry! Your k is too large! please contact wojiushimogui")
return 1

# draw all samples
for i in range(numSamples):
markIndex = int(clusterAssment[i, 0])  #为样本指定颜色
plt.plot(dataSet[i, 0], dataSet[i, 1], mark[markIndex])

mark = ['Dr', 'Db', 'Dg', 'Dk', '^b', '+b', 'sb', 'db', '<b', 'pb']
# draw the centroids
for i in range(k):  #画每一个类的质心点
plt.plot(centroids[i, 0], centroids[i, 1], mark[i], markersize = 12)

plt.show()


參考资料:http://www.cnblogs.com/qianlifeng/archive/2012/02/13/2350086.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: