1

I have 2 arrays:


 dates = [datetime.date(2019, 12, 9), datetime.date(2019, 12, 10),
       datetime.date(2019, 12, 12), datetime.date(2019, 12, 13),
       datetime.date(2019, 12, 14), datetime.date(2019, 12, 15),
       datetime.date(2019, 12, 16), datetime.date(2019, 12, 17),
       datetime.date(2019, 12, 18), datetime.date(2019, 12, 19)]

counts = [10, 2, 48067, 49791, 35347, 39418, 38817, 34269, 28066,
       22212]

I am trying to create a graph that will show each of the dates, but only getting a few

plt.figure(figsize=(100,10))

fig.tight_layout()
plt.xticks(rotation=90)

plt.plot(dates, counts)


plt.show()

I have tried following this but I still had the same results - not getting all the dates on the x axis

1 Answers1

1

After you plot your graph, you can create a DateLocator and DateFormatter to change the format and frequency of the x ticks. The full code with comments is below.

import numpy as np
import matplotlib.pyplot as plt
import datetime
import matplotlib.dates as mdates

dates = [datetime.date(2019, 12, 9), datetime.date(2019, 12, 10),
         datetime.date(2019, 12, 12), datetime.date(2019, 12, 13),
         datetime.date(2019, 12, 14), datetime.date(2019, 12, 15),
         datetime.date(2019, 12, 16), datetime.date(2019, 12, 17),
         datetime.date(2019, 12, 18), datetime.date(2019, 12, 19)]

counts = [10, 2, 48067, 49791, 35347, 39418, 38817, 34269, 28066, 22212]


plt.figure(figsize=(100,10))
fig.tight_layout()
plt.xticks(rotation=90)
plt.plot(dates, counts)

ax=plt.gca()
# Find the days in the data
days = mdates.DayLocator()
# Format the days in the data
days_fmt = mdates.DateFormatter('%D')
# Set xaxis to use the day locator
ax.xaxis.set_major_locator(days)
# Set xaxis to use the day formatter
ax.xaxis.set_major_formatter(days_fmt)

plt.show()

This produces the following graph where all days are displayed with separate ticks.

enter image description here

For a detailed example with more date formats, check out this sample.

ilke444
  • 2,641
  • 1
  • 17
  • 31