简体   繁体   中英

Editing X-axis in matplotlib.pyplot for python

I cannot seem to get the X-labels for the X-axis spaced out properly. A picture is given below. I need to understand how to set x-axis label distance and the data for x-axis is time series in hours and minutes.




title = "Energy plots for " + escalators[0].split(".")[0]
label = 'kWh'






xe = plt.figure(figsize=(30, 15))
plt.title(title)
plt.ylabel(label)
plt.plot(date_time, y, 'kp-', markersize=3, linewidth=0.5)


ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter('hh:mm:ss'))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
#ax.xaxis.set_major_locator(mdates.DateLocator(interval=2))
plt.gcf().autofmt_xdate()
plt.xticks(date_time)
plt.rc('xtick', labelsize=12)
plt.subplots_adjust(bottom = 0.1)

You could try setting the minimum, maximum, and incrementt, using np.arange

plt.xticks(np.arange(datetime(1985,7,1), datetime(2015,7,1), timedelta(days=1)).astype(datetime))

This would give you the x-axis in whatever range and interval you want.

Don't forget that you need to import from datetime import datetime, timedelta

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM