0

I'm trying to plot a lot a data points and the X axis is timestamps. My problem is that for some length Matplotlib automatically squeezes them together and you cannot read the x axis, as shown in the pic:

squeezed x axis

How can I prevent this from happening? I'm trying to save that plot automatically with savefig(). It is saved to a PNG.

gboffi
  • 22,939
  • 8
  • 54
  • 85
John
  • 871
  • 2
  • 9
  • 20
  • Your problem seems to belong to the ["X-Y" category](https://en.wikipedia.org/wiki/XY_problem#:~:text=The%20XY%20problem%20is%20a,them%20to%20resolve%20issue%20X). Does this answer your question? [Rotate tick labels in subplot (Pyplot, Matplotlib, gridspec)](https://stackoverflow.com/questions/31186019/rotate-tick-labels-in-subplot-pyplot-matplotlib-gridspec) – gboffi Aug 05 '20 at 11:00

2 Answers2

0

you can specify the X-Ticks with following:

    import matplotlib.pyplot as plt

    plt.plot(x_values, y_value)
    plt.xticks([0,5,10])

The Plot will have less ticks.

WIthout the x-ticks:

enter image description here

With x-ticks:

enter image description here

Sebastian Baum
  • 365
  • 1
  • 9
-1

I found the answer here on the matplotlib site:

https://matplotlib.org/3.1.1/gallery/recipes/common_date_problems.html

fig, ax = plt.subplots()
ax.plot(date, r.close)

# rotate and align the tick labels so they look better
fig.autofmt_xdate()

# use a more precise date string for the x axis locations in the
# toolbar
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.set_title('fig.autofmt_xdate fixes the labels')
John
  • 871
  • 2
  • 9
  • 20