-2

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)

ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257

2 Answers2

0

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

0

Thank you everyone for the help. I have managed to find a code that works.


def plot_graph(time,energy,date,img_dir,esc):
    title = "Energy plots for " + esc+ " " + date
    label = 'kWh'

    x = pd.to_datetime(time)

    y = energy



    # plt.close()


    xe = plt.figure(figsize=(24,18))
    plt.title(title)
    plt.ylabel(label)
    plt.plot(x, y, 'kp-')
    
    plt.ylim(0,500)
    #plt.xlim(datetime("00:00:00"),datetime("23:59:59"))
   

    ax=plt.gca()
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
    ax.xaxis.set_major_locator(mdates.HourLocator(interval=1))
    plt.gcf().autofmt_xdate()

    plt.savefig(img_dir + date + ' '+ esc +'.png' )
    
    return xe