Python-matplotlib画图

导入数据:

import pandas as pd
import numpy as np
'''导入数据'''
all_thing=pd.read_csv('EURUSD 60 train.csv',header=None)
#收盘价
train_data_close=pd.read_csv('EURUSD 60 train.csv',usecols=[5],header=None)
#开盘价
train_data_open=pd.read_csv('EURUSD 60 train.csv',usecols=[2],header=None)
#最低价
train_data_low=pd.read_csv('EURUSD 60 train.csv',usecols=[3],header=None)
#最高价
train_data_high=pd.read_csv('EURUSD 60 train.csv',usecols=[4],header=None)
#时间
train_data_time=pd.read_csv('EURUSD 60 train.csv',usecols=[0],header=None)
'''确认涨跌'''

蜡烛图:

from matplotlib import pyplot as plt
import mpl_finance as mpf
from matplotlib.pylab import date2num
import pandas as pd
import datetime

%matplotlib inline

quotes = []

timess=np.array(train_data_time).reshape(-1,)
list1=[]
for i in range(3000):
    format_data=timess[i][0:4]+'.'+timess[i][5:7]+'.'+timess[i][8:11]    
    sdate_num = date2num(datetime.datetime.strptime(format_data,'%Y.%m.%d'))#转换时间格式 
    sdate_plt = sdate_num
    sopen = np.array(train_data_open).reshape(-1,)[i]
    shigh = np.array(train_data_high).reshape(-1,)[i]
    slow = np.array(train_data_low).reshape(-1,)[i]
    sclose = np.array(train_data_close).reshape(-1,)[i]
    datas = (sdate_plt,sopen,shigh,slow,sclose) # 按照 candlestick_ohlc 要求的数据结构准备数据
    quotes.append(datas)

fig, ax = plt.subplots(facecolor=(0, 0.3, 0.5),figsize=(12,8))
fig.subplots_adjust(bottom=0.1)
ax.xaxis_date()
plt.xticks(rotation=45) #日期显示的旋转角度
plt.title('data')
plt.xlabel('time')
plt.ylabel('price')
mpf.candlestick_ohlc(ax,quotes,width=0.7,colorup='r',colordown='green') # 上涨为红色K线,下跌为绿色,K线宽度为0.7
plt.grid(True)

在这里插入图片描述
折线图:

import matplotlib.pyplot as plt
y=[y for y in range(0,100,1)]
plt.plot(y,train_data_open[:100],label='open')
plt.plot(y,train_data_close[:100],label='close')
plt.plot(y,train_data_low[:100],label='low')
plt.plot(y,train_data_high[:100],label='high')
plt.legend()
plt.show()

在这里插入图片描述
散点图:

import matplotlib.pyplot as plt
y=[i for i in range(0,100)]
plt.scatter(y,train_data_open[:100],color='red',label='open')
plt.scatter(y,train_data_high[:100],color='yellow',label='high')
plt.scatter(y,train_data_low[:100],color='blue',label='low')
plt.legend()
plt.show()

在这里插入图片描述
3D散点图:

from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
import matplotlib.cm as cmx
import numpy as np
import random
import sys
N = 1000
colors = np.random.rand(N)#颜色随机

tx = train_data_open[:1000]
ty = train_data_high[:1000]
tz = train_data_low[:1000]

fig = plt.figure()
ax = fig.gca(projection='3d')


ax.set_title("Trajectory")
ax.set_xlabel("open")
ax.set_ylabel("high")
ax.set_zlabel("low")


figure = ax.scatter(tx, ty, tz, marker='.',c=colors)

plt.show()

在这里插入图片描述

发布了43 篇原创文章 · 获赞 192 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览