pyqtqgraphicsscene

QGraphicsScene Does Not Appear in Top Left Corner of Window


I've got this very basic plotting code based on How to draw a polyline ("open polygon") in a QGraphicsView going, but I haven't understood why the plot is not appearing in the top left corner. I thought that:

scene = QGraphicsScene(0,0, self.width, self.height) #/10443894/

should be enough. But it does not seem to work. Can someone point out what I need to change to get the plot to the correct position. Thank you very much.

#!/usr/bin/python3
#
from PyQt5.QtGui import QColor,  QFont, QPainter, QPainterPath, QPainterPathStroker, QPen, QPolygonF ####NEW
from PyQt5.QtCore import Qt, QPointF
from PyQt5.QtWidgets import QApplication, QGraphicsScene, QGraphicsPathItem, QGraphicsView, QMainWindow
import sys
class plot(QMainWindow):
#
    def plot_parameters(self, data, width, height):
        len_data = len(data)
# pixel_ratio changes the raw data x-value to a screen x-value
        pixel_ratio_x = width/len_data # transform ratio for x-axis
        min_y = min(data)
        max_y = max(data)
# pixel_ratio changes the raw data y-value to a screen y-value
        pixel_ratio_y = height/(max_y-min_y)
        return pixel_ratio_x, pixel_ratio_y, min_y, max_y, len_data
#
    def y_axis_data(self, min_y, max_y):
# Y labels - a plotdata must be >= 0 - TODO
        y_labels = [max_y]
        yl_increment='1' # label increment
        lenlblvalue=len(str(int(min_y))) # label length
        if min_y>0.0:
            lenlblvalue=len(str(int(max_y)))
            min_y=0.0
        yunit=len(str(max(int(max_y), abs(int(min_y)))))-1 # maximum units of y values
        for i in range(0, yunit): # create increment
            yl_increment+='0'
        lblvalue=int((int(str(max_y)[0])+1)*int(yl_increment))# first lblvalue
        yl_increment=int(yl_increment)*.25
        while lblvalue>=max_y:
            lblvalue-=yl_increment
        while (lblvalue>min_y and lblvalue<max_y): #draw value labels and lines
            lblvalue -= yl_increment
            y_labels.append(lblvalue)
# y axis line tick marks
        return y_labels,yl_increment

    def plot_decoration(self, scene, data, min_y, max_y, width, height):
# create a y-axis with tick marks, labels and lines
        pen = QPen(Qt.gray, 0.5)
# calculate the y-axis line positions
        y_labels,yl_increment = self.y_axis_data(min_y, max_y)
# initial line position on y-axis
        y_position = 0
# add a tick mark and label at y_position
        for l in list(y_labels):
            y_position = l
            line = scene.addLine(0,height-(y_position*self.pixel_ratio_y), width, height-(y_position*self.pixel_ratio_y))
            line.setPen(pen)
            txt = scene.addText(str(l), QFont('sans',8,QFont.Light)) #/27612052/
            txt.setPos(0,height-(y_position*self.pixel_ratio_y)-12) # -12 to line up with tick mark
            y_position += yl_increment # move to next position
#
    def create_plot(self):
        self.width = 700
        self.height = 400
        poly_plot_data = []
# create scene and view
        scene = QGraphicsScene(0,0, self.width, self.height) #/10443894/
        view = QGraphicsView(scene)
        view.show()
# raw data
        data = [-10, -12, 0, -5, -10, -20]
# get parameters of data for plotting
        self.pixel_ratio_x, self.pixel_ratio_y, min_y, max_y, len_data = self.plot_parameters(data, self.width, self.height)
# create polyline data
        for d in range(0, len_data):
            poly_plot_data.append(QPointF(int(d*self.pixel_ratio_x), ((self.height)-(data[d])*self.pixel_ratio_y)))
# draw the axes - TODO x-axis
        self.plot_decoration(scene, data, min_y, max_y, self.width, self.height)
# draw polyline
        poly = QPolygonF(poly_plot_data) # /68704191/
        path = QPainterPath()
        path.addPolygon(poly)
        new_item = QGraphicsPathItem(path, None)
        new_item.setPen(QPen(QColor("red"), 1))
        scene.addItem(new_item)
        return view
#
    def __init__(self):
        super().__init__()
        plot = self.create_plot()
        self.setCentralWidget(plot)
        self.show()
###########################################
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = plot()
    sys.exit(app.exec_())

Solution

  • This is not a PyQt problem, this is a graph-scaling problem.

    You need to take into account the minimum y-value in the calculations that translate from the range of your data to the display-range of the chart you are drawing. The y-values of the data you are plotting are in the range -20 to 0. Most importantly, the minimum y-value is not zero.

    This is the loop that calculates points to add to poly_plot_data, formatted for clarity:

    for d in range(0, len_data):
        poly_plot_data.append(QPointF(
            int(d*self.pixel_ratio_x), ((self.height)-(data[d])*self.pixel_ratio_y)))
    

    The minimum y-value in your data is -20, with self.height being 400 and self.pixel_ratio_y being 20. If data[d] is -20, then ((self.height)-(data[d])*self.pixel_ratio_y) works out at 400-(-20)*20, which equals 800, and this is clearly not in the range 0 to 400.

    If you want the minimum y-value in your data to be scaled to y = 400 on your graphics-scene, you need to subtract the minimum y-value from each data point before multiplying by the scale factor:

    for d in range(0, len_data):
        poly_plot_data.append(QPointF(
            int(d*self.pixel_ratio_x), ((self.height)-(data[d]-min_y)*self.pixel_ratio_y)))
    

    data[d]-min_y is then how far above the minimum your data point is, which we then scale by the pixel ratio, and subtract from 400 to ensure that 0 is at the bottom and 400 is at the top.

    You also need to change the code that plots the tick labels and the lines on your chart to subtract min_y. I'll leave it up to you to make those changes.