python使用matplotlib画图
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画图的更多相关文章
- python使用matplotlib画图,jieba分词、词云、selenuium、图片、音频、视频、文字识别、人脸识别
一.使用matplotlib画图 关注公众号"轻松学编程"了解更多. 使用matplotlib画柱形图 import matplotlib from matplotlib impo ...
- pylab.show()没有显示图形图像(python的matplotlib画图包)
no display name and no $DISPLAY environment variable ============================ @Neil's answer is ...
- Python之matplotlib画图
折线图: Matplotlib处理csv文件 这里写两个其他的. 柱状图,柱状图个人喜欢用pygal来画,有两篇文章 - Pygal之掷骰子 和 pygal之掷骰子 - 2颗面数为6的骰子.下面用ma ...
- python中matplotlib画图
参考 https://blog.csdn.net/u010358304/article/details/78906768 https://www.cnblogs.com/onemorepoint/p/ ...
- python matplotlib画图产生的Type 3 fonts字体没有嵌入问题
ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...
- 使用python中的matplotlib 画图,show后关闭窗口,继续运行命令
使用python中的matplotlib 画图,show后关闭窗口,继续运行命令 在用python中的matplotlib 画图时,show()函数总是要放在最后,且它阻止命令继续往下运行,直到1.0 ...
- 如何在Python中快速画图——使用Jupyter notebook的魔法函数(magic function)matplotlib inline
如何在Python中快速画图--使用Jupyter notebook的魔法函数(magic function)matplotlib inline 先展示一段相关的代码: #we test the ac ...
- python 利用matplotlib中imshow()函数绘图
matplotlib 是python最著名的2D绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中.通过简单的绘图语 ...
- python使用matplotlib绘制折线图教程
Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...
随机推荐
- python +uiautomator 安卓UI控件操作
一.搭建环境 准备:win7.JDK.androidSDK(adt-bundle-windows-x86_64-20140702\sdk).Appium.安卓模拟器(真机也可以),可以到这个地址下载h ...
- JavaScript学习总结(11)——JS常用函数(二)
37. getElementsByClassName ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function getElementsByClassName( ...
- Prism 框架基础架构
概要 Prism提供指导,帮助您更轻松地设计和构建,灵活且易于维护的客户端业务应用程序,这些应用程序可在Windows运行时,Windows Presentation Foundation(WPF)桌 ...
- web显示winform,web打开winform,IE打开winform
前言:为什么要用ie打开winform 个人觉得,winform部署client太麻烦如金蝶··用友,winfrom打补丁太麻烦,加入新功能再部署很费时间:于是就想为什么不能用IE打开呢?这样就不须要 ...
- Autodesk 招聘Revit二次开发咨询顾问,与Autodesk全球团队紧密合作,提高职业生涯的好机会
朋友们, 因为我离开Autodesk的全职工作(变为部分时间工作),我的职位空出.急招这个职位.请踊跃把你周围的朋友推荐给Autodesk. 请将简历发给我转交给Autodesk 我的邮箱yexion ...
- 有关cascade的结构体
/* internal cascade classifier */ typedef struct CvCascadeHaarClassifier { CV_INT_HAAR_CLASSIFIER_FI ...
- windows 2016 配置 VNC 服务
windows 2016 配置 VNC 服务 下载windows版 https://www.realvnc.com/download/vnc/ 安装时勾选 vncserver 进入 "C:\ ...
- 妙味css3课程---1-2、css3中新增的伪类和伪元素有哪些
妙味css3课程---1-2.css3中新增的伪类和伪元素有哪些 一.总结 一句话总结: 1.div:target{}是什么意思? 比如a标签的锚点链接到div,div:target{}就可以找到这个 ...
- OrmLite使用小结(一)
在使用OrmLite过程中,遇到了不少问题.鉴于中文文档比較少,看英文文档又不知道怎样看起.仅仅能遇到问题查找解决方法并整理出来,如有错误,希望能指正! ** 1.模糊条件查询 ** 使用条件查询时. ...
- domino web app
近期在做个项目内容是:原企业的OA仅仅能在电脑桌面处理流程,不能在手机上处理审核功能,但随着企业需求,管理者需求在随时使用手机审核文档,达到及时处理文档及流程的及时性. 要求:1) ...