-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)

6
  • Please provide some randomly generated sample data. Specifically, what's your time range and your spacing? If you're trying to plot years, but the labels are hard-coded to display in seconds, you have a problem (which can be fixed) Commented Aug 24, 2022 at 18:00
  • its per minute data for 24 hours of a day. Commented Aug 24, 2022 at 19:23
  • Your label is per-second. That's ~86400 labels. Not a good look Commented Aug 24, 2022 at 20:02
  • What's mdates? Could you please post a minimal reproducible example like I asked? Commented Aug 24, 2022 at 20:04
  • 1
    Also, if you only have one day of data, why are you using DayLocator? Commented Aug 24, 2022 at 20:05

2 Answers 2

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

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

Comments

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

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.