Fix PlotItem.setAxisItems (#1376)

* Fix PlotItem.setAxisItems

- Use extend so visibleAxes remains a flat list.
- More robust logic for detecting adding an AxisItem instance to
  mulitple plots and suggest a workaround in the error message.

* Simplify membership check

* Add test for PlotItem setAxisitem logic
This commit is contained in:
Kenneth Lyons 2020-09-22 20:25:02 -07:00 committed by GitHub
parent e2dc21e2ca
commit a90c443b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 3 deletions

View File

@ -307,7 +307,7 @@ class PlotItem(GraphicsWidget):
# Array containing visible axis items
# Also containing potentially hidden axes, but they are not touched so it does not matter
visibleAxes = ['left', 'bottom']
visibleAxes.append(axisItems.keys()) # Note that it does not matter that this adds
visibleAxes.extend(axisItems.keys()) # Note that it does not matter that this adds
# some values to visibleAxes a second time
for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))):
@ -325,8 +325,11 @@ class PlotItem(GraphicsWidget):
if k in axisItems:
axis = axisItems[k]
if axis.scene() is not None:
if axis != self.axes[k]["item"]:
raise RuntimeError("Can't add an axis to multiple plots.")
if k not in self.axes or axis != self.axes[k]["item"]:
raise RuntimeError(
"Can't add an axis to multiple plots. Shared axes"
" can be achieved with multiple AxisItem instances"
" and set[X/Y]Link.")
else:
axis = AxisItem(orientation=k, parent=self)

View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
import pytest
import pyqtgraph as pg
app = pg.mkQApp()
@pytest.mark.parametrize('orientation', ['left', 'right', 'top', 'bottom'])
def test_PlotItem_shared_axis_items(orientation):
"""Adding an AxisItem to multiple plots raises RuntimeError"""
ax1 = pg.AxisItem(orientation)
ax2 = pg.AxisItem(orientation)
layout = pg.GraphicsLayoutWidget()
pi1 = layout.addPlot(axisItems={orientation: ax1})
pi2 = layout.addPlot()
# left or bottom replaces, right or top adds new
pi2.setAxisItems({orientation: ax2})
with pytest.raises(RuntimeError):
pi2.setAxisItems({orientation: ax1})