I have 3 csv's with stock data, all from 2012-01-1 to 2017-01-01. I have used Pandas to make Dataframes out of them, and am instructed to turn them into this example plot: [Desired outcome][1]
My only issue is that Pandas isn't taking the 'Date' Series from my Dataframe to create the X axis of time like in the picture, instead I get an X axis of values 0-1400 [My failed plot][2] (I have 1258 dates, so it's getting the index of the date rather than the date itself).
This is my code for the plot right now:
ford['Open'].plot()
tesla['Open'].plot()
GM['Open'].plot(x=ford['Date'],figsize=(16,8),title='Open price',xlabel='Date')
plt.legend(['Ford','Tesla','GM'])
I have tried using the xticks argument and passing it tesla['Date'] (The name of the date series), but when I do I get this error:
ConversionError: Failed to convert value(s) to axis units: 0 2012-01-03 1 2012-01-04 2 2012-01-05 3 2012-01-06 4 2012-01-09 ...
1253 2016-12-23 1254 2016-12-27 1255 2016-12-28 1256 2016-12-29 1257 2016-12-30 Name: Date, Length: 1258, dtype: object
I couldn't find this particular issue on google and I've looked for an hour. I'm definitely missing something but I don't know what. Thanks for any help [1]:
CodePudding user response:
Select the index of the dates you want to display (say, one tick every 200 value), and use plt.xticks. Try this:
import numpy as np
ford['Open'].plot()
tesla['Open'].plot()
GM['Open'].plot()
plt.legend(['Ford','Tesla','GM'])
ticks = np.arange(0, len(ford['Date']), 200)
plt.xticks(ticks, ford['Date'][ticks])