PDA

View Full Version : Number of tick marks on the axis in QBarCategoryAxis.



quant
8th September 2020, 15:05
I managed to put string values on the x axis(datetime data is converted to a string).
Created on the x axis QBarCategoryAxis.
Everything would be fine, but I can't adjust tick marks on the axis.

To avoid being verbose more details here https://forum.qt.io/topic/118786/can-i-set-string-values-on-the-x-axis-with-the-ability-to-adjust-the-number-of-values-something-like-settickcount/2

If that's not possible, say so.
Since this idea is long-standing and time-consuming.

ChrisW67
14th September 2020, 06:24
Looking at the docs for QBarCategoryAxis the intended use of this Axis is for discrete categories on a bar chart, not an abbreviation of a continuous time line. Bar chart categories are typically few in number. This class renders as many tick marks as needed to separate the categories.

The QDateTimeAxis class is for displaying a continuous time line, which includes the two days of weekend (I assume) that result in gaps because you do not have data on those days.
You might be able to use QDateTimeAxis considering the answer in this Stack Overflow response: https://stackoverflow.com/questions/50434059/how-to-hide-some-of-the-categories-in-qbarcategoryaxis-in-qt.
Ultimately, I think you would end up writing your own QuantWeekdayTimeAxis subclass of QAbstractAxisClass to achieve what you want.

As a completely different approach look at Qwt, particularly the QwtPlotTradingCurve (https://qwt.sourceforge.io/class_qwt_plot_trading_curve.html) (also https://qwt.sourceforge.io/otherscreenshots.html). The learning curve here is steeper, but the flexibility is almost certainly there.

quant
20th September 2020, 14:10
Thank you, I did all this earlier.
Maybe you can still change the labels for the axes?

quant
19th November 2022, 16:50
I still haven't been able to solve the problem. If I use the QDateTimeAxis axis for the candlestick chart, then when there is no data, empty gaps are obtained in the chart. I did as advised and created a line chart with the QDateTimeAxis axis, and for the candlestick chart I used the QValueAxis axis. But, it's still far from what need. On the chart, candlestick and line (int_line_serie) charts have a QValueAxis axis, another line chart (dt_line_serie) has a QDateTimeAxis axis. It can be seen that the first two match, but the linear (green line) QDateTimeAxis does not. I searched a lot how to create a QuantWeekdayTimeAxis subclass of QAbstractAxisClass but didn't find anything. Can someone tell me how to do this in pyqt or at least in C ++.

Need an axis so that I can adjust the number of divisions. In matplotlib, you can use the date time in string format, so this problem was solved.

13771



from PyQt5 import QtWidgets, QtChart
from PyQt5.QtGui import QPen
from PyQt5.QtCore import *
from PyQt5.QtChart import *
import pandas_datareader.data as web

df = web.DataReader('GE', 'yahoo', start='2022-05-01', end='2022-05-25')

date = df.index
x = len(date)

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._chart_view = QtChart.QChartView()

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

lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(self._chart_view)

self._chart = QtChart.QChart()
self._candlestick_serie = QtChart.QCandlestickSeries()
self.dt_line_serie = QtChart.QLineSeries()
self.int_line_serie = QtChart.QLineSeries()

st = QPen(Qt.red)
st.setWidth(3)
self.int_line_serie.setPen(st)

for i in range(0, len(df)):
o_ = df.iloc[i, 2]
h_ = df.iloc[i, 0]
l_ = df.iloc[i, 1]
c_ = df.iloc[i, 3]
self._candlestick_serie.append(QtChart.QCandlestic kSet(o_, h_, l_, c_, float(i)))
self.dt_line_serie.append(qt[i], o_)
self.int_line_serie.append(float(i), o_)

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

self._chart_view.setChart(self._chart)

axisX = QValueAxis()
axisX.setLabelFormat("%d")
self._chart.addAxis(axisX, Qt.AlignBottom)
self._candlestick_serie.attachAxis(axisX)
self.int_line_serie.attachAxis(axisX)

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

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


if __name__ == "__main__":
import sys

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

d_stranz
20th November 2022, 17:27
Why don't you check out QCustomPlot (https://www.qcustomplot.com/)? It is much better than the brain-dead QChart library, and you can do virtually anything with it through customization. Things like this. (https://www.qcustomplot.com/index.php/demos/financialchartsdemo)

Edit - Sorry, looks like there may not be Python bindings.