'pg.PlotCurveItem() stepmode="center" issue with x and y data shape

i have a problem with the pyqtgraph PlotCurveItem() function. This is the code. The plot1 is shown in another part of the program.

... 
vals = np.random.random(size=100)*0.8
y, x = np.histogram(vals,bins=np.linspace(min(vals),max(vals),num=50)
curve = pg.PlotCurveItem()
curve.setData(x,y,stepMode="center")
self.plot1.addItem(curve)
...

I want to make a histogram with stepMode="center" but there occure two error codes which contradict each other as one needs the shapes of x and y to be same and the other one says they need to be len(x)=len(y)+1.

When i make the length of array x as mentioned i will get alternately either one or the other error:

 - Error1
...
File ".../lib/python3.8/site-packages/pyqtgraph/widgets/GraphicsView.py", line 142, in paintEvent
    return super().paintEvent(ev)
  File ".../lib/python3.8/site-packages/pyqtgraph/debug.py", line 124, in w
    printExc('Ignored exception:')
  --- exception caught here ---
  File ".../lib/python3.8/site-packages/pyqtgraph/debug.py", line 122, in w
    func(*args, **kwds)
  File ".../lib/python3.8/site-packages/pyqtgraph/graphicsItems/PlotCurveItem.py", line 768, in paint
    self.paintGL(p, opt, widget)
  File ".../lib/python3.8/site-packages/pyqtgraph/graphicsItems/PlotCurveItem.py", line 884, in paintGL
    pos[:,1] = y
ValueError: could not broadcast input array from shape (49,) into shape (50,)
  printExc('Ignored exception:')



 - Error2
...
File ".../lib/python3.8/site-packages/pyqtgraph/graphicsItems/PlotCurveItem.py", line 479, in setData
    self.updateData(*args, **kargs)
  File ".../lib/python3.8/site-packages/pyqtgraph/graphicsItems/PlotCurveItem.py", line 532, in updateData
    raise Exception("len(X) must be len(Y)+1 since stepMode=True (got %s and %s)" % (self.xData.shape, self.yData.shape))
Exception: len(X) must be len(Y)+1 since stepMode=True (got (49,) and (49,))

Thanks for any help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source