1

Here an example code in which I would like to draw the entire date on the x axis

import datetime
import matplotlib.pyplot as plt
import random
import numpy as np
from matplotlib.dates import DateFormatter

years = np.arange( 2005, 2016, 1)
years[1] = 2005
years[2] = 2005
months = [ 3, 2, 1, 5, 7, 12, 2, 3, 5, 2, 6]
dates = []
for Y in years:
    dates = dates + [ datetime.datetime(Y, 6 , 4) ]


y = np.random.randn(len(dates))

fig, ax = plt.subplots()

ax.plot_date(dates, y, 'r+', ms = 2, mew = 12)

ax.fmt_xdata = DateFormatter('%y-%m-%d')
fig.autofmt_xdate()

plt.show()

Unfortunately the result is this enter image description here

And what I need is to write all the date on the x axis like in this picture

enter image description here

What you suggest me to change on my code?

1 Answer 1

1

ax.fmt_xdata is the formatter for the dates usually only used for the hover functionality in interactive plots. enter image description here

What you want here is to set the major formatter of the xaxis to a DateFormatter:

ax.xaxis.set_major_formatter(DateFormatter('%b-%d-%Y'))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.