Skip to content

Zoom/Pan not working properly [Bug]:  #21575

Open
@inxp

Description

@inxp

Bug summary

Zoom/Pan not working properly in the following PyQt5 example with new version of Matplotlib. (I checked with 3.4.0 and 3.4.3)

Everything works properly with matplotlib 3.3.4 (Check gifs)

Problem occurs only when multiple plots are embedded.

After sometime of switching between figures and zooming/panning following error is shown.

Traceback (most recent call last):
  File "\lib\site-packages\matplotlib\cbook\__init__.py", line 270, in process
    func(*args, **kwargs)
  File "\lib\site-packages\matplotlib\backend_bases.py", line 3074, in _zoom_pan_handler
    self.release_pan(event)
  File "\lib\site-packages\matplotlib\backend_bases.py", line 3147, in release_pan
    ax.end_pan()
  File "\lib\site-packages\matplotlib\axes\_base.py", line 4239, in end_pan
    del self._pan_start
AttributeError: _pan_start

Thanks

Code for reproduction

import sys
import os

	
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QAbstractItemView, QAction, QApplication, QFileDialog, QInputDialog
from PyQt5.uic import loadUiType

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
	FigureCanvasQTAgg as FigureCanvas,
	NavigationToolbar2QT as NavigationToolbar)
	
Ui_MainWindow, QMainWindow = loadUiType('window.ui')

class Main(QMainWindow, Ui_MainWindow):
	def __init__(self, ):
		super(Main, self).__init__()
		self.setupUi(self)
		self.fig_dict = {}
		self.figdata_dict = {}
		
		self.mplfigs.itemClicked.connect(self.changefig)
		
		fig = Figure()
		self.addmpl(fig)
		
	
	def changefig(self, item):
		text = item.text()
		text = str(text)
		self.rmmpl()
		self.addmpl(self.fig_dict[text])
	
		
	def addfig(self, name, fig):
		self.fig_dict[name] = fig
		self.mplfigs.addItem(name)
		
	def addmpl(self,fig):
		self.canvas = FigureCanvas(fig)
		self.mplvl.addWidget(self.canvas)
		self.canvas.draw()
		self.toolbar = NavigationToolbar(self.canvas,self.mplwindow, coordinates=2)
		self.toolbar.zoom()
		self.mplvl.addWidget(self.toolbar)
		
	def rmmpl(self,):
		self.mplvl.removeWidget(self.canvas)
		self.canvas.close()
		self.mplvl.removeWidget(self.toolbar)
		self.toolbar.close()
 
 
if __name__ == '__main__':

	fig1 = Figure()
	ax1f1 = fig1.add_subplot(111)
	ax1f1.plot([1,2,3,1,2,3])
	
	fig2 = Figure()
	ax1f2 = fig2.add_subplot(111)
	ax1f2.plot([100,200,300,400,100,200,300])
	ax1f2.plot([200,300,400,200,300,400])
	
	app = QApplication(sys.argv)
	main = Main()
	main.addfig('Oneplot', fig1)
	main.addfig('Twoplots', fig2)
	main.show()
	sys.exit(app.exec_())

Actual outcome

Expected outcome

Operating system

Windows 10 64-bit

Matplotlib Version

3.4.3

Matplotlib Backend

pyqt5

Python version

3.9.7

Jupyter version

No response

Other libraries - window.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1280</width>
    <height>768</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <widget class="QWidget" name="mplwindow" native="true">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Expanding" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="mouseTracking">
       <bool>false</bool>
      </property>
      <property name="focusPolicy">
       <enum>Qt::ClickFocus</enum>
      </property>
      <layout class="QVBoxLayout" name="mplvl"/>
     </widget>
    </item>
    <item>
     <widget class="QListWidget" name="mplfigs">
      <property name="sizePolicy">
       <sizepolicy hsizetype="Fixed" vsizetype="Expanding">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="minimumSize">
       <size>
        <width>200</width>
        <height>0</height>
       </size>
      </property>
      <property name="maximumSize">
       <size>
        <width>200</width>
        <height>16777215</height>
       </size>
      </property>
      <property name="focusPolicy">
       <enum>Qt::NoFocus</enum>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1280</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Installation

pip

Conda channel

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions