Answer the question
In order to leave comments, you need to log in
How to display time on axis of matplotlib plot?
Hello!
There is the following task: given a list with a string value of time (time_interval). An array of objective function (y) is given. I would like to build a graph where the values from time_interval are plotted along the x-axis, and the values of the objective function are plotted along the y-axis. Please tell me how to build such a graph.
import numpy as np
import matplotlib.pyplot as plt
time_interval = ['19:0:0', '19:1:0', '19:2:0', '19:3:0', '19:4:0']
y = np.random.randn(5)
x = np.array([x for x in range(5)])
plt.plot(time_interval, y)
plt.show()
Answer the question
In order to leave comments, you need to log in
You need to convert your time to datetime type. And then build a graph.
For example:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates
import datetime as dt
fmt = dates.DateFormatter('%H:%M:%S')
fig, ax = plt.subplots()
time_interval = ['19:0:0', '19:1:0', '19:2:0', '19:3:0', '19:4:0']
time_interval = [dt.datetime.strptime(i, "%H:%M:%S") for i in time_interval]
y = np.random.randn(5)
x = np.array([x for x in range(5)])
ax.plot(time_interval, y, "-o")
ax.xaxis.set_major_formatter(fmt)
fig.autofmt_xdate()
plt.show()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question