如何平滑pyqtgraph中的图形?

How to smooth a graph in pyqtgraph?

我用 pyqtgraph 绘制了一个图表,现​​在我正在尝试“平滑”图表(而不是锯齿形),(我读过)应该与 antialias 一起使用,但我找不到正确的“位置”使其发挥作用。

import sys
from PyQt5 import QtWidgets
import pyqtgraph as pg

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.plot()

    def plot(self):
        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)
        self.graphWidget.setBackground('w')
        pen = pg.mkPen(color=('b'), width=2)
        styles = {'color':'r', 'font-size':'15px'}

        self.graphWidget.setConfigOptions(antialias=True)  # AttributeError: setConfigOptions

        self.graphWidget.setLabel('left', 'x-values', **styles)
        self.graphWidget.setLabel('bottom', 'y-values', **styles)
        
        x_values = [1, 2, 3, 4, 5]
        y_values = [2, 1, 2, 4, 3]

        self.graphWidget.plot(x_values, y_values, pen=pen, symbol='o',
                                   symbolSize=8, symbolPen='k', symbolBrush='k')

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

我也试过了,但没有成功(没有做任何不同的事情):

pen = pg.mkPen(color=('b'), width=2, antialias=True)
self.graphWidget.plot(x_values, y_values, pen=pen, symbol='o',
                                   symbolSize=8, symbolPen='k', symbolBrush='k', antialias=True)

如果有人能告诉我如何使用此权限(或者是否有其他选项可以平滑图形),我将非常高兴。

我正在使用 Python 3.8、PyQt5 5.15.0、PyCharm 2020.2 和 Linux Mint 19.3。

anti-aliasing属性的目的不是为了让绘制的曲线平滑,而是为了让画的流畅。如果你想要平滑的曲线,那么你必须对点进行插值,例如使用 scipy.interpolate.make_interp_spline:

import sys
from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np
from scipy.interpolate import make_interp_spline


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.plot()

    def plot(self):
        pg.setConfigOptions(antialias=True)
        self.graphWidget = pg.PlotWidget()
        self.setCentralWidget(self.graphWidget)
        self.graphWidget.setBackground("w")
        pen = pg.mkPen(color=("b"), width=2)
        styles = {"color": "r", "font-size": "15px"}

        self.graphWidget.setLabel("left", "x-values", **styles)
        self.graphWidget.setLabel("bottom", "y-values", **styles)

        x_values = [1, 2, 3, 4, 5]
        y_values = [2, 1, 2, 4, 3]

        xnew = np.linspace(min(x_values), max(x_values), 100)
        spl = make_interp_spline(x_values, y_values, 3)
        ynew = spl(xnew)

        self.graphWidget.plot(xnew, ynew, pen=pen)


def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

documentation 中所述,您必须为 模块设置抗锯齿:

pg.setConfigOptions(antialias=True)

或者,由于所有 pyqtgraph 图实际上都是 QGraphicsView, set the appropriate renderHints() 的子类:

self.graphWidget.setRenderHints(QtGui.QPainter.Antialiasing)