python二维图颜色函数_Python绘图之二维图与三维图详解
各位工程师累了吗? 推荐一篇可以让你技术能力达到出神入化的网站"持久男"
1.二维绘图
a. 一维数据集
用 Numpy ndarray 作为数据传入 ply
1.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(10)
print "y = %s"% y
x = range(len(y))
print "x=%s"% x
plt.plot(y)
plt.show()
2.操纵坐标轴和增加网格及标签的函数
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(10)
plt.plot(y.cumsum())
plt.grid(True) ##增加格点
plt.axis('tight') # 坐标轴适应数据量 axis 设置坐标轴
plt.show()
3.plt.xlim 和 plt.ylim 设置每个坐标轴的最小值和最大值
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(20)
plt.plot(y.cumsum())
plt.grid(True) ##增加格点
plt.xlim(-1,20)
plt.ylim(np.min(y.cumsum())- 1, np.max(y.cumsum()) + 1)
plt.show()
4. 添加标题和标签 plt.title, plt.xlabe, plt.ylabel 离散点, 线
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(1000)
y = np.random.standard_normal(20)
plt.figure(figsize=(7,4)) #画布大小
plt.plot(y.cumsum(),'b',lw = 1.5) # 蓝色的线
plt.plot(y.cumsum(),'ro') #离散的点
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple Plot')
plt.show()
b. 二维数据集
np.random.seed(2000)
y = np.random.standard_normal((10, 2)).cumsum(axis=0) #10行2列 在这个数组上调用cumsum 计算赝本数据在0轴(即第一维)上的总和
print y
1.两个数据集绘图
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.plot(y, lw = 1.5)
plt.plot(y, 'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
plt.show()
2.添加图例 plt.legend(loc = 0)
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,1], lw = 1.5, label = '2st')
plt.plot(y, 'ro')
plt.grid(True)
plt.legend(loc = 0) #图例位置自动
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
plt.show()
3.使用2个 Y轴(左右)fig, ax1 = plt.subplots() ax2 = ax1.twinx()
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
fig, ax1 = plt.subplots() # 关键代码1 plt first data set using first (left) axis
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #图例位置自动
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('A simple plot')
ax2 = ax1.twinx() #关键代码2 plt second data set using second(right) axis
plt.plot(y[:,1],'g', lw = 1.5, label = '2nd')
plt.plot(y[:,1], 'ro')
plt.legend(loc = 0)
plt.ylabel('value 2nd')
plt.show()
4.使用两个子图(上下,左右)plt.subplot(211)
通过使用 plt.subplots 函数,可以直接访问底层绘图对象,例如可以用它生成和第一个子图共享 x 轴的第二个子图.
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(7,5))
plt.subplot(211) #两行一列,第一个图
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #图例位置自动
plt.axis('tight')
plt.ylabel('value')
plt.title('A simple plot')
plt.subplot(212) #两行一列.第二个图
plt.plot(y[:,1],'g', lw = 1.5, label = '2nd')
plt.plot(y[:,1], 'ro')
plt.grid(True)
plt.legend(loc = 0)
plt.xlabel('index')
plt.ylabel('value 2nd')
plt.axis('tight')
plt.show()
5.左右子图
有时候,选择两个不同的图标类型来可视化数据可能是必要的或者是理想的.利用子图方法:
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((10, 2))
plt.figure(figsize=(10,5))
plt.subplot(121) #两行一列,第一个图
plt.plot(y[:,0], lw = 1.5,label = '1st')
plt.plot(y[:,0], 'ro')
plt.grid(True)
plt.legend(loc = 0) #图例位置自动
plt.axis('tight')
plt.xlabel('index')
plt.ylabel('value')
plt.title('1st Data Set')
plt.subplot(122)
plt.bar(np.arange(len(y)), y[:,1],width=0.5, color='g',label = '2nc')
plt.grid(True)
plt.legend(loc=0)
plt.axis('tight')
plt.xlabel('index')
plt.title('2nd Data Set')
plt.show()
c.其他绘图样式,散点图,直方图等
1.散点图
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.scatter(y[:,0],y[:,1],marker='o')
plt.grid(True)
plt.xlabel('1st')
plt.ylabel('2nd')
plt.title('Scatter Plot')
plt.show()
2.直方图 plt.hist
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.hist(y,label=['1st','2nd'],bins=25)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()
3.直方图 同一个图中堆叠
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
plt.figure(figsize=(7,5))
plt.hist(y,label=['1st','2nd'],color=['b','g'],stacked=True,bins=20)
plt.grid(True)
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()
4.箱型图 boxplot
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((1000, 2))
fig, ax = plt.subplots(figsize=(7,4))
plt.boxplot(y)
plt.grid(True)
plt.setp(ax,xticklabels=['1st' , '2nd'])
plt.xlabel('value')
plt.ylabel('frequency')
plt.title('Histogram')
plt.show()
5.绘制函数
from matplotlib.patches import Polygon
import numpy as np
import matplotlib.pyplot as plt
#1. 定义积分函数
def func(x):
return 0.5 * np.exp(x)+1
#2.定义积分区间
a,b = 0.5, 1.5
x = np.linspace(0, 2 )
y = func(x)
#3.绘制函数图形
fig, ax = plt.subplots(figsize=(7,5))
plt.plot(x,y, 'b',linewidth=2)
plt.ylim(ymin=0)
#4.核心, 我们使用Polygon函数生成阴影部分,表示积分面积:
Ix = np.linspace(a,b)
Iy = func(Ix)
verts = [(a,0)] + list(zip(Ix, Iy))+[(b,0)]
poly = Polygon(verts,facecolor='0.7',edgecolor = '0.5')
ax.add_patch(poly)
#5.用plt.text和plt.figtext在图表上添加数学公式和一些坐标轴标签。
plt.text(0.5 *(a+b),1,r"$\int_a^b f(x)\mathrm{d}x$", horizontalalignment ='center',fontsize=20)
plt.figtext(0.9, 0.075,'$x$')
plt.figtext(0.075, 0.9, '$f(x)$')
#6. 分别设置x,y刻度标签的位置。
ax.set_xticks((a,b))
ax.set_xticklabels(('$a$','$b$'))
ax.set_yticks([func(a),func(b)])
ax.set_yticklabels(('$f(a)$','$f(b)$'))
plt.grid(True)
2.金融学图表 matplotlib.finance
1.烛柱图 candlestick
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
fig, ax = plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom = 0.2)
mpf.candlestick(ax, quotes, width=0.6, colorup='b',colordown='r')
plt.grid(True)
ax.xaxis_date() #x轴上的日期
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期倾斜
plt.show()
2. plot_day_summary
该函数提供了一个相当类似的图标类型,使用方法和 candlestick 函数相同,使用类似的参数. 这里开盘价和收盘价不是由彩色矩形表示,而是由两条短水平线表示.
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
fig, ax = plt.subplots(figsize=(8,5))
fig.subplots_adjust(bottom = 0.2)
mpf.plot_day_summary(ax, quotes, colorup='b',colordown='r')
plt.grid(True)
ax.xaxis_date() #x轴上的日期
ax.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30) #日期倾斜
plt.show()
3.股价数据和成交量
#!/etc/bin/python
#coding=utf-8
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.finance as mpf
start = (2014, 5,1)
end = (2014, 7,1)
quotes = mpf.quotes_historical_yahoo('^GDAXI',start,end)
# print quotes[:2]
quotes = np.array(quotes)
fig, (ax1, ax2) = plt.subplots(2, sharex=True, figsize=(8,6))
mpf.candlestick(ax1, quotes, width=0.6,colorup='b',colordown='r')
ax1.set_title('Yahoo Inc.')
ax1.set_ylabel('index level')
ax1.grid(True)
ax1.xaxis_date()
plt.bar(quotes[:,0] - 0.25, quotes[:, 5], width=0.5)
ax2.set_ylabel('volume')
ax2.grid(True)
ax2.autoscale_view()
plt.setp(plt.gca().get_xticklabels(),rotation=30)
plt.show()
3.3D 绘图
#!/etc/bin/python
#coding=utf-8
import numpy as np
import matplotlib.pyplot as plt
stike = np.linspace(50, 150, 24)
ttm = np.linspace(0.5, 2.5, 24)
stike, ttm = np.meshgrid(stike, ttm)
print stike[:2]
iv = (stike - 100) ** 2 / (100 * stike) /ttm
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=(9,6))
ax = fig.gca(projection='3d')
surf = ax.plot_surface(stike, ttm, iv, rstride=2, cstride=2, cmap=plt.cm.coolwarm, linewidth=0.5, antialiased=True)
ax.set_xlabel('strike')
ax.set_ylabel('time-to-maturity')
ax.set_zlabel('implied volatility')
plt.show()
到此这篇关于Python绘图之二维图与三维图详解的文章就介绍到这了,更多相关Python绘图内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!
python二维图颜色函数_Python绘图之二维图与三维图详解相关推荐
- python二维图颜色函数_Python scipy的二维图像卷积运算与图像模糊处理操作示例
本文实例讲述了Python scipy的二维图像卷积运算与图像模糊处理操作.分享给大家供大家参考,具体如下: 二维图像卷积运算 一 代码 import numpy as np from scipy i ...
- time库是python中处理时间的标准库_python语言time库和datetime库基本使用详解
今天是边复习边创作博客的第三天,我今年大二,我们专业开的有这门课程,因为喜欢所以更加认真学习,本以为没人看呢,看了后台浏览量让我更加认真创作,这篇博客花了2个半小时的时间,结合自己所学,所思,所想写作 ...
- python调用包中的方法_python 中不同包 类 方法 之间的调用详解
目录结构如下: 在hello.py中导入ORM.py这个文件的时候,采用 import ORMPackage.ORM 或者 import ORM u = User(id = 123, name='co ...
- python cv2 轮廓的包络 面积_Python 基于FIR实现Hilbert滤波器求信号包络详解
在通信领域,可以通过希尔伯特变换求解解析信号,进而求解窄带信号的包络. 实现希尔伯特变换有两种方法,一种是对信号做FFT,单后只保留单边频谱,在做IFFT,我们称之为频域方法:另一种是基于FIR根据传 ...
- python正则表达式提取数字比较好_python正则表达式从字符串中提取数字的思路详解...
python从字符串中提取数字 使用正则表达式,用法如下: ## 总结 ## ^ 匹配字符串的开始. ## $ 匹配字符串的结尾. ## \b 匹配一个单词的边界. ## \d 匹配任意数字. ## ...
- python列表和元组的应用_python学习笔记之列表(list)与元组(tuple)详解
前言 最近重新再看python的基础知识,感觉自己还是对于这些知识很陌生,需要用的时候还是需要翻书查阅,还是先注重基础吧--我要重新把python的教程阅读一遍,把以前自己忽略的部分学习,加强练习和记 ...
- python中randint是什么意思_python中random.randint和random.randrange的区别详解
在python中,通过导入random库,就能使用randint 和 randrange 这两个方法来产生随机整数.那这两个方法的区别在于什么地方呢?让我们一起来看看! 区别: randint 产生的 ...
- python线程池模块第三方包_python线程池(threadpool)模块使用笔记详解
最近在做一个视频设备管理的项目,设备包括(摄像机,DVR,NVR等),包括设备信息补全,设备状态推送,设备流地址推送等,如果同时导入的设备数量较多,如果使用单线程进行设备检测,那么由于设备数量较多,会 ...
- python列表的排序方法是_Python列表排序 reverse、sort、sorted 操作方法详解
python语言中的列表排序方法有三个:reverse反转/倒序排序.sort正序排序.sorted可以获取排序后的列表.在更高级列表排序中,后两中方法还可以加入条件参数进行排序. reverse() ...
最新文章
- php数据库重复记录数,php – 每隔30分钟计算一次数据库上的重复...
- javaweb成长之路:struts2的探索(一)
- 程序员面试100题之三:不用+、-、×、÷数字运算符做加法
- php环境搭建(php5.5.8+apache2.4)
- C#处理Json文件
- 【算法学习笔记】81.动态规划 分类讨论 SJTU OJ 1075 括号匹配升级
- plc简易电子计算机设计,PLC-电子计算器设计.doc
- c语言流程图生成器免费软件,c语言流程图生成器
- springboot 整合 shiro (Web Applications)避坑一 ,请看shiro官网
- 《未来简史》读书笔记
- 苹果11蓝牙配对不成功怎么办_【苹果手机蓝牙不能配对】苹果手机蓝牙无法配对_苹果手机蓝牙怎么配对...
- ESP32 开发笔记(三)源码示例 12_IR_Rev_RMT 使用RMT实现红外遥控接收解码(NEC编码)
- UE4 编辑器模块引用,不影响打包处理
- 2019杭州电子科技大学计算机考研经验谈
- 链表中添加一个节点和删除一个节点
- SharedPreferences in credential encrypted storage are not available until after user is unlocked
- 移动互联时代的地方网站转型
- 腾讯游戏10大经典案例(二):天堂等于天下
- unity3d-unet小demo
- mongo慢查询排查
热门文章
- Java数据库篇5——事务
- 个人用户不得开设服务器对外提供信息服务,北京大学网络安全知识温馨提示
- div靠边隐藏_css如何隐藏div不占位置?
- C语言中输入123求位权,数反转 - it610.com
- mysql资质_MySQL语句与Java代码实现按需过滤企业员工的资质证书
- python flask高级编程之restful_python Flask实现restful api service
- SQL关联查询————LEFT JOIN关键字的使用
- 如何检测服务器运行正常运行,一种检测服务器运行状态的方法、装置和系统专利_专利查询 - 天眼查...
- 三轴加速度传感器和六轴惯性传感器_一文读懂汽车MEMS惯性传感器的七大应用(上)...
- 算法训练 和为T 深度搜索