matplotlib crosshair cursor in PyQt dialog does not show up

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


matplotlib crosshair cursor in PyQt dialog does not show up



I want to have a crosshair at the cursor in my matplotlib window. This works out in the example, given in the matplotlib gallery. But unfortunately, it does not work if I have the matplotlib widget in a Qt dialog window (QDialog).


matplotlib


QDialog



This is my sample of code, where I want to instantiate the matplotlib.widgets.Cursor object, but nothing shows up.


matplotlib.widgets.Cursor


import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.figure = plt.figure(facecolor='white')
self.canvas = FigureCanvas(self.figure)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)

''' plot some random stuff '''
ax = self.figure.add_subplot(111)
self.ax = ax
ax.plot([1,2])

# Set cursor
Cursor(self.ax, useblit=False, color='red', linewidth=1)
self.canvas.draw()

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())



Can someone help?




2 Answers
2



I have modified your code as follows and it works on my computer. Hope it helps.


import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
from matplotlib.widgets import Cursor

class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.figure = plt.figure(facecolor='white')
self.canvas = FigureCanvas(self.figure)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.canvas)
self.setLayout(layout)

''' plot some random stuff '''
ax = self.figure.add_subplot(111)
self.ax = ax
ax.plot([1,2])
# Set cursor
cursor = Cursor(self.ax, useblit=False, color='red', linewidth=1)

############## The added part: #############
def onclick(event):
cursor.onmove(event)
self.canvas.mpl_connect('button_press_event', onclick)
############################################
self.canvas.draw()

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())



The original code in (1):
Cursor(self.ax, useblit=False, color='red', linewidth=1)
don't leave a reference to the Cursor as indicated in the documentation.



The code in (2) has the assignment but not to a class reference but to a local variable:
cursor = Cursor(self.ax, useblit=False, color='red', linewidth=1)



The code in (2) works with the indicated additions but the cursor move slowly.



The solution is the code as in (1) but modified as follows:
self.cursor = Cursor(self.ax, useblit=False, color='red', linewidth=1)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

jucg1mFnxrLrUYBOl,lyXynl8xF6noICgGAKy75LaIVvvyBc3S Kunlw lgDu,PtEgz,3,rlPMlkBfvlJEgOxTT,RR,GBSzVdq Nd
cHePwsu8s s,zQuZ6,qlT7mR0ibHmw,n9

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty