I have been working with the PlotPy library (not to be confused with Plotly) to create a Python Qt GUI to display a series of plots — one of which happens to be a 2D Histogram. Following their example with some modifications to the data, I was able to create a Histogram2DItem
that displays 150,000 dots on a scatter plot that appear within the X values of 0.57-13.57 and the Y values of 1.59-15.46 (rounded to the nearest hundredth).
In order to generate this data, the following code is processed:
from numpy import array, concatenate, dot, random
#...
N = 150000
matrix = array([[2.0, 0.2], [3, 5.0]])
X_array_data_1 = random.normal(2, 0.3, size=(N, 2))
X_array_data_2 = random.normal(2, 0.3, size=(N, 2))
X = concatenate((X_array_data_1 + [0, 1.0], dot(X_array_data_2, matrix) + [-1, -1.0]))
column_1 = X[:,0]
column_2 = X[:,1]
combined_columns = X[:,0] + X[:,1]
# See below for the setup_scatterplot() definition.
self.setup_scatterplot(column_1, column_2, combined_columns)
In addition, I have set the BasePlot
(which the Histogram2DItem
is added into) so that it limits itself from 0.0 to 10.0 on both axes. The idea with setting the plot up this way was to make it so that the X value of 0 appeared on the far left of the graph and the Y value of 0 appeared on the bottom of the graph, setting a clearly-defined origin to the graph. What appears, instead, is the following histogram with an X-axis that begins at around -0.2 and a Y-axis begins at 1.8.
The setup_scatterplot()
function (see below) then instantiates the necessary PlotPy items. Keep in mind that PlotPy is the successor to guiqwt, both of which are higher-level iterations of the Qwt library. This is important to understand, as Qwt functions and types have been considered while looking for potential solutions to this issue.
import numpy as np
from PySide6 import QtWidgets
from PySide6.QtWidgets import QVBoxLayout
from plotpy.items import Histogram2DItem
from plotpy.styles import Histogram2DParam
#...
def setup_scatterplot(self, x_axis, y_axis, z_axis):
X = x_axis
Y = y_axis
Z = z_axis
# NOTE: The PlotPy documentation uses the "make" function to create its Histogram.
# This, however, does not change the outcome on the graph.
histogram2d_param = Histogram2DParam()
histogram2d_param.label = "Markers"
self.hist2d = Histogram2DItem(np.asarray(X), np.asarray(Y), histogram2d_param, Z=Z)
self.scatter_plot_panel = QVBoxLayout()
self.scatter_plot = BasePlot()
self.scatter_plot.set_plot_limits(0.0, 10.0, 0.0, 10.0, 2, 0) # 2 is the X-axis and 0 is the Y-axis.
self.scatter_plot.set_aspect_ratio(lock=True)
self.scatter_plot.setAxisAutoScale(False) # This doesn't seem to fix auto-scaling at all.
self.scatter_plot.add_item(self.hist2d)
self.scatter_plot.replot() # Replotting or not, the graph does not seem to update.
self.scatter_plot.set_axis_direction(0, False) # Sets the Y-axis to go from 0 on the bottom and up.
self.scatter_plot_panel.addWidget(self.scatter_plot)
From what I have noticed: by itself, prior to adding the 2D histogram, the scatter_plot
shows a graph that goes from 0 to 10 on the X- and Y-axes without issue. It is only after the .add_item(self.hist2d)
operation where the maximum and minimum values of both axes are changed to how they appear in the image above.
For those more familiar with Qwt: the PlotPy BasePlot
type inherits QwtPlot
. The set_plot_limits()
function in BasePlot
calls upon another function in the BasePlot
code called set_axis_limits()
, which calls upon the QwtPlot function setAxisScale()
.
I have seen others state that fixing the X-axis length is as simple as disabling the auto-scaling and manually setting the axes with the .setAxisScale()
function. As I eventually want to apply zooming+scrolling functionality to this histogram, I have also looked into setting the zoom range, resulting in my finding a comment about implementing the QwtPlotZoomer
class, but this is not available to me.
If anyone knows anything about PlotPy/Qwt and how to set limits on the plot after applying a histogram to it, any help at all would be greatly appreciated!