python用matplotlib画K线
importmatplotlib.pyplotaspltimportmatplotlib.financeasmpfwithopen('SH#600004.txt')aso...
import matplotlib.pyplot as plt
import matplotlib.finance as mpf
with open('SH#600004.txt') as obj:
text=obj.readlines()
# vol 成交量 amo成交额
baseinfo=text[0]
dayinfo=text[2:-1]
date_list =[dayinfo[i].split(',')[0] for i in range(len(dayinfo))]
open_list =[dayinfo[i].split(',')[1] for i in range(len(dayinfo))]
high_list =[dayinfo[i].split(',')[2] for i in range(len(dayinfo))]
low_list =[dayinfo[i].split(',')[3] for i in range(len(dayinfo))]
close_list=[dayinfo[i].split(',')[4] for i in range(len(dayinfo))]
quotes=zip(date_list,open_list,high_list,low_list,close_list)
#N=100
#open_list[:N],high_list[:N],low_list[:N],close_list[:N]
fig,ax=plt.subplots()
mpf.candlestick_ohlc(ax,quotes,width=0.6,colorup='r',colordown='green')
plt.title(baseinfo)
plt.xlabel(date_list)
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.show()
#报错信息
#TypeError: unsupported operand type(s) for -: 'str' and 'str'
请问应该怎么改 展开
import matplotlib.finance as mpf
with open('SH#600004.txt') as obj:
text=obj.readlines()
# vol 成交量 amo成交额
baseinfo=text[0]
dayinfo=text[2:-1]
date_list =[dayinfo[i].split(',')[0] for i in range(len(dayinfo))]
open_list =[dayinfo[i].split(',')[1] for i in range(len(dayinfo))]
high_list =[dayinfo[i].split(',')[2] for i in range(len(dayinfo))]
low_list =[dayinfo[i].split(',')[3] for i in range(len(dayinfo))]
close_list=[dayinfo[i].split(',')[4] for i in range(len(dayinfo))]
quotes=zip(date_list,open_list,high_list,low_list,close_list)
#N=100
#open_list[:N],high_list[:N],low_list[:N],close_list[:N]
fig,ax=plt.subplots()
mpf.candlestick_ohlc(ax,quotes,width=0.6,colorup='r',colordown='green')
plt.title(baseinfo)
plt.xlabel(date_list)
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
plt.show()
#报错信息
#TypeError: unsupported operand type(s) for -: 'str' and 'str'
请问应该怎么改 展开
1个回答
2017-12-04 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc
#从雅虎财经获取历史行情
date1 = (2017, 1, 1)
date2 = (2017, 4, 30)
quotes = quotes_historical_yahoo_ohlc('600000.ss', date1, date2)
if len(quotes) == 0:
raise SystemExit
#创建一个子图
fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
#设置主要刻度和显示格式
mondays = WeekdayLocator(MONDAY)
mondaysFormatter = DateFormatter('%Y-%m-%d')
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_major_formatter(mondaysFormatter)
#设置次要刻度和显示格式
alldays = DayLocator()
alldaysFormatter = DateFormatter('%d')
ax.xaxis.set_minor_locator(alldays)
#ax.xaxis.set_minor_formatter(alldaysFormatter)
#设置x轴为日期
ax.xaxis_date()
ax.autoscale_view()
#X轴刻度文字倾斜45度
plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')
candlestick_ohlc(ax, quotes, width=0.6, colorup='r', colordown='g')
ax.grid(True)
plt.title('600000')
plt.show()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询