How can I get pyqtgraph to accept my setting of the YRange?
-
I have a graph where the YRange is important. Depending on a setting (_use_metric) I want one set of values for min and max, and if _use_metric is different, I want a different set of min and max. In the following code my debugging confirms which values are to be used for the min and max. But then what is displayed is yet another, third. set of min and max. It even has a negative min even though ALL the y-data are positive fractions. When pyqtgraph substitutes a negative min it destroys the whole point of a scree plot is looking at the decreasing y values for the "elbow" where the data flattens. How do I get pyqtgraph to use my setting of the range? I do have the .setYRange statement but it is not being used. I tried many alternatives where I change supplying or ignoring padding. None of that appears to impact the result. The XRange correctly uses 1, 10. Only YRange is overridden.
def _plot_scree(self): print(f"DEBUG -- at top of plot_scree {self._use_metric=}") # print(f"DEBUG -- in plot_scree A {self.min_stress=}") graphics_layout_widget = pg.GraphicsLayoutWidget() graphics_layout_widget.setBackground('w') plot = graphics_layout_widget.addPlot(title="Scree Diagram", color="k", size="25pt") plot.setAspectLocked(True, ratio=1) plot.setLabel( 'bottom', "Number of Dimensions", color="k", size='15pt') plot.setLabel( 'left', "Stress", color="k", size='15pt') plot.showGrid(x=True, y=True) pen = pg.mkPen(color=(255, 0, 0)) x_coords = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # y_coords = [.24, .18, .13, .13, .12, .11, .10, .09, .08, .07] # y_coords = [24, 18, 13, 13, 12, 11, 10, 9, 8, 7] y_coords = self._min_stress plot.setXRange(1, 10, padding=None) print(f"DEBUG -- in plot_scree B \n {x_coords=} \n {y_coords=}") if self._use_metric: max_stress = max(y_coords) show_max_stress = math.ceil(max_stress) # ax.axis([1, 10, 0, show_max_stress]) print(f"DEBUG -- in plot_scree C \n {show_max_stress=} \n {self._max_stress=} ") plot.setYRange(1, show_max_stress, padding=0) else: show_min_stress = int(self._min_stress[0] + 2) # show_min_stress = max(y_coords) # ax.axis([1, 10, 0, show_min_stress]) print(f"DEBUG -- in plot_scree D \n {show_min_stress=} \n {self._min_stress=} ") plot.setYRange(0, show_min_stress, padding=0) # plot.setYRange(0, 1, padding=0) y_range = plot.viewRange()[1] print(f"DEBUG -- in plot_scree E \n {y_range=}") line = pg.PlotDataItem(x_coords, y_coords, pen=pen) plot.addItem(line) y2_range = plot.viewRange()[1] print(f"DEBUG -- in plot_scree E \n {y2_range=}") return graphics_layout_widget
-
I found the issue. I had used setAspectLocked(True, ratio=1) in a previous plot and neglected to remove it. Once I removed it, my range of values worked fine.
-