Results 1 to 4 of 4

Thread: How not to display all x axis coordinate labels pyqt5

  1. #1
    Join Date
    Aug 2019
    Posts
    7
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default

    How not to display all x axis coordinate labels pyqt5?
    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 https://drive.google.com/file/d/1nzf...ew?usp=sharing

    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
    2. from PyQt5.QtCore import Qt
    3. import math
    4. import numpy as np
    5. import pandas as pd
    6.  
    7. df = pd.read_csv('file.txt',
    8. index_col='DATE',
    9. parse_dates=True,
    10. infer_datetime_format=True)
    11.  
    12.  
    13. date = df.iloc[:, 0].index.date
    14. o = df.iloc[:, 0].values
    15. h = df.iloc[:, 1].values
    16. l = df.iloc[:, 2].values
    17. z = df.iloc[:, 3].values
    18. x = len(z)
    19. x_ = x - 1
    20.  
    21. class MainWindow(QtWidgets.QMainWindow):
    22. def __init__(self, parent=None):
    23. super().__init__(parent)
    24.  
    25. self.step = 30
    26. self._chart_view = QtChart.QChartView()
    27. self.scrollbar = QtWidgets.QScrollBar(
    28. QtCore.Qt.Horizontal,
    29. sliderMoved=self.onAxisSliderMoved,
    30. pageStep=self.step,
    31. )
    32.  
    33.  
    34. self.scrollbar.setRange(0, x_)
    35.  
    36. central_widget = QtWidgets.QWidget()
    37. self.setCentralWidget(central_widget)
    38.  
    39. lay = QtWidgets.QVBoxLayout(central_widget)
    40. for w in (self._chart_view, self.scrollbar):
    41. lay.addWidget(w)
    42.  
    43. self._chart = QtChart.QChart()
    44. self._candlestick_serie = QtChart.QCandlestickSeries()
    45.  
    46. tm = []
    47.  
    48. for i in range(0, len(z)):
    49. o_ = o[i]
    50. h_ = h[i]
    51. l_ = l[i]
    52. c_ = z[i]
    53. self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_))
    54. tm.append(str(i))
    55.  
    56. min_x, max_x = 0, x_
    57.  
    58. self._chart.addSeries(self._candlestick_serie)
    59. self._chart.createDefaultAxes()
    60. self._chart.legend().hide()
    61. self._chart.axisX(self._candlestick_serie).setCategories(tm)
    62. self._chart_view.setChart(self._chart)
    63. self.lims = np.array([min_x, max_x])
    64. self.onAxisSliderMoved(self.scrollbar.value())
    65. self.adjust_axes(1, 31)
    66.  
    67.  
    68. def adjust_axes(self, value_min, value_max):
    69. if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
    70. self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
    71.  
    72. @QtCore.pyqtSlot(int)
    73. def onAxisSliderMoved(self, value):
    74. value2 = value + self.step
    75. value1 = value
    76. if value2 >= x_:
    77. value2 = x_
    78. value1 = value2 - self.step
    79. self.adjust_axes(math.floor(value1), math.ceil(value2))
    80.  
    81.  
    82.  
    83. if __name__ == "__main__":
    84. import sys
    85.  
    86. app = QtWidgets.QApplication(sys.argv)
    87. w = MainWindow()
    88. w.show()
    89. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    123.jpg
    Last edited by quant; 6th June 2020 at 17:19.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,654
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How not to display all x axis coordinate labels pyqt5

    You currently set categories using the string form of the integer index. As a starting point you should try setting something based on the dates you read from the file.
    Then you could investigate how to use QDateTimeAxis

  3. #3
    Join Date
    Aug 2019
    Posts
    7
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: How not to display all x axis coordinate labels pyqt5

    In C#, I did this: I converted datetime(data) data to string.
    There were indexes on the x axis 0, 1, 2, 3..... etc.
    The x axis had captions of datetime data converted to string.
    This was done in order to exclude weekends and holidays when there is no data on the y-axis.
    I don't know how to do this in pyqt.
    I assume that first I need to declare each axis separately, without using createDefaultAxes().

    Here I was told that QCandlestickSeries uses category names.https://stackoverflow.com/questions/...he-pyqt5-chart

    For example, as here I tried to do, but I did not succeed:
    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
    2. from PyQt5.QtCore import Qt
    3. from PyQt5.QtChart import *
    4. import math
    5. import numpy as np
    6. import pandas as pd
    7. import datetime
    8.  
    9. df = pd.read_csv('file.txt',
    10. index_col='DATE',
    11. parse_dates=True,
    12. infer_datetime_format=True)
    13.  
    14.  
    15. date = df.iloc[:, 0].index.date
    16. o = df.iloc[:, 0].values
    17. h = df.iloc[:, 1].values
    18. l = df.iloc[:, 2].values
    19. z = df.iloc[:, 3].values
    20. x = len(z)
    21. x_ = x - 1
    22.  
    23. qt = [None] * x
    24. for i in range(0, x):
    25. qt[i] = (date[i].strftime("%Y/%m/%d"))
    26.  
    27. print(type(date[0]))
    28.  
    29. class MainWindow(QtWidgets.QMainWindow):
    30. def __init__(self, parent=None):
    31. super().__init__(parent)
    32.  
    33. self.step = 30
    34. self._chart_view = QtChart.QChartView()
    35. self.scrollbar = QtWidgets.QScrollBar(
    36. QtCore.Qt.Horizontal,
    37. sliderMoved=self.onAxisSliderMoved,
    38. pageStep=self.step,
    39. )
    40.  
    41.  
    42. self.scrollbar.setRange(0, x_)
    43.  
    44. central_widget = QtWidgets.QWidget()
    45. self.setCentralWidget(central_widget)
    46.  
    47. lay = QtWidgets.QVBoxLayout(central_widget)
    48. for w in (self._chart_view, self.scrollbar):
    49. lay.addWidget(w)
    50.  
    51. self._chart = QtChart.QChart()
    52. self._candlestick_serie = QtChart.QCandlestickSeries()
    53.  
    54. tm = []
    55.  
    56. for i in range(0, len(z)):
    57. o_ = o[i]
    58. h_ = h[i]
    59. l_ = l[i]
    60. c_ = z[i]
    61. #tm = qt[i]
    62. tm.append(date[i])
    63. self._candlestick_serie.append(QtChart.QCandlestickSet(o_, h_, l_, c_))
    64.  
    65.  
    66. min_x, max_x = 0, x_
    67.  
    68. self._chart.addSeries(self._candlestick_serie)
    69. self._chart.legend().hide()
    70.  
    71. self._chart.axisX(self._candlestick_serie).setCategories(tm)
    72. axisX = QDateTimeAxis()
    73. axisX.setTickCount(5)
    74. axisX.setFormat("dd.MM.yyyy")
    75. self._chart.addAxis(axisX, Qt.AlignBottom)
    76. self._candlestick_serie.attachAxis(axisX)
    77.  
    78. axisY = QValueAxis()
    79. axisY.setLabelFormat("%i")
    80. self._chart.addAxis(axisY, Qt.AlignLeft)
    81. self._candlestick_serie.attachAxis(axisY)
    82.  
    83.  
    84. self._chart_view.setChart(self._chart)
    85. self.lims = np.array([min_x, max_x])
    86. self.onAxisSliderMoved(self.scrollbar.value())
    87. self.adjust_axes(1, 31)
    88. #self._chart.axisX(self._candlestick_serie).setTickCount(7)
    89.  
    90.  
    91. def adjust_axes(self, value_min, value_max):
    92. if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
    93. self._chart.axisX(self._candlestick_serie).setRange(str(value_min), str(value_max))
    94.  
    95. @QtCore.pyqtSlot(int)
    96. def onAxisSliderMoved(self, value):
    97. value2 = value + self.step
    98. value1 = value
    99. if value2 >= x_:
    100. value2 = x_
    101. value1 = value2 - self.step
    102. self.adjust_axes(math.floor(value1), math.ceil(value2))
    103.  
    104.  
    105.  
    106. if __name__ == "__main__":
    107. import sys
    108.  
    109. app = QtWidgets.QApplication(sys.argv)
    110. w = MainWindow()
    111. w.show()
    112. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Last edited by quant; 10th June 2020 at 17:14.

  4. #4
    Join Date
    Aug 2019
    Posts
    7
    Thanks
    2
    Qt products
    Platforms
    Windows

    Default Re: How not to display all x axis coordinate labels pyqt5

    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:
    1. Set separate axes for Candlestickseries to adjust parameter axisX.setTickCount()
    2. 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.

    Qt Code:
    1. from PyQt5 import QtCore, QtGui, QtWidgets, QtChart
    2. from PyQt5.QtCore import Qt
    3. from PyQt5.QtChart import *
    4. import math
    5. import numpy as np
    6. import pandas as pd
    7.  
    8. df = pd.read_csv('file.txt',
    9. index_col='DATE',
    10. parse_dates=True,
    11. infer_datetime_format=True)
    12.  
    13. date = df.iloc[:, 0].index.date
    14. o = df.iloc[:, 0].values
    15. h = df.iloc[:, 1].values
    16. l = df.iloc[:, 2].values
    17. z = df.iloc[:, 3].values
    18. x = len(z)
    19. x_ = x - 1
    20.  
    21. qt = [None] * x
    22. for i in range(0, x):
    23. qt[i] = (date[i].strftime("%Y/%m/%d"))
    24.  
    25. class MainWindow(QtWidgets.QMainWindow):
    26. def __init__(self, parent=None):
    27. super().__init__(parent)
    28.  
    29. self.step = 30
    30. self._chart_view = QtChart.QChartView()
    31. self.scrollbar = QtWidgets.QScrollBar(
    32. QtCore.Qt.Horizontal,
    33. sliderMoved=self.onAxisSliderMoved,
    34. pageStep=self.step,
    35. )
    36.  
    37. self.scrollbar.setRange(0, x_)
    38.  
    39. central_widget = QtWidgets.QWidget()
    40. self.setCentralWidget(central_widget)
    41.  
    42. lay = QtWidgets.QVBoxLayout(central_widget)
    43. for w in (self._chart_view, self.scrollbar):
    44. lay.addWidget(w)
    45.  
    46. self._chart = QtChart.QChart()
    47. self._line_serie = QtChart.QLineSeries()
    48.  
    49. for i in range(0, len(z)):
    50. c_ = z[i]
    51. self._line_serie.append(QtCore.QPointF(i, c_))
    52.  
    53. min_x, max_x = 0, x_
    54. self._chart.addSeries(self._line_serie)
    55.  
    56. axisX = QValueAxis()
    57. axisX.setTickCount(5)
    58. axisX.setLabelFormat("%d")
    59. self._chart.addAxis(axisX, Qt.AlignBottom)
    60. self._line_serie.attachAxis(axisX)
    61.  
    62. axisY = QValueAxis()
    63. #axisY.setLabelFormat("%f")
    64. self._chart.addAxis(axisY, Qt.AlignLeft)
    65. self._line_serie.attachAxis(axisY)
    66.  
    67. self._chart.legend().hide()
    68. self._chart_view.setChart(self._chart)
    69. self.lims = np.array([min_x, max_x])
    70. self.onAxisSliderMoved(self.scrollbar.value())
    71. self.adjust_axes(1, 31)
    72.  
    73. def adjust_axes(self, value_min, value_max):
    74. if value_min > 0 and value_max > 0 and value_max <= x_ and value_max > value_min:
    75. self._chart.axisX(self._line_serie).setRange(value_min, value_max)
    76.  
    77. @QtCore.pyqtSlot(int)
    78. def onAxisSliderMoved(self, value):
    79. value2 = value + self.step
    80. value1 = value
    81. if value2 >= x_:
    82. value2 = x_
    83. value1 = value2 - self.step
    84. self.adjust_axes(math.floor(value1), math.ceil(value2))
    85.  
    86.  
    87. if __name__ == "__main__":
    88. import sys
    89.  
    90. app = QtWidgets.QApplication(sys.argv)
    91. w = MainWindow()
    92. w.show()
    93. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. PyQt5 - Reading values from a custom QTablewidget cell
    By sheejo in forum Qt Programming
    Replies: 2
    Last Post: 17th May 2020, 16:04
  2. Replies: 2
    Last Post: 28th July 2019, 21:11
  3. QwtPlot - showing two values on x-Axis
    By Earinor in forum Qwt
    Replies: 3
    Last Post: 30th March 2017, 09:47
  4. Cannot get the axis's current min and max values
    By Alexander111122 in forum Qwt
    Replies: 2
    Last Post: 24th February 2016, 15:18
  5. Y-Axis values draw artifact
    By alex-krutikov in forum Qwt
    Replies: 3
    Last Post: 23rd October 2009, 08:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.