我只是开始学习matplotlib。我试图绘制雅虎char api情节股票。我正在尝试这个程序,但它不起作用......有我的程序
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def graph():
date, closep, highp, lowp, openp, valuep = np.loadtxt('/home/najeeb/Desktop/table.csv', delimiter=',', unpack=True,
converters={0: mdates.strpdate2num('%Y-%m-%d')})
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1, axisbg='w')
plt.plot_date(x=date, y=value, fmt='-')
plt.title('title')
plt.ylabel('value')
plt.xlabel('date')
plt.show()
graph()
请指导我如何解决这个问题,并有另一种方法来绘制股票图...谢谢
答案 0 :(得分:0)
CSV文件如下所示:
Date,Open,High,Low,Close,Volume,Adj Close
2014-10-17,97.50,99.00,96.81,97.67,68032200,97.67
2014-10-16,95.55,97.72,95.41,96.26,72110700,96.26
2014-10-15,97.97,99.15,95.18,97.54,100875400,97.54
您的代码np.loadtxt()
试图解析标题'日期'作为一个日期,由于该字符串不是有效的日期值,因此无法正常工作,因此我使用skiprows=1
跳过标题。
另一个问题是,CSV有7列,在你的元组中你只拆解了6个值
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import urllib2
url = 'http://ichart.finance.yahoo.com/table.csv?s=AAPL&d=9&e=14&f=2008&g=d&a=8&b=7&c=1984&ignore=.csv'
def graph():
date, open, high, low, close, volume, adj_close = np.loadtxt(urllib2.urlopen(url), skiprows=1, delimiter=',', unpack=True, converters={0: mdates.strpdate2num('%Y-%m-%d')})
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1, axisbg='w')
plt.plot_date(x=date, y=adj_close, fmt='-')
plt.title('Apple, 1984 to 2008')
plt.ylabel('Adjusted close')
plt.xlabel('Date')
plt.show()
graph()