Example with datetime axis

7,244 views
Skip to first unread message

Yan

unread,
Oct 9, 2013, 12:43:07 AM10/9/13
to pyqt...@googlegroups.com
Is there any example that shows how to display datetime format on the axis?

Thanks

Luke Campagnola

unread,
Oct 9, 2013, 11:21:06 AM10/9/13
to pyqt...@googlegroups.com
On Wed, Oct 9, 2013 at 12:43 AM, Yan <yanko...@gmail.com> wrote:
Is there any example that shows how to display datetime format on the axis?


Not really.. there is a partial implementation in examples/customPlot.py, but it needs more work.
 
Luke

Nathaniel Saul

unread,
Nov 29, 2013, 6:42:12 PM11/29/13
to pyqt...@googlegroups.com
Hi Luke,
First off, pyqtgraph has been great to the project I'm working on.  Thanks a bunch!
Second.  I too am trying to implement a plot with datetime on the x axis.  Right now the x axis is in Unix time in microseconds.  I need to keep the data within this order of magnitude, but I'd really like it to have it display in a more natural format.    I've tried playing around with the DateAxis class in the customPlot.py that you suggested above but can't get it to come out correctly.  Is there anything other examples I can go off of?  Any help would be greatly appreciated.

Thanks,
Nathan

Luke Campagnola

unread,
Dec 1, 2013, 10:51:58 AM12/1/13
to pyqt...@googlegroups.com
On Fri, Nov 29, 2013 at 6:42 PM, Nathaniel Saul <saul...@gmail.com> wrote:
Hi Luke,
First off, pyqtgraph has been great to the project I'm working on.  Thanks a bunch!
Second.  I too am trying to implement a plot with datetime on the x axis.  Right now the x axis is in Unix time in microseconds.  I need to keep the data within this order of magnitude, but I'd really like it to have it display in a more natural format.    I've tried playing around with the DateAxis class in the customPlot.py that you suggested above but can't get it to come out correctly.  Is there anything other examples I can go off of?  Any help would be greatly appreciated.

Hi Nathan,
I have been working on a date/time axis; you can see the current state of it here: https://github.com/lcampagn/pyqtgraph/tree/date_time_axisitem

This branch adds graphicsItems/DateTimeAxisItem.py and examples/DateTimeAxis.py. It is designed to allow the user to specify what formatting they want for each scale, while providing a suitable set of defaults. You can see there are still many issues with it, but hopefully it is a good starting place.

Luke

shx2

unread,
May 31, 2014, 4:28:11 PM5/31/14
to pyqt...@googlegroups.com
Hi everybody,

I've implemented my own DateAxis, which seems to be working nicely. Is this of any interest to anybody? Should I submit a pull request?

One thing about my implementation is that it is using pandas for timestamp manipulation, which might cause an undesirable external dependency... The reason I decided to use pandas is that it provides the best tools I konw for manipulating timestamps and time frequencies (by far, IMO).

shx2

Luke Campagnola

unread,
May 31, 2014, 11:38:47 PM5/31/14
to pyqt...@googlegroups.com
On Sat, May 31, 2014 at 4:28 PM, shx2 <shx...@gmail.com> wrote:
Hi everybody,

I've implemented my own DateAxis, which seems to be working nicely. Is this of any interest to anybody? Should I submit a pull request?

I am very interested; this is an often-requested feature. (but please do not be discouraged if it takes me some time to review your PR)
 
One thing about my implementation is that it is using pandas for timestamp manipulation, which might cause an undesirable external dependency... The reason I decided to use pandas is that it provides the best tools I konw for manipulating timestamps and time frequencies (by far, IMO).

Yes, that would be problematic. I imagine there is not too much code required for this..  is it possible to just copy or re-implement the relevant code from Pandas? If you do copy anything, please include a note about the original source and license of the code.


Luke

Lukas

