PDA

View Full Version : How not to display QDateTimeAxis data on weekends and holidays(PyQt5)



quant
16th June 2020, 10:35
I managed to sort out the axes. But I am not satisfied that QDateTimeAxis creates data on the x-axis, where there is no data on the y-axis.
In C# and matplotlib, I was able to solve this problem by resetting the axis labels to string format.
Here is an example of how this is done in matplotlib https://matplotlib.org/3.2.1/gallery/text_labels_and_annotations/date_index_formatter.html#sphx-glr-gallery-text-labels-and-annotations-date-index-formatter-py

I don't know how to do this in pyqt5(Qt), and I can't find any examples.
Data " file.txt" are located here https://drive.google.com/file/d/1nzfD9K1ROEMXU4MKWoLSfp13CxUuyMSN/view

Below I attach a screenshot of what these empty values look like
13472


from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
from PyQt5.QtCore import *
from PyQt5.QtChart import *
import math
import numpy as np
import pandas as pd

df = pd.read_csv('file.txt',
index_col='DATE',
parse_dates=True,
infer_datetime_format=True)

date = df.iloc[:, 0].index.date
o = df.iloc[:, 0].values
h = df.iloc[:, 1].values
l = df.iloc[:, 2].values
z = df.iloc[:, 3].values
x = len(z)
x_ = x - 1

qt = [None] * x

for i in range(0, x):
qt[i] = QDateTime(date[i]).toMSecsSinceEpoch()

class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)

self.step = 30
self._chart_view = QtChart.QChartView()
self.scrollbar = QtWidgets.QScrollBar(
QtCore.Qt.Horizontal,
sliderMoved=self.onAxisSliderMoved,
pageStep=self.step,
)

self.scrollbar.setRange(0, x_)

central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)

lay = QtWidgets.QVBoxLayout(central_widget)
for w in (self._chart_view, self.scrollbar):
lay.addWidget(w)

self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()

for i in range(0, len(z)):
o_ = o[i]
h_ = h[i]
l_ = l[i]
c_ = z[i]
tm = float(qt[i])
self._candlestick_serie.append(QtChart.QCandlestic kSet(o_, h_, l_, c_, tm))


min_x, max_x = 0, x_

self._chart.addSeries(self._candlestick_serie)
self._chart.legend().hide()

self._chart_view.setChart(self._chart)
self.lims = np.array([min_x, max_x])

axisX = QDateTimeAxis()
axisX.setTickCount(5)
axisX.setFormat("yyyy-MM-dd")
self._chart.addAxis(axisX, Qt.AlignBottom)
self._candlestick_serie.attachAxis(axisX)

axisY = QValueAxis()
self._chart.addAxis(axisY, Qt.AlignLeft)
self._candlestick_serie.attachAxis(axisY)


self.onAxisSliderMoved(self.scrollbar.value())
self.adjust_axes(1, 31)

def adjust_axes(self, value_min, value_max):
if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
self._chart.axisX(self._candlestick_serie).setRang e(QDateTime(date[value_min]), QDateTime(date[value_max]))


@QtCore.pyqtSlot(int)
def onAxisSliderMoved(self, value):
value2 = value + self.step
value1 = value
if value2 >= x_:
value2 = x_
value1 = value2 - self.step
self.adjust_axes(math.floor(value1), math.ceil(value2))


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())