Python金融数据可视化汇总
作者:laozhang 时间:2023-04-12 21:27:41
通过本篇内容给大家介绍一下Python实现金融数据可视化中两列数据的提取、分别画、双坐标轴、双图、两种不同的图等代码写法和思路总结。
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2000)
y = np.random.standard_normal((20,2))
# print(y)
'''
不同的求和
print(y.cumsum())
print(y.sum(axis=0))
print(y.cumsum(axis=0))
'''
# 绘图
plt.figure(figsize=(7,4))
plt.plot(y.cumsum(axis=0),linewidth=2.5)
plt.plot(y.cumsum(axis=0),'bo')
plt.grid(True)
plt.axis("tight")
plt.xlabel('index')
plt.ylabel('values')
plt.title('a simple plot')
plt.show()
2.下面分别提取两组数据,进行绘图。
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2000)
date = np.random.standard_normal((20,2))
y = date.cumsum(axis=0)
print(y)
# 重点下面两种情况的区别
print(y[1]) # 取得是 第1行的数据 [-0.37003581 1.74900181]
print(y[:,0]) # 取得是 第1列的数据 [ 1.73673761 -0.37003581 0.21302575 0.35026529 ...
# 绘图
plt.plot(y[:,0],lw=2.5,label="1st",color='blue')
plt.plot(y[:,1],lw=2.5,label="2st",color='red')
plt.plot(y,'ro')
# 添加细节
plt.title("A Simple Plot",size=20,color='red')
plt.xlabel('Index',size=20)
plt.ylabel('Values',size=20)
# plt.axis('tight')
plt.xlim(-1,21)
plt.ylim(np.min(y)-1,np.max(y)+1)
# 添加图例
plt.legend(loc=0)
plt.show()
3.双坐标轴。
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2000)
date = np.random.standard_normal((20,2))
y = date.cumsum(axis=0)
y[:,0]=y[:,0]*100
fig,ax1 = plt.subplots()
plt.plot(y[:,0],'b',label="1st")
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel("Index")
plt.ylabel('Values of 1st')
plt.title("This is double axis label")
plt.legend(loc=0)
ax2=ax1.twinx()
plt.plot(y[:,1],'g',label="2st")
plt.plot(y[:,1],'r*')
plt.ylabel("Values of 2st")
plt.legend(loc=0)
plt.show()
4. 分为两个图绘画。
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2000)
date = np.random.standard_normal((20,2))
y = date.cumsum(axis=0)
y[:,0]=y[:,0]*100
plt.figure(figsize=(7,5)) # 确定图片大小
plt.subplot(211) # 确定第一个图的位置 (行,列,第几个)两行一列第一个图
plt.plot(y[:,0],'b',label="1st")
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel("Index")
plt.ylabel('Values of 1st')
plt.title("This is double axis label")
plt.legend(loc=0)
plt.subplot(212) # 确定第一个图的位置
plt.plot(y[:,1],'g',label="2st")
plt.plot(y[:,1],'r*')
plt.ylabel("Values of 2st")
plt.legend(loc=0)
plt.show()
5.在两个图层中绘制两种不同的图(直线图立方图)
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2000)
date = np.random.standard_normal((20,2))
y = date.cumsum(axis=0)
y[:,0]=y[:,0]*100
plt.figure(figsize=(7,5)) # 确定图片大小
plt.subplot(121) # 确定第一个图的位置
plt.plot(y[:,0],'b',label="1st")
plt.plot(y[:,0],'ro')
plt.grid(True)
plt.axis('tight')
plt.xlabel("Index")
plt.ylabel('Values',size=20)
plt.title("1st date set")
plt.legend(loc=0)
plt.subplot(122) # 确定第一个图的位置
plt.bar(np.arange(len(y[:,1])),y[:,1],width = 0.5,color='g',label="2nd") # 直方图的画法
plt.grid(True)
plt.xlabel("Index")
plt.title('2nd date set')
plt.legend(loc=0)
plt.show()
来源:https://www.cnblogs.com/hanbb/p/7846452.html
标签:Python,金融,数据,可视化
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django怎么在admin后台注册数据库表
2024-01-26 03:12:50
![](https://img.aspxhome.com/file/2023/3/134493_0s.png)
ASP.NET MVC4入门教程(六):验证编辑方法和编辑视图
2024-05-13 09:15:36
![](https://img.aspxhome.com/file/2023/9/126199_0s.png)
caffe的python接口生成配置文件学习
2023-07-09 04:46:41
讲解SQL Server危险扩展存储删除和恢复
2008-12-09 14:30:00
python查看FTP是否能连接成功的方法
2023-11-04 13:31:50
用Python Turtle画棵樱花树送给自己
2022-06-30 10:16:47
![](https://img.aspxhome.com/file/2023/8/88968_0s.jpg)
navicat 8 for mysql建库的方法
2024-01-26 04:07:53
![](https://img.aspxhome.com/file/2023/8/112878_0s.png)
mysql数据库mysql: [ERROR] unknown option '--skip-grant-tables'
2024-01-18 08:41:27
一行代码生成Tableau可视化图表的方法
2022-09-21 12:01:47
![](https://img.aspxhome.com/file/2023/6/117946_0s.png)
比较规范的验证Email地址是否正确的正则表达式
2009-07-28 17:55:00
sql 查询记录数结果集某个区间内记录
2023-07-09 08:25:01
MySQL数据库在Linux下二进制日志恢复方法
2009-07-30 08:55:00
Vue组件全局注册实现警告框的实例详解
2024-05-02 16:53:05
![](https://img.aspxhome.com/file/2023/5/130155_0s.jpg)
php通过获取头信息判断图片类型的方法
2023-11-10 00:31:50
SQLServer 跨库查询实现方法
2024-01-29 02:02:25
python之Socket网络编程详解
2021-05-29 14:43:22
![](https://img.aspxhome.com/file/2023/5/66365_0s.jpg)
JavaScript版的DateAdd、DateDiff、IsDate函数
2008-01-30 15:35:00
基于PyQt4和PySide实现输入对话框效果
2023-06-11 13:30:58
![](https://img.aspxhome.com/file/2023/5/60955_0s.png)
Python导入父文件夹中模块并读取当前文件夹内的资源
2023-08-27 09:03:43
![](https://img.aspxhome.com/file/2023/4/94164_0s.png)
Python Matplotlib通过plt.subplots创建子绘图
2022-06-03 08:23:16
![](https://img.aspxhome.com/file/2023/5/98905_0s.png)