unread,
Jun 10, 2014, 8:42:32 AM6/10/14
to pyqt...@googlegroups.com
Hi shx

I would love to try your DateAxis implementation if your willing to share it.

Thanks
Lukas

Werner Macho

unread,
Jun 17, 2014, 5:10:29 PM6/17/14
to pyqt...@googlegroups.com
Hi!

I am also very interested in trying your solution before I am going to implement my own one ...
Could you please make a pull request on the Project?

regards
Werner

Lukas

unread,
Jun 19, 2014, 7:20:38 AM6/19/14
to pyqt...@googlegroups.com
Hi everyone

I just submitted a pull request for my own implementation of a DateAxisItem. It's not very well tested but seems to work fine in my own application. The date axis provides basic time zone support in terms of a configurable utc_offset, supports multiple tick levels, has no 3rd party dependencies (I think) and should be fairly easy to customize with new zoom levels, tick format strings etc. 

I hope it's useful 
Message has been deleted

Blot Romain

unread,
Jul 29, 2014, 9:46:34 AM7/29/14
to pyqt...@googlegroups.com
Hi,

I would to try it too but I can find it? Where did you submit it?

Thx

Lukas

unread,
Jul 29, 2014, 9:52:32 AM7/29/14
to pyqt...@googlegroups.com
The pull request is here https://github.com/pyqtgraph/pyqtgraph/pull/74, it hasn't been merged yet. If you just want to try it feel free to clone my fork.

- L

Peter Maunz

unread,
Sep 6, 2014, 11:43:08 PM9/6/14
to pyqt...@googlegroups.com
Hi Lukas,

Thanks so much for sharing your DateAxisItem. It looks very good. In testing I found the following bugs/inconsistencies:

The date conversion does not recognize daylight savings time. time.timezone only has the correction for the standard time of the timezone.
you could change line 175 to be
        self.utcOffset = (datetime.utcnow()-datetime.now()).total_seconds()
with this change plots are in the local time.

If a xAxisLabel is used, let's say "local time" then it appears as "local time (x 1e+09)".
probably DateAxisItem should reimplement labelString. It might also be nice to add the parts of the date that are not shown on the axis here. For example if the window shows 16:00 to 17:00 one could use "6/9/2014" here.

Best,
--Peter

reptile Forex

unread,
Jan 23, 2015, 5:26:10 PM1/23/15
to pyqt...@googlegroups.com
Hi,

There is no examples with pandas or external files. Maybe is simple, but always usefull.
Can someone explain why for some datatime cases exist this error?

    (1999-06-08, 1.0290,1.0479,1.0261,1.0458,28381),
              ^
SyntaxError: invalid token

IMPORTANT! This only for for 1999-06-08, 1999-06-09  other not made any problem in script. If line 53 and 53 are deleted (with 08 and 09 days..) script working (attachment). Soo.. example with long file is usefull. It was hard to find problem here.

The example data:
data = [  ## fields are (time, open, close, low, high).
(1999-06-01, 00:00, 1.0424,1.0479,1.0410,1.0454,30595),
(1999-06-02, 00:00, 1.0457,1.0470,1.0323,1.0358,33521),
(1999-06-03, 00:00, 1.0356,1.0421,1.0301,1.0330,37554),
(1999-06-04, 00:00, 1.0309,1.0386,1.0275,1.0373,30627),
(1999-06-07, 00:00, 1.0362,1.0370,1.0256,1.0301,26735),
(1999-06-08, 00:00,1.0290,1.0479,1.0261,1.0458,28381),
(1999-06-09, 00:00,1.0460,1.0492,1.0420,1.0467,25783),
(1999-06-10, 00:00, 1.0465,1.0527,1.0433,1.0489,31368),
(1999-06-11, 00:00, 1.0485,1.0566,1.0465,1.0514,24104),
]
item = CandlestickItem(data)
plt = pg.plot()
plt.addItem(item)
plt.setWindowTitle('pyqtgraph example: customGraphicsItem')


