1

I'm going to be plotting some temperature data over time and wrote a test program to see with Matplotlib can do for me. When I plot the the date the date time does not display as I would expect, the date is output but the time is some type of counter not the time of day as expected.

Sample data:

import pandas as pd

df = pd.DataFrame([
    ['08/16/2019 00:00:00',70 ],['08/17/2019 00:05:00',70.5 ],
    ['08/17/2019 00:10:00',70.5],['08/17/2019 00:15:00',71 ],
    ['08/17/2019 00:20:00',72 ],['08/17/2019 00:25:00',73 ],
    ['08/17/2019 00:30:00',74 ],['08/17/2019 00:35:00',74.5], 
    ['08/17/2019 00:40:00',75 ],['08/17/2019 00:45:00',74.5], 
    ['08/17/2019 00:50:00',73 ],['08/17/2019 00:55:00',75 ],
    ['08/17/2019 01:00:00',72.5],['08/17/2019 01:05:00',78 ],
    ['08/17/2019 01:10:00',78]], columns=['Date Time', 'Temperature'])

df
Out[1]: 
              Date Time  Temperature
0   08/16/2019 00:00:00         70.0
1   08/17/2019 00:05:00         70.5
2   08/17/2019 00:10:00         70.5
3   08/17/2019 00:15:00         71.0
4   08/17/2019 00:20:00         72.0
5   08/17/2019 00:25:00         73.0
6   08/17/2019 00:30:00         74.0
7   08/17/2019 00:35:00         74.5
8   08/17/2019 00:40:00         75.0
9   08/17/2019 00:45:00         74.5
10  08/17/2019 00:50:00         73.0
11  08/17/2019 00:55:00         75.0
12  08/17/2019 01:00:00         72.5
13  08/17/2019 01:05:00         78.0
14  08/17/2019 01:10:00         78.0
import csv
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime

path="/home/mikejs/PythonSandbox/temps.csv"
file = open(path,newline='')
reader = csv.reader(file)
header = next(reader)
dates = []
temps  = []

for row in reader:
    date = datetime.strptime(row[0],'%m/%d/%Y %H:%M:%S')
    dates.append(date)
    temps.append(float(row[1]))

plt.title("Temperatures Over Time")
plt.plot(dates,temps )
plt.ylabel('Temperatues')
plt.xlabel('Date/Time')
plt.xticks(rotation='45')
plt.tight_layout();
plt.savefig('temps.png')
plt.show()

enter image description here

realr
  • 3,652
  • 6
  • 23
  • 34
  • Please share some sample input so we can understand what can be going wrong. Matplotlib has an entire [`date`](https://matplotlib.org/3.1.1/api/dates_api.html) module. – realr Aug 21 '19 at 01:54
  • This is example of the data: Date Time,Temperature 08/16/2019 00:00:00,70 08/17/2019 00:05:00,70.5 08/17/2019 00:10:00,70.5 08/17/2019 00:15:00,71 08/17/2019 00:20:00,72 08/17/2019 00:25:00,73 08/17/2019 00:30:00,74 08/17/2019 00:35:00,74.5 08/17/2019 00:40:00,75 08/17/2019 00:45:00,74.5 08/17/2019 00:50:00,73 08/17/2019 00:55:00,75 08/17/2019 01:00:00,72.5 08/17/2019 01:05:00,78 08/17/2019 01:10:00,78 – user2461513 Aug 21 '19 at 02:13
  • @user2461513 Use `pandas` then use [Provide a copy of the data](https://stackoverflow.com/questions/52413246/how-do-i-provide-a-reproducible-copy-of-my-existing-dataframe). Edit your question and put the data there. – Trenton McKinney Aug 21 '19 at 02:29

1 Answers1

1

You can use matplotlib.dates module to transform your axis into dates and then it will interpret the spacing correctly.

Below is an example using pandas to reproduce your sample:

import pandas as pd
from matplotlib import dates as mdates

df = pd.DataFrame([
    ['08/16/2019 00:00:00',70 ],['08/17/2019 00:05:00',70.5 ],
    ['08/17/2019 00:10:00',70.5],['08/17/2019 00:15:00',71 ],
    ['08/17/2019 00:20:00',72 ],['08/17/2019 00:25:00',73 ],
    ['08/17/2019 00:30:00',74 ],['08/17/2019 00:35:00',74.5], 
    ['08/17/2019 00:40:00',75 ],['08/17/2019 00:45:00',74.5], 
    ['08/17/2019 00:50:00',73 ],['08/17/2019 00:55:00',75 ],
    ['08/17/2019 01:00:00',72.5],['08/17/2019 01:05:00',78 ],
    ['08/17/2019 01:10:00',78]], columns=['Date Time', 'Temperature'])


### make sure you set date as the index (and optionally drop the column)
df.index = pd.to_datetime(df['Date Time'])
df.drop(['Date Time'], axis=1, inplace=True)

Below I am plotting the sample data with the first observation and skipping it. As it is 24hrs ahead of the rest, you can notice the large spacing.

plt.figure(figsize=(16,6))
ax1 = plt.subplot(121)
ax1.set_title("Temperatures Over Time")
ax1.plot(mdates.date2num(df.index), df['Temperature'])
ax1.set_ylabel('Temperatues')
ax1.set_xlabel('Date/Time')
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m %H:%M"))
plt.xticks(df.index, rotation='45')

# fig, ax = plt.subplots()
ax = plt.subplot(122)
ax.plot(mdates.date2num(df[1:].index), df[1:]['Temperature'])
ax.set_title("Temperatures Over Time (skipping first)")
ax.set_xticks(df[1:].index)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m %H:%M"))
plt.xticks(rotation='45')
plt.tight_layout();
# plt.savefig('temps.png')
plt.show()

enter image description here

Also for editing the axis, it has a function to format it according to your needs. For instance in the following line you can adapt your format.

ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m %H:%M"))
realr
  • 3,652
  • 6
  • 23
  • 34
  • What exactly does the plt.subplot(121) do? – user2461513 Aug 21 '19 at 05:48
  • I am creating two subplots in one figure. the three digits (121) means, sequentially: total rows (1), total columns (2) and instance (1 for first and 2 for second). It is a grid system, I used to demonstrate that the time was correct by plotting 2 plots. – realr Aug 21 '19 at 16:13
  • 1
    Thanks, your solution worked. I also learned some otherthings. – user2461513 Aug 22 '19 at 02:19