Issue
I am trying to create a line graph for a Pyqt application, the graph it self works fine but I am annoyed by the fact that it opens a window for a split second which then closes and the plot moves to the Qt QGraphicsView. I have searched for a solution but I can not seem to find any answers that work.
def lineChartTest(self):
x = np.array(list(range(0, 10)))
y = np.array([3, 7, 5, 11, 8, 13, 9, 16, 15, 12])
x_smooth = np.linspace(x.min(), x.max(), 300)
spl = make_interp_spline(x, y, k=3)
y_smooth = spl(x_smooth)
plot = pg.plot()
plot.setGeometry(0, 0, 1007, 500)
plot.setLabel('bottom', 'X-axis Values')
plot.setLabel('left', 'Y-axis Values')
plot.setXRange(0, 10)
plot.setYRange(0, 20)
plot.setTitle("Test pyqtgraph")
plot.setBackground('1E1B44')
line = plot.plot(x_smooth, y_smooth, pen=pg.mkPen('E42AFF', width=2))
plot.addLegend()
self.scene.addWidget(plot)
This is the code I am using to create the plot currently.
Solution
The plot()
function automatically shows the PlotWidget before returning it (see sources), so it's briefly displayed as a top level window before being added to the view.
Since you're not adding any arguments to plot()
, you don't really need that function, and you can just create a new PlotWidget instance and add that to the view:
def lineChartTest(self):
# ...
plot = pg.PlotWidget()
# ...
On the other hand, a PlotWidget already is a QGraphicsView, so it might not have a lot of sense to add a view to the scene of another one. Consider using a PlotWidget instead of the QGraphicsView, and add PlotItems to it.
Answered By - musicamante
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.