使用Python导入CSV

时间:2015-09-12 14:17:34

标签: python csv matplotlib

我试图使用matplotlib绘制图表。但是,在我运行我的代码之后,存在attributeError:modules对象没有属性' pyplot'但csv可用。问题是什么?这是我的代码: {

   import numpy as np
   import urllib
   import matplotlib.dates as mdates

   def bytespdate2num(fmt, encoding='utf-8'):
              strconverter = mdates.strpdate2num(fmt)
              def bytesconverter(b):
                     s = b.decode(encoding)
                     return strconverter(s)
              return bytesconverter


       def graph_data(stock):
              stock_price_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'
       source_code = urllib.request.urlopen(stock_price_url).read().decode()
       stock_data = []
       split_source = source_code.split('\n')
                 for line in split_source:
                      split_line = line.split(',')
                      if len(split_line) == 6:
             if 'values' not in line and 'labels' not in line:
                      stock_data.append(line)

date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
                                                      delimiter=',',
                                                      unpack=True,
                                                      # %Y = full year. 2015
                                                      # %y = partial year 15
                                                      # %m = number month
                                                      # %d = number day
                                                      # %H = hours
                                                      # %M = minutes
                                                      # %S = seconds
                                                      # 12-06-2014
                                                      # %m-%d-%Y
                                                      converters={0: bytespdate2num('%Y%m%d')})

plt.plot_date(date, closep,'-', label='Price')

plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.show()


   graph_data('TSLA')
 }

1 个答案:

答案 0 :(得分:0)

import matplotlib.pyplot as plt

参考:http://matplotlib.org/users/pyplot_tutorial.html