Pyqtgraph - how add axis Datetime?

Viewed 1042

The example uses the file 'QtChart.ui' (this is important) the widget for Pyqtgraph is embedded in it (this is also important).

PROBLEM: The graph is displayed, but the seconds since the beginning of the epoch are displayed instead of the date-time axis.

I set the variable axis = pg.DateAxisItem(orientation='bottom') Following the example, it should be added to such a line pw = pg.PlotWidget(viewBox=vb, axisItems={'bottom': axis}), but I do not have such a line in my code.

How to set the date-time axis instead of seconds from the beginning of the epoch?

CODE:

import random
import sys
from PyQt5.QtCore import *
from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import QMainWindow
import time
import pyqtgraph as pg

x = []
y = []

class draw_interface(QMainWindow):
    def __init__(self):
        super(draw_interface, self).__init__()
        uic.loadUi('QtChart.ui', self)

        self.timer_update_events = QTimer()
        self.timer_update_events.timeout.connect(self.update_chart)
        self.timer_update_events.start(1000)

        axis = pg.DateAxisItem(orientation='bottom')

        self.line = pg.PlotCurveItem(clear=True, pen="g")
        self.widget.addItem(self.line)

        self.show()

    def update_chart(self):
        global x, y

        x.append(time.time())
        y.append(random.uniform(0, 1))
        self.line.setData(x, y)

my_app = QtWidgets.QApplication([])
my_main_window = draw_interface()
sys.exit(my_app.exec_())

AND CODE 'QtChart.ui':

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1319</width>
    <height>1091</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <layout class="QGridLayout" name="gridLayout">
      <property name="leftMargin">
       <number>10</number>
      </property>
      <property name="topMargin">
       <number>10</number>
      </property>
      <property name="rightMargin">
       <number>10</number>
      </property>
      <property name="bottomMargin">
       <number>10</number>
      </property>
      <item row="0" column="0">
       <widget class="QFrame" name="frame_2">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="maximumSize">
         <size>
          <width>200</width>
          <height>16777215</height>
         </size>
        </property>
        <property name="frameShape">
         <enum>QFrame::StyledPanel</enum>
        </property>
        <property name="frameShadow">
         <enum>QFrame::Raised</enum>
        </property>
       </widget>
      </item>
      <item row="0" column="1">
       <widget class="PlotWidget" name="widget" native="true"/>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1319</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <customwidgets>
  <customwidget>
   <class>PlotWidget</class>
   <extends>QWidget</extends>
   <header location="global">pyqtgraph</header>
   <container>1</container>
  </customwidget>
 </customwidgets>
 <resources/>
 <connections/>
</ui>
1 Answers

You have to use the setAxisItems() method:

# ...
axis = pg.DateAxisItem(orientation='bottom')
self.line = pg.PlotCurveItem(clear=True, pen="g")
self.widget.addItem(self.line)
self.widget.setAxisItems({"bottom": axis})
# ...
Related