Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
I want to plot a time series graph from a CSV file. I have succeeded to read the file and transformed the data from string to date using strptime and stored it in a list. But when I tried plotting a test plot in matplotlib with the list including the date information it plotted the date as a series of dots; that is, for a date 2012-may-31 19:00 hours, I got a graph with a dot at 2012, 05, 19, 31, 00 on the y-axis for the value of x=1 and so on.  How to plot the correct graph?

1 Answer

0 votes
by (108k points)

First, convert your x-axis data from text to datetime.datetime, with the help of datetime.strptime function:

>>> from datetime import datetime

>>> datetime.strptime("2012-may-31 19:00", "%Y-%b-%d %H:%M")

 datetime.datetime(2012, 5, 31, 19, 0)

This is an example:

import matplotlib.pyplot as plt

import datetime

import numpy as np

x = np.array([datetime.datetime(2013, 9, 28, i, 0) for i in range(24)])

y = np.random.randint(100, size=x.shape)

plt.plot(x,y)

plt.show()

Sign up for this Intellipaat's Python Course fast, to Learn Python concepts in detail and get certified.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Aug 11, 2020 in Data Science by blackindya (18.4k points)

Browse Categories

...