Matplotlib 3.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

The following code plots the time series chart of the Google Stock Price:

  1. Load the Google Stock Price file (date and price) into x, y coordinates:
stock = pd.read_csv('GOOG.csv', header=None, delimiter=',')
  1.  Add column names:
stock.columns = ['date','price']
  1. Convert the pandas DataFrame into a time series:
stock['date'] = pd.to_datetime(stock['date'], format='%d-%m-%Y') 
  1. Set the date as the index for the pandas DataFrame:
indexed_stock = stock.set_index('date') 
ts = indexed_stock['price']
  1. Plot the graph:
plt.plot(ts)
  1. Display the graph on the screen:
plt.show()