1

I'm trying to plot price data with dates ax the xticks. I've taken a look at this answer - "Changing the Tick Prequency" and this answer - "showing specific xtick on Matplotlib" but my problem seems to persist. Allow me to elaborate:

The data that I have looks something like this:

date             Close
2017-05-10      0.12512
2017-05-11      0.12353
2017-05-12     -0.35235
.
.
.
2019-01-10      0.87890

It's a total of 608 entries, and the closing prices have all been scaled to be within (-1, 1).

The code that I wrote to make the plot is:

plt.plot(train['Close'], label='Training Data')
plt.Figure()
plt.plot(test['Close'], label='Test Data')
plt.grid(True)
plt.title("Closing Prices")
plt.ylabel('Price Scaled')
plt.xlabel('Time')
plt.legend()
plt.show()

which produces this image:

enter image description here

As you can see, the xtick intervals are 100 in size and the graph starts at 0 and ends at 608. My question is: how do I format the graph so that the starting date (2017-05-10) is at 0, and the xtick prints only for each 100th tick? Also, I'm trying to get the 608th tick to show its date as well.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Sean
  • 2,890
  • 8
  • 36
  • 78
  • You can try: ax.xaxis.set_major_formatter(mdates.DateFormatter('%y-%m-%d')) https://matplotlib.org/api/dates_api.html – Jetman Apr 08 '19 at 06:51
  • `plt.plot(x,y)` takes the two arguments of x and y. If you only specify `y`, the x axis will only show the numbers from 0 to number of data points, or if `y` is a pandas series, it will take that pandas series index. So either specify `x` as well, or make the dates the pandas index. – ImportanceOfBeingErnest Apr 08 '19 at 10:55

0 Answers0