JanvE
Posts: 5
Joined: Sun Feb 11, 2018 3:21 pm

Python & Matplotlib X Ax two hour time span from current time

Sat Mar 31, 2018 1:30 pm

Dear All,
I am looking for a solution to create a graph from the actual time + two hours ahead. So a graph with 20 points over a timespan of two hours. I don't understand how to configure the x ax in a way that it shows only the desired timeframe of two hours. Thanks for any help.

Code: Select all

#Import modules
from xml.dom import minidom
import urllib.request
import csv
import matplotlib.pyplot as plt
from time import sleep
from random import shuffle
import datetime
from matplotlib.dates import DateFormatter


# create data 
y = [ 2,4,6,8,10,12,14,16,18,20,18,16,14,12,10,8,6,4,2]
x = [datetime.datetime.now() + datetime.timedelta(hours=i) for i in range(len(y))] 



# plot
form=DateFormatter('%H:%M')
plt.plot(x,y)

plt.gcf().axes[0].xaxis.set_major_formatter(form)
plt.show()

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python & Matplotlib X Ax two hour time span from current time

Sat Mar 31, 2018 4:46 pm

I haven't looked at your program, but if you simply want to display a fixed number of data points, why not just

Code: Select all

plt.plot(x[-20:], y[-20:])

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python & Matplotlib X Ax two hour time span from current time

Sat Mar 31, 2018 6:47 pm

After an interlude of profound thought, I expect I was not addressing the problem you were having. You probably need to read
https://matplotlib.org/api/_as_gen/matp ... ticks.html
Turn your datetime objects into strings first.

robbes
Posts: 144
Joined: Sun Jan 20, 2013 7:11 pm
Location: Canada - off the west coast

Re: Python & Matplotlib X Ax two hour time span from current time

Sun Apr 01, 2018 4:41 pm

By default, matplotlib is inserting tick values where you don't want them, but these can be over-ridden with the MinuteLocator or HourLocator function.
Also, it is easier to see your data points if you suppress the default line style that matplotlib uses.

Code: Select all

import matplotlib.pyplot as plt
import datetime
from matplotlib.dates import DateFormatter
from matplotlib.dates import MinuteLocator

y = [ 2,4,6,8,10,12,14,16,18,20,18,16,14,12,10,8,6,4,2]
x = [datetime.datetime.now() + datetime.timedelta(minutes=6*i) for i in range(len(y))] 

fig = plt.figure()
ax = fig.add_axes([.1, .2, .8, .7])
ax.xaxis.set_major_locator(MinuteLocator(interval=6))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.plot(x,y, marker='.', linestyle='None')
plt.xticks(rotation=90)
plt.show()

JanvE
Posts: 5
Joined: Sun Feb 11, 2018 3:21 pm

Re: Python & Matplotlib X Ax two hour time span from current time

Thu Apr 12, 2018 4:27 pm

robbes wrote:
Sun Apr 01, 2018 4:41 pm
By default, matplotlib is inserting tick values where you don't want them, but these can be over-ridden with the MinuteLocator or HourLocator function.
Also, it is easier to see your data points if you suppress the default line style that matplotlib uses.

Code: Select all

import matplotlib.pyplot as plt
import datetime
from matplotlib.dates import DateFormatter
from matplotlib.dates import MinuteLocator

y = [ 2,4,6,8,10,12,14,16,18,20,18,16,14,12,10,8,6,4,2]
x = [datetime.datetime.now() + datetime.timedelta(minutes=6*i) for i in range(len(y))] 

fig = plt.figure()
ax = fig.add_axes([.1, .2, .8, .7])
ax.xaxis.set_major_locator(MinuteLocator(interval=6))
ax.xaxis.set_major_formatter(DateFormatter('%H:%M'))
ax.plot(x,y, marker='.', linestyle='None')
plt.xticks(rotation=90)
plt.show()
Ok, thank you!

Return to “Python”