How to plot a data including date and time?
Elliott Roper
nospam at yrl.co.uk
Wed Aug 14 09:49:53 EDT 2019
On 14 Aug 2019, amirrezaheidarysbu at gmail.com wrote
(in article<23d45668-fa47-4640-832a-5a5c64600b95 at googlegroups.com>):
> On Tuesday, August 13, 2019 at 11:47:28 PM UTC+2, amirrezah... at gmail.com
> wrote:
> > I have a .csv file, in first column I have date and hour, and in the second
> > column I have energy use data. How can I make a bar chart with Date and
> > time as the x axis and the energy use as the Y axis?
> >
> > Thanks
>
> Thank you for your guidance. I am already using matplotlib but I do not know
> how to import a column of date and time and to use it properly as the x axis.
> can you tell me the code?
>
> Thanks
If you don't mind using a steam hammer to crack a nut, it is amazing what you
can do with pandas using just the "10 minute guide" chapter in the (shudder)
10,000 page manual. The chief benefit is how thoroughly it protects you from
Numpy and Matplotlib.
I solved a similar problem (tracking home blood pressure with exponentially
weighted means) up and running in half a day from a completely cold start.
The graphing bit is a delight. However, if you want to do something that the
10 minute guide does not cover, you can lose a man-month without really
trying. Pandas is a beautiful monster!
Here's the relevant bit
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import pytz
from pytz import common_timezones, all_timezones
#preparing the csv from a text file elided as irrelevant
#except that the .csv headings have to be Date Systolic Diastolic for this to
work as designed
# Plot the intermediate file with pandas after adding exponentially weighted
means
df = pd.read_csv('Blood pressure.csv')
df['Date'] = pd.to_datetime(df['Date'])
df['Syst EWM'] = df['Systolic'].ewm(span=200).mean()
df['Diast EWM'] = df['Diastolic'].ewm(span=200).mean()
plt.ioff()
df.plot(x='Date')
print(df.tail(60)) #a debug line I left in to watch the EWMs sink to more
healthy levels
plt.ylabel('mm Hg')
plt.suptitle("Home BP record")
plt.show()
That should give you a start
--
To de-mung my e-mail address:- fsnospam$elliott$$ PGP Fingerprint: 1A96 3CF7
637F 896B C810 E199 7E5C A9E4 8E59 E248
More information about the Python-list
mailing list