【问题标题】:PyQtGraph - How to set intervals of axisPyQtGraph - 如何设置轴的间隔
【发布时间】:2017-04-26 00:28:58
【问题描述】:

下面是我编写的一个函数,它根据分数元组列表(分数及其频率)创建图表

def initialise_chart(self, scores):

    pg.setConfigOption("background", "w")
    pg.setConfigOption("foreground", "k")

    results_graph = pg.PlotWidget()
    self.chart_results.addWidget(results_graph)
    results_graph.plot([i[0] for i in scores], [i[1] for i in scores], pen={'color': "#006eb4"})
    results_graph.setLabels(left="Frequency", bottom="Scores")
    results_graph.setXRange(0, self.max_mark, padding=0)

这会产生以下图表:

有什么方法可以设置 y 轴的间隔,使数字以 1 为步长增加,但范围仍然是自动缩放的?例如。示例图的 y 轴上显示的唯一数字是 0、1、2

【问题讨论】:

    标签: python pyqt5 pyqtgraph


    【解决方案1】:

    您必须更改 AxisItem 上的刻度,例如:

    import pyqtgraph as pg
    import numpy as np
    from pyqtgraph.Qt import QtCore, QtGui
    
    app = pg.mkQApp()
    
    pw = pg.PlotWidget(title="Example")
    x = np.arange(20)
    y = x**2/150
    pw.plot(x=x, y=y, symbol='o')
    pw.show()
    pw.setWindowTitle('Example')
    
    ax = pw.getAxis('bottom')  # This is the trick
    dx = [(value, str(value)) for value in list((range(int(min(x.tolist())), int(max(x.tolist())+1))))]
    ax.setTicks([dx, []])
    
    ay = pw.getAxis('left')  # This is the trick
    dy = [(value, str(value)) for value in list((range(int(min(y.tolist())), int(max(y.tolist())+1))))]
    ay.setTicks([dy, []])
    
    if __name__ == '__main__':
        import sys
    
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()
    

    之前:

    之后:

    【讨论】:

    • 非常感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 2019-11-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 2011-11-25
    • 1970-01-01
    • 2018-07-19
    相关资源
    最近更新 更多