PyQtGraph - How to set intervals of axis

家住魔仙堡 提交于 2019-12-12 04:29:18

问题


Below is a function I have written that create a graph based on a list of tuples of scores (scores and their frequency)

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)

This produces the following graph:

Is there any way to set the intervals of the y-axis so that the numbers go up in steps of 1, but the range is still autoscaled? eg. the only number displayed on the example graph's y-axis would be 0, 1, 2


回答1:


You must change the ticks on the AxisItem, by example:

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_()

Before:

After:



来源:https://stackoverflow.com/questions/41078849/pyqtgraph-how-to-set-intervals-of-axis

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!