..yes i operating with """#for (datetime, open, close, low, high, volumen) in self.data:  """ but looks this is other problem here
1.PNG
2.PNG

Upol Ryskulova

unread,
Mar 30, 2015, 5:14:30 AM3/30/15
to pyqt...@googlegroups.com
Hello everyone,
pyqtgraph really made my job easier, thank you Luke. 
Right now, I am trying to implement time axis using AxisItem class. But I got extra axis line on top of the window. How can I fix this? I also have tried your DateTimeAixsItem.py from the samples folder, it also displayed that extra line.

Regards,
Upol
extraAxis.png

Hauke Müntinga

unread,
Apr 24, 2015, 11:53:20 AM4/24/15
to pyqt...@googlegroups.com
Hi everyone,

I have the exact same problem as Upol: When I use my implementation of a TimeAxisItem, that extra axis is shown. Any ideas where to fix this?

Best regards,
Hauke

Upol Ryskulova

unread,
Apr 25, 2015, 2:52:06 AM4/25/15
to pyqt...@googlegroups.com
Hello Hauke,
Luke did fix this bug in the github source code, it is going to be fixed with the next release(as Luke said). I updated my PlotItem.py in the directory /usr/lib/python2.7/dist-packages/pyqtgraph/graphicsItems/PlotItem with the fixed PlotItem.py located on github account without waiting next release.





--
You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyqtgraph/pSIn0xNGodA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/affaa055-cdde-4852-9040-2743c3668553%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Timothy Victor

unread,
Sep 11, 2015, 3:57:11 PM9/11/15
to pyqtgraph
Hi Luke / All

Did the "extra axis" issue make it to the 0.9.10 build?

Thanks

Tim

Upol Ryskulova

unread,
Sep 12, 2015, 7:55:23 AM9/12/15
to pyqt...@googlegroups.com
Hi Timothy,

in the latest version 0.9.10 the extra axis issue still remains. You can directly download pyqtgraph library from the github and install it by yourself without using deb or exe installers.

I did it on ubuntu by these commands
git clone https://github.com/pyqtgraph/pyqtgraph
cd pyqtgraph
sudo python setup.py install

Regards,
Upol

Xiaofan Zhang

unread,
Sep 13, 2015, 3:55:36 AM9/13/15
to pyqtgraph, shx...@gmail.com
Hi shx2,

Could you share your DateAxis implementation? Thanks.

-Xiaofan

Petr Maršík

unread,
Sep 15, 2015, 5:10:45 PM9/15/15
to pyqtgraph
Hi,

I'm trying to implement time series plot and I've been modifying your customPlot.py. I had to made a little change in the tickString method, because my dataset contains samples (every 5 minutes) from the 1st January 2013 and ends in 2015. My code:
import pandas as pd
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import time
import sys, os
from datetime import datetime,timedelta

fileName = 'C:/..../dftext.hdf5'
df = pd.read_hdf(fileName,'data')
timeDataSet = df.iloc[:,0]
dateTime = timeDataSet.values
#dateTimeSampled = timeDataSet.keys()
#cleaneddtList = [x for x in datetime if str(x) == 'nan']
tempDataSet = df.iloc[:,8]
print 'plotting time series for temperature column: %s' % tempDataSet.name
temp = tempDataSet.values
N = len(dateTime)
vzorky = np.array(range(N))*5*60

