python中的matplotlib(1)
来源:互联网 发布:无线接入网络关键技术 编辑:程序博客网 时间:2024/05/22 00:38
调用matplotlib画图的流程:
- 调用figure()得到fig对象
- 调用fig.add_subplot(111)得到axis对象
- 调用plt.plot绘制
- plt.show()显示出figure
add_subplot()
返回一个axes对象,里面的参数abc表示在一个figure窗口中,有a行b列个小窗口,然后本次plot在第c个窗口中
from numpy import *import matplotlib.pyplot as plt x=arange(0,10,0.1) # [ 0. 0.1 0.2 ..., 9.7 9.8 9.9]print(len(x))y=random.randn(len(x)) fig=plt.figure() ax=fig.add_subplot(111) plt.plot(x,y) ax.set_title('random numbers') plt.show()
结果:
如果一块画布中要显示多个图:
from numpy import *import matplotlib.pyplot as plt fig = plt.figure()ax = fig.add_subplot(2,1,1)ax.plot(x,y)ax = fig.add_subplot(2,2,3)ax.plot(x,y)plt.show()
结果:
画散点图scatter
matplotlib.pyplot.scatter(x,y,s=20,c=’b’,marker=’o’,cmap=None,norm=None,vmin=None,
vmax=None,linewidths=None,verts=None,hold=None,**kwargs)
绘制散点图时,其中x和y是相同长度的数组:
其中散点的形状参数marker如下:
其中颜色参数c如下:
b—(blue) g–(green) k—(blace) y–(yellow)
c—(cyan) m–(magenta) r–(red) w–(white )
显示标题,坐标轴,和图标:
from numpy import *import matplotlib.pyplot as plt #产生测试数据 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #设置标题 ax1.set_title('Scatter Plot') #设置X轴标签 plt.xlabel('X') #设置Y轴标签 plt.ylabel('Y') #画散点图 ax1.scatter(x,y,c = 'r',marker = 'o') #设置图标 plt.legend('y') #显示所画的图 plt.show()
标记不同大小:
from numpy import *import matplotlib.pyplot as plt #产生测试数据 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #设置标题 ax1.set_title('Scatter Plot') #设置X轴标签 plt.xlabel('X') #设置Y轴标签 plt.ylabel('Y') # ..........................#画散点图 sValue = x*10 ax1.scatter(x,y,s=sValue,c='r',marker='x') #设置图标 plt.legend('x1') #显示所画的图 plt.show()
标记不同颜色:
from numpy import *#import operator # 运算符模块,执行排序操作时将用到import matplotlib.pyplot as plt #产生测试数据 x = np.arange(1,10) y = x fig = plt.figure() ax1 = fig.add_subplot(111) #设置标题 ax1.set_title('Scatter Plot') #设置X轴标签 plt.xlabel('X') #设置Y轴标签 plt.ylabel('Y') #画散点图 cValue = ['r','y','g','b','r','y','g','b','r'] ax1.scatter(x,y,c=cValue,marker='s') #设置图标 plt.legend('x1') #显示所画的图 plt.show()
线宽linewidths
from numpy import *import matplotlib.pyplot as plt #产生测试数据 x = arange(1,10)y = x z=[1,1,1,2,2,2,3,3,3]print(z) fig = plt.figure() ax1 = fig.add_subplot(111) #设置标题 ax1.set_title('Scatter Plot') #设置X轴标签 plt.xlabel('X') #设置Y轴标签 plt.ylabel('Y') #画散点图,其中c=z表示有1,2,3种颜色,s=100表示固定大小为100ax1.scatter(x,y,c=z,s=100,marker='o') #设置图标 plt.legend('x1') #显示所画的图 plt.show()
当然也可以让其图标大小和颜色随样本的属性而变化:
from numpy import *import matplotlib.pyplot as plt #产生测试数据 x = arange(1,10)y = x z=[1,1,1,2,2,2,3,3,3]print(z) fig = plt.figure() ax1 = fig.add_subplot(111) #设置标题 ax1.set_title('Scatter Plot') #设置X轴标签 plt.xlabel('X') #设置Y轴标签 plt.ylabel('Y') #画散点图 #ax1.scatter(x,y,c=z,s=100,marker='o') ax1.scatter(x,y,c=x,s=50*x,marker='o')#设置图标 plt.legend('x1') #显示所画的图 plt.show()
matplotlib的matplotlib.pyplot
在机器学习的决策树中要绘制树形图,会用到pyplot函数
效果如下:
>>> import matplotlib.pyplot as plt>>> fig = plt.figure(1, facecolor='white') #创建新图形,背景为白色>>> fig.clf() # 清空绘图区>>> createPlot.ax1 = plt.subplot(111, frameon=True)
>>> createPlot.ax1 = plt.subplot(111, frameon=False)
import matplotlib.pyplot as pltdef createPlot(): fig = plt.figure(1, facecolor='white') #创建新图形 fig.clf() # 清空绘图区 createPlot.ax1 = plt.subplot(121, frameon=True) createPlot.ax1 = plt.subplot(122, frameon=False) plt.show()createPlot()
其他的以后用到了再添加。。。。。
参考:
http://blog.csdn.net/pipisorry/article/details/40005163
http://www.cnblogs.com/bovine/archive/2012/11/09/2763374.html
http://blog.csdn.net/anneqiqi/article/details/64125186
- python中的matplotlib(1)
- Python中的matplotlib用法(转载)
- 入门python中的matplotlib
- python中的matplotlib用法
- Python 中的matplotlib库
- python中的matplotlib绘图问题
- Python中的matplotlib用法总结
- python画图matplotlib的Legend(显示图中的标签)
- python:matplotlib及pandas绘图(1)
- python用matplotlib画图例子(1)
- 1-Python 安装 Matplotlib
- Python--Matplotlib学习1
- python画图:matplotlib(1)
- 使用python中的Matplotlib绘图示例
- 使用python中的Matplotlib绘图示例(续)
- Python中的Numpy、SciPy、MatPlotLib安装教程
- Python 中的绘图matplotlib & mayavi库
- python Matplotlib 学习笔记(1)
- jqGrid JS之TreeGrid (树形表格)- yellowcong
- Java 并发--- 线程间协作的两种方式:wait、notify、notifyAll和Condition
- android代码控制软键盘的打开与关闭的工具类(KeybordS)
- 劳动争议纠纷案件可以找风险代理吗
- ionic如何使用cordova-plugin-alipay支付
- python中的matplotlib(1)
- IntelliJ IDEA WEB项目的部署配置
- 容斥原理基本概念与例题
- tabar突起按钮点击
- 全球化背景下的物联网产业发展
- 一些程序员可以了解的项目/内容
- 代理注册公司的风险大吗?
- ASP.NET MVC5 怒跨 Linux 平台
- JVM知识点总览-中高级Java工程师面试必备