pyqtpysideqchart

PySide\PyQt How to set custom frequency and color of QChart's gridlines


There is a simple graph. How to do:

  1. Set custom gridlines color.

  2. By default, a gridlines is drawn at each tick mark on the axes. I need marks on the X axis to be, for example, every 5 points, and the grid has 20 points each (as in the 'picture').

from PySide6.QtWidgets import QVBoxLayout, QApplication
from PySide6.QtCharts import QLineSeries, QChart, QChartView, QValueAxis
from PySide6.QtGui import QColor, QBrush
import sys
import math


class Chart1(QChart):
    def __init__(self):
        super().__init__()
        series = QLineSeries()
        x_axis = QValueAxis()
        y_axis = QValueAxis()
        self.setAxisX(x_axis)
        self.setAxisY(y_axis)
        self.legend().setVisible(False)
        for i in range (0, 100, 1):
            series.append(i, math.sin(i))
        self.addSeries(series)
        self.axisX().setRange(0, 100)
        self.axisY().setRange(-1,1)
        x_axis.setTickType(QValueAxis.TickType.TicksDynamic)
        x_axis.setTickInterval(20)
        y_axis.setTickType(QValueAxis.TickType.TicksDynamic)
        y_axis.setTickInterval(0.2)
        axis_brush = QBrush(QColor("white"))
        y_axis.setLabelsBrush(axis_brush)
        x_axis.setLabelsBrush(axis_brush)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    c1 = Chart1()
    chart_view = QChartView(c1)
    chart_view.chart().setBackgroundBrush(QBrush("black"))
    chart_view.show()
    sys.exit(app.exec())

enter image description here

I couldn't find any discussion of such possibilities.


Solution

  • ups, everything turned out to be very simple.

    Grid color: x_axis.setGridLineColor(QColor("red"))

    More frequent grating with the same frequency of marks on the axisx_axis.setMinorTickCount(5)