0

I'm trying to get my label on the x axis between the ticks. Previous answers to this question, linked below, are not helping me.

My approach so far has been to force it manually using this code:

x = np.arange(1,365,step=30)
my_xticks =['Jan','Feb','March','April','May','June','July','Aug','Sep','Oct','Nov','Dec']
plt.xticks(x, my_xticks)

which nearly gives me the results:

enter image description here

but the problem is the months are at the tick, and I need them to be between ticks.

Using my current method, how can I get the months to be in between each tick?

Previous answers to this question, here and here make use of matplotlib.axes.Axes.set_xticks. The trick in these answers seems to be to declare fig, ax = subplots(), and then access the axes where it is possible to adjust the alignment. My problem is that I have created the chart using only fig = plt.figure() and I don't know how to access the axes. When I tried to add ax = plt.subplots() it seemed to reset the whole project and the chart became unreadable.

If there is any guidance on both these problems I'd be grateful.

ZakS
  • 1,073
  • 3
  • 15
  • 27
  • 1
    Given that there is an existing solution available, the only missing bit is to get an axes `ax` from a pyplot plot. You may either create one yourself, `ax = fig.add_subplot(111)` or `ax = plt.subplot(111)` *before* plotting anything; or you may get the current axes via `ax = plt.gca()`. In general you might want to consider using the object oriented approach with `fig, ax = subplots()` in all your plots, not having to work around in order to obtain axes handles. – ImportanceOfBeingErnest May 07 '18 at 10:25
  • @ImportanceOfBeingErnest, I've edited my question to reflect what is different about it to the previous ones, namely how to get an axes. I think future searchers might benefit from my question, if they are confused by how to get the axes. I've done what you suggested and it worked, but I do want to ask you -- why is fig, ax = subplots() an OO approach, but plt.gca() not? How does it give me an advantage to use it over plt.gca(). Thanks. – ZakS May 07 '18 at 11:18
  • [The matplotlib usage guide](http://matplotlib.org/tutorials/introductory/usage.html) might be interesting to read. `fig, ax = subplots()` in itself is not the object oriented approach. But using `fig` and `ax` afterwards is object oriented, because it only uses the objects directly. The pyplot (`plt`) functions always rely on there being a current figure or other current object to work on. The advantage of using the objects directly is that you have complete control of them. If you do `ax8.set_xticks` you know that you are setting the ticks for `ax8`. – ImportanceOfBeingErnest May 07 '18 at 12:45

0 Answers0