Issue
Basically I have a datetime list covering only one day so I want to plot against time of day which I have done but I don't know how to label the x-axis with time rather than date as shown in the figure.
# time = a list of times in format eg 01:23:45
x_values = [datetime.datetime.strptime(d,"%H:%M:%S").time() for d in time]
x_values = [datetime.datetime.strptime(d,"%H:%M:%S").time() for d in time]
my_day = datetime.date(2019, 3, 2)
x_dt = [datetime.datetime.combine(my_day, t) for t in x_values]
tick_spacing = 0.08
fig, ax = plt.subplots(1,1)
ax.plot(x_dt,magnitude_list)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
plt.title('88992 Light Curve')
plt.ylabel('Magnitude')
plt.xlabel('Time')
plt.figure(figsize=(30,10))
plt.show()
Solution
just like you set the tick spacing, you can set the (date)time format, after importing dates
from matplotlib.
EX:
from datetime import datetime, timedelta
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
from matplotlib import ticker
# generate some dummy data...
n = 12
x = [datetime.now()+timedelta(hours=i) for i in range(n)]
y = np.random.rand(n)
tick_spacing = 0.08
time_fmt = '%H:%M:%S' # how you want to display time/date
fig, ax = plt.subplots(1,1)
ax.plot(x, y)
ax.xaxis.set_major_locator(ticker.MultipleLocator(tick_spacing))
ax.xaxis.set_major_formatter(mdates.DateFormatter(time_fmt))
plt.title('88992 Light Curve')
plt.ylabel('Magnitude')
plt.xlabel('Time')
plt.figure(figsize=(30,10))
plt.show()
Answered By - MrFuppes
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.