I would like to generate sequence of ordered numbers which is of same length of some other list. I have tried
def parse_data_from_file(filename):
times = []
temperatures = []
with open(filename) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader)
for row in reader:
times.append(row[0])
temperatures.append(float(row[1]))
return times, temperatures
The issue is time is not getting represnted properly on x-axis as attached here:
You haven't said how you're making the plots. I'll assume you're using matplotlib , which is probably the most popular plotting package for Python.
I know that when I'm working with dates or times, I usually use datetime.strptime()
to get the data into a datetime format that Python likes:
from datetime import datetime
# assume your date info looks like 08-Jun-2022 14:22:22
dt = datetime.strptime(row[0], '%d-%b-%Y %H:%M:%S')
times.append(dt)
Matplotlib has a convenience function date2num
that converts a datetime object to a format that it likes to use. Their official example is here . Here is an even shorter example for you:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib
fig, ax = plt.subplots()
t = matplotlib.dates.date2num(times)
y = temperatures
ax.plot(t, y)
ax.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1, 7)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.