问题描述
我试图在pyqtgraph中使用X轴中的日期或时间
我尝试此问题How can I use DateAxisItem of PyQtGraph?中的解决方案 但是对我没有用
这是我的桌子
CREATE TABLE IF NOT EXISTS transactions (
id SERIAL PRIMARY KEY UNIQUE NOT NULL,montant DECIMAL(100,2),medecin VARCHAR,date_d DATE,time_d TIME,users_id INTEGER,FOREIGN KEY(users_id) REFERENCES users(id))
这是用户界面
<?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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QPushButton" name="pushButton_113">
<property name="geometry">
<rect>
<x>550</x>
<y>72</y>
<width>121</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Recherche</string>
</property>
</widget>
<widget class="QDateEdit" name="dateEdit_19">
<property name="geometry">
<rect>
<x>40</x>
<y>70</y>
<width>471</width>
<height>41</height>
</rect>
</property>
<property name="dateTime">
<datetime>
<hour>0</hour>
<minute>0</minute>
<second>0</second>
<year>2020</year>
<month>1</month>
<day>1</day>
</datetime>
</property>
<property name="calendarPopup">
<bool>true</bool>
</property>
</widget>
<widget class="PlotWidget" name="graphicsView_2">
<property name="geometry">
<rect>
<x>60</x>
<y>130</y>
<width>691</width>
<height>391</height>
</rect>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>PlotWidget</class>
<extends>QGraphicsView</extends>
<header location="global">pyqtgraph</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
这是我最后一次尝试
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
from pyqtgraph import PlotWidget,plot
import pyqtgraph as pg
import pandas as pd
from pandas import *
from datetime import datetime
import psycopg2
from admin import Ui_MainWindow as ui
class TimeAxisItem(pg.AxisItem):
def tickStrings(self,values,scale,spacing):
return [datetime.fromtimestamp(value) for value in values]
class MainApp(QMainWindow,ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.Handel_Buttons()
def Handel_Buttons(self):
self.pushButton_113.clicked.connect(self.draw_graph_all)
def draw_graph_all(self): #pushButton_113
self.connection = psycopg2.connect(user="postgres",password="password",host="localhost",database="database")
self.cur = self.connection.cursor()
self.cur.execute( '''SELECT date_d,SUM(montant) FROM transactions GROUP BY date_d ''')
rows = self.cur.fetchall()
date_axis = TimeAxisItem(orientation='bottom')
self.graphicsView_2 = pg.PlotWidget(axisItems = {'bottom': date_axis})
date = []
montant = []
for row in rows:
date.append(row[0])
montant.append(row[1])
#[x.timestamp() for x in date]
self.graphicsView_2.plot(x=pd.to_datetime(date),y=montant,pen=None,symbol='o')
self.setCentralWidget(self.graphicsView_2)
self.graphicsView_2.show()
更新
经过几次尝试后,我能得到的最好结果是图表显示了Y轴,但没有X轴像图像一样
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)