class DateAxis(pg.AxisItem):
    def tickStrings(self, values, scale, spacing):
        #values= np.array(range(248833))*5
        ts = (dateTime[0] - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1,'s')
        ddt = (ts/(3600*24))
        strns = []
        rng = max(vzorky)-min(vzorky)
        if rng < 120:
            return pg.AxisItem.tickStrings(self, values, scale, spacing)
        if rng < 3600*24:
            string = '%H:%M:%S'
            label1 = '%b %d -'
            label2 = ' %b %d, %Y'
        elif rng >= 3600*24 and rng < 3600*24*30:
            string = '%d'
            label1 = '%b - '
            label2 = '%b, %Y'
        elif rng >= 3600*24*30 and rng < 3600*24*30*12:
            string = '%b'
            label1 = '%Y -'
            label2 = ' %Y'
        elif rng >=3600*24*30*12:
            string = '%Y'
            label1 = ''
            label2 = ''
        for x in vzorky:

            try:
                strns.append(time.strftime(string, (datetime.timetuple(datetime.utcfromtimestamp(x)+timedelta(days=ddt)))))
            except ValueError:  ## Windows can't handle dates before 1970
                strns.append('')
        try:
            label = time.strftime(label1, datetime.timetuple((datetime.utcfromtimestamp(min(vzorky))+timedelta(days=ddt)))) \
                        +time.strftime(label2, datetime.timetuple((datetime.utcfromtimestamp(max(vzorky))+timedelta(days=ddt))))
        except ValueError:
            label = ''
            
app = pg.mkQApp()

axis = DateAxis(orientation='bottom')

win = pg.GraphicsWindow()
axis = DateAxis(orientation='bottom')
plot = win.addPlot(axisItems={'bottom': axis})
curve = plot.plot(x=vzorky,y=temp)

#pg.plot(x=dateTimeSampled, y=temp)
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    
    
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()


I have a problem though. The graph shows, however the x-axis does not, and it throws out error, for which I can't figure out solution:
TypeError                                 Traceback (most recent call last)
C:\Users\Petr\Anaconda\lib\site-packages\pyqtgraph\graphicsItems\AxisItem.pyc in paint(self, p, opt, widget)
    523                 picture = QtGui.QPicture()
    524                 painter = QtGui.QPainter(picture)
--> 525                 specs = self.generateDrawSpecs(painter)
    526                 profiler('generate specs')
    527                 if specs is not None:

C:\Users\Petr\Anaconda\lib\site-packages\pyqtgraph\graphicsItems\AxisItem.pyc in generateDrawSpecs(self, p)
    928                 strings = tickStrings[i]
    929
--> 930             if len(strings) == 0:
    931                 continue
    932

TypeError: object of type 'NoneType' has no len()

Can someone help please? Or if someone post some newer example, I'd really appreciate it.

Sorry for my terrible English.

Dne středa 9. října 2013 17:21:06 UTC+2 Luke Campagnola napsal(a):

Timothy Victor

unread,
Sep 15, 2015, 5:58:01 PM9/15/15
to pyqtgraph
Thanks Upol.   Sorry for the delayed response.

I'll build the app locally as you suggest.

Tim

Ober Wallis

unread,
Jan 3, 2018, 7:53:32 AM1/3/18
to pyqtgraph
Dear Luke

First many thanks to this cool and helpful tool.

I stumbled over this thread during my search of examples and advice how to plot data over timestamp. Currently I am using version 0.10.0 and since in version 0.9.11 you guys have been playing around with the plotting over time and therefore I was wondering wheter there is an easy way now to do so...

I tried this Thread but it didn't solve my problems, as I have a GUI made with Qt Designer, so I failed to implement the "CAxisTime"-class.

Also in the documentation I couldn't find much, except of this hint that something has been implemented working with time: Displaying images and video

Best regards

Andreas

Carlos Pascual

unread,
Jan 8, 2018, 4:19:59 AM1/8/18
to pyqt...@googlegroups.com
Hi Andreas,

You can find an starting example in <pyqtgraph>/examples/customPlot.py

But IMHO, the implemented axis item there is too basic for real purposes
(e.g., the tick values do not align with natural time units).

We implemented a slightly more advanced DateAxisItem for our own project
(Taurus) which you may find useful:

https://gist.github.com/cpascual/cdcead6c166e63de2981bc23f5840a98

I hope it helps

Carlos

