Unsolved How not to display all x axis coordinate labels pyqt5
-
How not to display all x axis coordinate labels?
For example every 30 values.It would also be good to find out how instead of indexes 1, 2, 3 ......195, 196 display datetime data, but without days off.
Data " file.txt" are located here link text
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart from PyQt5.QtCore import Qt 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 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() tm = [] for i in range(0, len(z)): o_ = o[i] h_ = h[i] l_ = l[i] c_ = z[i] self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_)) tm.append(str(i)) min_x, max_x = 0, x_ self._chart.addSeries(self._candlestick_serie) self._chart.createDefaultAxes() self._chart.legend().hide() self._chart.axisX(self._candlestick_serie).setCategories(tm) self._chart_view.setChart(self._chart) self.lims = np.array([min_x, max_x]) 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).setRange(str(value_min), str(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_())
-
@quant12345 said in How not to display all x axis coordinate labels pyqt5:
How not to display all x axis coordinate labels?
For example every 30 values.I don't know, but have you looked at the properties https://doc.qt.io/qt-5/qvalueaxis.html#tickCount-prop (used in example at https://doc.qt.io/qt-5/qtcharts-multiaxis-example.html#using-multiple-axes), https://doc.qt.io/qt-5/qvalueaxis.html#tickInterval-prop & https://doc.qt.io/qt-5/qvalueaxis.html#minorTickCount-prop? There is also an example with custom labels in https://doc.qt.io/qt-5/qtcharts-customchart-example.html, though I don't think you'll want that.
-
@Denni-0 Sorry, I just saw that the link to the file doesn't work.
Now everything is fine, I updated the file link. -
@JonB I tried to figure it out myself using the Qt documentation, but I couldn't.
I assume that you do not need to use " create Default Ax es()".
Set the axes separately. I have little experience in Qt. So I don't know how to do it.
I have an understanding of how to do this in matplotlib, but I like pyqt better. -
I managed to set the x and y axes separately for the line graph.
It was not possible to install separate axes for QCandlestickSeries.I need to do two things:
- Set separate axes for Candlestickseries to adjust parameter axisX.setTickCount()
- Display the x-axis converted datetime to string, instead of indexes
P.S. At least the example on the line graph from my second(2) question will do.
If this is not possible in pyqt5, then also write. So as not to waste time.
```
from PyQt5 import QtCore, QtGui, QtWidgets, QtChart from PyQt5.QtCore import Qt 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] = (date[i].strftime("%Y/%m/%d")) 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._line_serie = QtChart.QLineSeries() for i in range(0, len(z)): c_ = z[i] self._line_serie.append(QtCore.QPointF(i, c_)) min_x, max_x = 0, x_ self._chart.addSeries(self._line_serie) axisX = QValueAxis() axisX.setTickCount(5) axisX.setLabelFormat("%d") self._chart.addAxis(axisX, Qt.AlignBottom) self._line_serie.attachAxis(axisX) axisY = QValueAxis() #axisY.setLabelFormat("%f") self._chart.addAxis(axisY, Qt.AlignLeft) self._line_serie.attachAxis(axisY) self._chart.legend().hide() self._chart_view.setChart(self._chart) self.lims = np.array([min_x, max_x]) 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._line_serie).setRange(value_min, 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_())