N
N
NV272016-10-09 23:33:20
Python
NV27, 2016-10-09 23:33:20

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

Thank you for your attention :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-10-10
@Avernial

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 question

Ask a Question

731 491 924 answers to any question