1

I'm trying to set minutes time as x-axis of matplotlib plot.The time period is from 8:30 to 15:00 everyday.I only need minutes time,don't need date.

currently I only know how to set date as x-axis:

from datetime import datetime

import matplotlib.dates as mdates
import matplotlib.pyplot as plt

dates = ['01/02/1991', '01/03/1991', '01/04/1991']
xs = [datetime.strptime(d, '%m/%d/%Y').date() for d in dates]
ys = range(len(xs))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
# Plot
plt.plot(xs, ys)
plt.gcf().autofmt_xdate()  
plt.show()

Any friend can help?

3
  • Does this answer your question? Matplotlib: xticks every 15 minutes, starting on the hour Commented May 11, 2020 at 15:57
  • Not all of them,can you give me more detail,appreciate your relpy! Commented May 11, 2020 at 16:22
  • @William have you found my answer helpful? Commented May 11, 2020 at 22:55

2 Answers 2

3

Here's one way you can generate the desired plot:

from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt
import matplotlib.dates as md

#a generator that gives time between start and end times with delta intervals
def deltatime(start, end, delta):
    current = start
    while current < end:
        yield current
        current += delta
dates = ['01/02/1991', '01/03/1991', '01/04/1991']

# generate the list for each date between 8:30 to 15:00 at 30 minute intervals
datetimes=[]
for i in dates:
    startime=datetime.combine(datetime.strptime(i, "%m/%d/%Y"), datetime.strptime('8:30:00',"%H:%M:%S").time())
    endtime=datetime.combine(datetime.strptime(i, "%m/%d/%Y"), datetime.strptime('15:00:00',"%H:%M:%S").time())
    datetimes.append([j for j in deltatime(startime,endtime, timedelta(minutes=30))])

#flatten datetimes list
datetimes=[datetime for day in datetimes for datetime in day]
xs = datetimes
ys = range(len(xs))

# plot
fig, ax = plt.subplots(1, 1)
ax.plot(xs, ys,'ok')

# From the SO:https://stackoverflow.com/questions/42398264/matplotlib-xticks-every-15-minutes-starting-on-the-hour
## Set time format and the interval of ticks (every 240 minutes)
xformatter = md.DateFormatter('%H:%M')
xlocator = md.MinuteLocator(interval = 240)

## Set xtick labels to appear every 240 minutes
ax.xaxis.set_major_locator(xlocator)

## Format xtick labels as HH:MM
plt.gcf().axes[0].xaxis.set_major_formatter(xformatter)
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this simple one:

DesiredIntervalInSeconds = 270 #Set ticks every four and half minutes
maxSecond = X_values[-1]+60   
tickCount = int(maxSecond//DesiredIntervalInSeconds)
xticks = [k*DesiredIntervalInSeconds for k in range(tickCount+1)]
xticks_labels = [f"{x//60}:{'0'+str(x%60) if x%60<10 else x%60 }" for x in xticks]
ax.set_xticks(xticks)
ax.set_xticklabels(xticks_labels)

Result: enter image description here

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.