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

python使用matplotlib画图的更多相关文章

  1. python使用matplotlib画图,jieba分词、词云、selenuium、图片、音频、视频、文字识别、人脸识别

    一.使用matplotlib画图 关注公众号"轻松学编程"了解更多. 使用matplotlib画柱形图 import matplotlib from matplotlib impo ...

  2. pylab.show()没有显示图形图像(python的matplotlib画图包)

    no display name and no $DISPLAY environment variable ============================ @Neil's answer is ...

  3. Python之matplotlib画图

    折线图: Matplotlib处理csv文件 这里写两个其他的. 柱状图,柱状图个人喜欢用pygal来画,有两篇文章 - Pygal之掷骰子 和 pygal之掷骰子 - 2颗面数为6的骰子.下面用ma ...

  4. python中matplotlib画图

    参考 https://blog.csdn.net/u010358304/article/details/78906768 https://www.cnblogs.com/onemorepoint/p/ ...

  5. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题

    ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...

  6. 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令

    使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...

  7. 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline

    如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...

  8. python 利用matplotlib中imshow()函数绘图

    matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.通过简单的绘图语 ...

  9. python使用matplotlib绘制折线图教程

    Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...

随机推荐

  1. jQuery简单tab按钮切换

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 一款开源Office软件---Lotus Symphony在Linux系统下的应用

    点击下载观看试用录像   Linux系统下的办公软件有OpenOffice.永中Office.红旗Red Office.金山Wps Office及StarOffice等,今天我为大家介绍IBM推进军O ...

  3. python3 时间处理

    1 标记当前时间 import datetime from dateutil import tz #标记当前时间为中国时间 注意(replace 只有标记的意思没有转化的意思) datetime.da ...

  4. Android启动原理剖析

    我们知道Android是以一个Activity为单位的,可是我们并没有看到一个Activity是怎么開始启动的. 今天我 们就从Android的源码開始讲吧. ActivityThread: Andr ...

  5. Property-属性动画

    今天第一次接触到属性动画.参考着 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475 的博客,自己学习下. 它的区别跟 ...

  6. php实现遍历文件目录

    php实现遍历文件目录 一.总结 1.熟悉简单:很经典的例子,多看,然后发现熟悉了很简单 二.php实现遍历目录 php实现遍历目录 代码一: //遍历目录 function iteral($path ...

  7. 1.lombok系列1:初识lombok

    转自:https://www.imooc.com/article/18156 初识lombok 官网:https://projectlombok.org/ 什么是lombok 连官网都懒得废话,只给出 ...

  8. cc1.exe -fno-stack-protector

    # github.com/mattn/go-sqlite3 cc1.exe: error: unrecognized command line option "-fno-stack-prot ...

  9. position记录

    1.  relative(相对定位):生成相对定位的元素,通过top,bottom,left,right的设置相对于其正常(原先本身)位置进行定位.可通过z-index进行层次分级.均是以父级的左上角 ...

  10. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...