python使用matplotlib绘图

时间:2022-12-21 08:55:06

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()

python使用matplotlib绘图
(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()

python使用matplotlib绘图
(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()

python使用matplotlib绘图

此库中的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()

python使用matplotlib绘图
实际上遇到的源代码作为例子贴上:用到的就是上面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