PS: The original code comes from:
https://github.com/cpascual/taurus/blob/tep17/lib/taurus/qt/qtgui/tpg/
... but the above gist is adapted by removing all Taurus-specific code so that
it can be executed as-is. My intention is to submit this (and other similar
stuff) with a PR to pyqtgraph if Luke is interested on it .

On Wednesday, January 3, 2018 4:53:31 AM CET Ober Wallis wrote:
> Dear Luke
>
> First many thanks to this cool and helpful tool.
>
> I stumbled over this thread during my search of examples and advice how to
> plot data over timestamp. Currently I am using version 0.10.0 and since in
> version 0.9.11 you guys have been playing around with the plotting over
> time and therefore I was wondering wheter there is an easy way now to do
> so...
>
> I tried this Thread
> <https://groups.google.com/forum/#!topic/pyqtgraph/kGHogKpcZC0> but it
> didn't solve my problems, as I have a GUI made with Qt Designer, so I
> failed to implement the "CAxisTime"-class.
>
> Also in the documentation I couldn't find much, except of this hint that
> something has been implemented working with time: Displaying images and
> video <http://www.pyqtgraph.org/documentation/images.html?highlight=time>
>
> Best regards
>
> Andreas
>
> Am Sonntag, 1. Juni 2014 05:38:47 UTC+2 schrieb Luke Campagnola:
> > On Sat, May 31, 2014 at 4:28 PM, shx2 <shx...@gmail.com <javascript:>>
> >
> > wrote:
> >> Hi everybody,
> >>
> >> I've implemented my own DateAxis, which seems to be working nicely. Is
> >> this of any interest to anybody? Should I submit a pull request?
> >
> > I am very interested; this is an often-requested feature. (but please do
> > not be discouraged if it takes me some time to review your PR)
> >
> >> One thing about my implementation is that it is using pandas for
> >> timestamp manipulation, which might cause an undesirable external
> >> dependency... The reason I decided to use pandas is that it provides the
> >> best tools I konw for manipulating timestamps and time frequencies (by
> >> far,
> >> IMO).
> >
> > Yes, that would be problematic. I imagine there is not too much code
> > required for this.. is it possible to just copy or re-implement the
> > relevant code from Pandas? If you do copy anything, please include a note
> > about the original source and license of the code.
> >
> >
> > Luke


--
+----------------------------------------------------+
Carlos Pascual Izarra
Scientific Software Coordinator
Computing Division
ALBA Synchrotron [http://www.albasynchrotron.es]
Carrer de la Llum 2-26
E-08290 Cerdanyola del Valles (Barcelona), Spain
E-mail: cpas...@cells.es
Phone: +34 93 592 4428
+----------------------------------------------------+

Ober Wallis

unread,
Jan 11, 2018, 2:33:17 AM1/11/18
to pyqtgraph
Hola Carlos,

Many thanks, I will give it a try!

Andreas

Lukas Heiniger

unread,
Jan 11, 2018, 3:02:38 AM1/11/18
to pyqt...@googlegroups.com

Hi Andreas

 

Here’s another one I implemented years ago: https://github.com/3rdcycle/pyqtgraph/tree/date-axis-item

It never got merged for some reason and I haven’t pulled upstream for almost a year but should still work with the latest version of pyqtgraph I guess.

 

Lukas

--

You received this message because you are subscribed to a topic in the Google Groups "pyqtgraph" group.
To unsubscribe from this topic, visit


To unsubscribe from this group and all its topics, send an email to

Ober Wallis

unread,
Jan 15, 2018, 8:14:55 AM1/15/18
to pyqtgraph
Sali Lukas,

Many thanks to you too. I 'll have a look and let you know what finally worked for me.

Best regards,
Andreas

.
To unsubscribe from this group and all its topics, send an email to
pyqtgraph+unsubscribe@googlegroups.com.


To view this discussion on the web visit

Reply all
Reply to author
Forward
0 new messages