How can I draw a cross which follows my mouse cursor? - python

I want draw a cross in my paintEvent() which moves with the mouse. I'm using python via pyqt.
I'm using the code below, but the result isn't good.
from __future__ import division
import sys
import platform
# Qt4 bindings for core Qt functionalities (non-GUI)
import PyQt4.QtCore as QtCore
# Python Qt4 bindings for GUI objects
import PyQt4.QtGui as QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
# import the Qt4Agg FigureCanvas object, that binds Figure to
# Qt4Agg backend. It also inherits from QWidget
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
# Matplotlib Figure object
from matplotlib.figure import Figure
class C_MplCanvas(FigureCanvas):
def __init__(self):
# setup Matplotlib Figure and Axis
self.fig = Figure()
#self.cursor = Cursor()
self.fig.set_facecolor('black')
self.fig.set_edgecolor('black')
#self.ax = self.fig.add_subplot(111)
#self.ax.patch.set_facecolor('black')
# initialization of the canvas
FigureCanvas.__init__(self, self.fig)
#super(FigureCanvas, self).__init__(self.fig)
# we define the widget as expandable
FigureCanvas.setSizePolicy(self,
QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Expanding)
# notify the system of updated policy
FigureCanvas.updateGeometry(self)
self.xx=0
self.yy=0
self.justDoubleClicked=False
def contextMenuEvent(self, event):
menu = QMenu(self)
oneAction = menu.addAction("&One")
twoAction = menu.addAction("&Two")
self.connect(oneAction, SIGNAL("triggered()"), self.one)
self.connect(twoAction, SIGNAL("triggered()"), self.two)
'''
if not self.message:
menu.addSeparator()
threeAction = menu.addAction("Thre&e")
self.connect(threeAction, SIGNAL("triggered()"),
self.three)
'''
menu.exec_(event.globalPos())
def one(self):
self.message = QString("Menu option One")
self.update()
def two(self):
self.message = QString("Menu option Two")
self.update()
def three(self):
self.message = QString("Menu option Three")
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.TextAntialiasing)
#painter.drawText(self.rect(), Qt.AlignCenter, text)
#
#painter.setPen('red')
pen=painter.pen()
painter.setPen(QColor(255, 0, 0))
painter.drawLine(self.xx-100,self.yy,self.xx+100,self.yy)
painter.drawLine(self.xx,self.yy-100,self.xx,self.yy+100)
self.update()
def mouseReleaseEvent(self, event):
if self.justDoubleClicked:
self.justDoubleClicked = False
else:
self.setMouseTracking(not self.hasMouseTracking())
self.update()
def mouseMoveEvent(self, event):
self.xx=event.pos().x()
self.yy=event.pos().y()
self.update()
class C_MPL(QWidget):
def __init__(self, parent = None):
# initialization of Qt MainWindow widget
super(C_MPL, self).__init__(parent)
#QtGui.QWidget.__init__(self, parent)
# instantiate a widget, it will be the main one
#self.main_widget = QtGui.QWidget(self)
#vbl = QtGui.QVBoxLayout(self.main_widget)
# set the canvas to the Matplotlib widget
self.canvas = C_MplCanvas()
# create a vertical box layout
self.vbl = QtGui.QVBoxLayout()
# add mpl widget to vertical box
self.vbl.addWidget(self.canvas)
# set the layout to th vertical box
self.setLayout(self.vbl)
if __name__ == "__main__":
import sys
'''
def valueChanged(a, b):
print a, b
'''
app = QApplication(sys.argv)
form = C_MPL()
#form.connect(form, SIGNAL("valueChanged"), valueChanged)
form.setWindowTitle("C_MPL")
#form.move(0, 0)
form.show()
#form.resize(400, 400)
app.exec_()
#bmu: That's great,just like I want.And now there another question:
cid0=self.mpl_connect('axes_enter_event', self.enter_axes)
cid1=self.mpl_connect('button_press_event', self.onpick)
cid2=self.mpl_connect('motion_notify_event', self.onmove)
cid3=self.mpl_connect('draw_event', self.clear)
cid4=self.mpl_connect('key_press_event',self.press)
the strange thing is the 'key_press_event' can't be triggerd but all other events can. there r a case: macosx backend ignores multiple mpl_connect() calls
https://github.com/matplotlib/matplotlib/pull/585,
but i think it's differnt with me.
i got cid0,1,2,3,4 which is different each other
So , any idea can share? I an very crazy now.....
Following is my code,u can test it if u have the same problem:
import sys
import platform
from PyQt4 import QtGui, QtCore
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt4 import NavigationToolbar2QT as NavigationToolbar
import time
from PyQt4.QtCore import *
from PyQt4.QtGui import *
#Just a test
class MplCanvas(FigureCanvas):
def __init__(self):
# initialization of the canvas
self.fig=Figure()
FigureCanvas.__init__(self,self.fig )
self.ax = self.fig.add_axes([.15, .15, .75, .75])
self.canvas = self.ax.figure.canvas
#my added
#self.ax = self.fig.add_axes([.15, .15, .75, .75])
#cursor = C_Cursor(self.LvsT, useblit=True, color='red', linewidth=2 )
x=np.arange(0,20,0.1)
self.ax.plot(x,x*x,'o')
self.ax.set_xlim(-2,2)
self.ax.set_ylim(-2,2)
self.visible = True
self.horizOn = True
self.vertOn = True
self.useblit = True
#if self.useblit:
#lineprops['animated'] = True
self.lineh = self.ax.axhline(self.ax.get_ybound()[0], visible=False)
self.linev = self.ax.axvline(self.ax.get_xbound()[0], visible=False)
self.background = None
self.needclear = False
self.count = 0
cid0=self.mpl_connect('axes_enter_event', self.enter_axes)
cid1=self.mpl_connect('button_press_event', self.onpick)
cid2=self.mpl_connect('motion_notify_event', self.onmove)
cid3=self.mpl_connect('draw_event', self.clear)
cid4=self.mpl_connect('key_press_event',self.press)
self.draw()
def clear(self, event):
'clear the cursor'
if self.useblit:
self.background = self.canvas.copy_from_bbox(self.ax.bbox)
self.linev.set_visible(False)
self.lineh.set_visible(False)
def onmove(self, event):
'on mouse motion draw the cursor if visible'
print("move")
if event.inaxes != self.ax:
self.linev.set_visible(False)
self.lineh.set_visible(False)
if self.needclear:
self.canvas.draw()
self.needclear = False
return
self.needclear = True
if not self.visible: return
self.linev.set_xdata((event.xdata, event.xdata))
self.lineh.set_ydata((event.ydata, event.ydata))
self.linev.set_visible(self.visible and self.vertOn)
self.lineh.set_visible(self.visible and self.horizOn)
self._update()
def _update(self):
if self.useblit:
if self.background is not None:
self.canvas.restore_region(self.background)
self.ax.draw_artist(self.linev)
self.ax.draw_artist(self.lineh)
self.canvas.blit(self.ax.bbox)
else:
self.canvas.draw_idle()
return False
#
def enter_axes(self,event):
print "Enter"
def onpick(self,event):
print "click"
print 'you pressed', event.canvas
a = np.arange(10)
print a
print self.count
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(a)
fig.show()
def press(self,event):
print ('press', event.key)
self.fig.canvas.draw()
class MplWidget(QtGui.QWidget):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.vbl = QtGui.QVBoxLayout()
self.canvas = MplCanvas()
self.vbl.addWidget(self.canvas)
self.setLayout(self.vbl)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = MplWidget()
form.show()
#form.resize(400, 400)
app.exec_()
#all:Tks all. I resolved this problem after added:
self.canvas.setFocusPolicy( Qt.ClickFocus )
self.canvas.setFocus()
maybe this a little bug bcs i need explicity setfocus on cavans,maybe if not:)
details u can check https://github.com/matplotlib/matplotlib/issues/707

Here is an example using matplotlib event handling (however I am wondering, if it is really useful to have something like a second cursor).
import numpy as np
import matplotlib.pyplot as plt
class MouseCross(object):
def __init__(self, ax, **kwargs):
self.ax = ax
self.line, = self.ax.plot([0], [0], visible=False, **kwargs)
def show_cross(self, event):
if event.inaxes == self.ax:
self.line.set_data([event.xdata], [event.ydata])
self.line.set_visible(True)
else:
self.line.set_visible(False)
plt.draw()
if __name__ == '__main__':
fig, ax = plt.subplots()
ax.plot(np.random.random(100) * 10.0)
# note that not every "normal" matplotlib marker will work
# math symbols work fine
cross = MouseCross(ax, marker=r'$\bigoplus$', markersize=30,
color='red',)
fig.canvas.mpl_connect('motion_notify_event', cross.show_cross)
plt.tight_layout()
plt.show()

Question...are you trying to change what the cursor looks like over your widget? If so, the easiest thing is to do get rid of the paintEvent entirely, and add setCursor to your init method:
class C_MplCanvas(FigureCanva):
def __init__( self ):
# setup code
...
self.setCursor(Qt.CrossCursor)
This will tell Qt to use the Cross Cursor whenever the mouse is over that particular widget. This will actually replace the arrow (which may or may not be what you want). If that isn't what you want, I would recommend doing something where you create a PNG image of what you do want, create a child QLabel, and move the child widget around in your mouseMoveEvent.
If you need more help on that lemme know.

Related

How to persist changes made by the NavigationToolbar on a periodically updated graph

I have a PyQt5 GUI application. This application shows a graph (with my default xlim and ylim) that gets updated every second, a real-time graph basically. This functionality I have, but I want to add a NavigationToolbar so one can zoom in/out the graph.
I added the toolbar to my layout and it gets displayed. So far so good. Now I zoom in, the graph gets zoomed in, but once the graph gets periodically updated the xlim and ylim are defaulted again and the zoom is gone. What properties I need to call from the toolbar so I can save them and pass them to my _update_canvas function? I looked at https://matplotlib.org/3.1.0/api/axes_api.html, and noticed the function get_ylim. So i tried as followed:
self._dynamic_ax.set_ylim(self._dynamic_ax.get_ylim()) and self._dynamic_ax.set_ylim(self._dynamic_ax2.get_ylim())
As well as:
self._dynamic_ax.set_navigate(True)
However these didn't work. How can I persist the settings set by NavigationToolbar? Not only the zoom but also the pan.
A minimal runnable code sample:
import sys
from matplotlib.backends.qt_compat import QtCore, QtWidgets, QtGui
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
buffer_size = 120
t = [t for t in range(buffer_size)]
bitthrough = [t for t in range(buffer_size)]
errors = bitthrough[::-1]
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PCANbus sniffer")
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.setMinimumSize(QtCore.QSize(640, 400))
self.show()
class MyTableWidget(QtWidgets.QWidget):
def __init__(self, parent):
super(QtWidgets.QWidget, self).__init__(parent)
self.layout = QtWidgets.QVBoxLayout(self)
self.tabs = QtWidgets.QTabWidget()
self.tab_graph = QtWidgets.QWidget()
self.tab_info = QtWidgets.QWidget()
self.tabs.addTab(self.tab_graph, "PCANbus occupation")
self.tabs.addTab(self.tab_info, "PCANbus information")
self.tab_graph.layout = QtWidgets.QVBoxLayout(self)
self.dynamic_canvas = FigureCanvas(Figure(figsize=(6, 4)))
self.tab_graph.layout.addWidget(self.dynamic_canvas)
self.toolbar = NavigationToolbar(self.dynamic_canvas, self)
self.tab_graph.layout.addWidget(self.toolbar)
self._dynamic_ax = self.dynamic_canvas.figure.subplots()
self._dynamic_ax.set_xlabel("time (s)")
self._dynamic_ax.set_xlim(-5, 125)
self._dynamic_ax.set_ylabel("Throughput (%)", color="black")
self._dynamic_ax.set_ylim(-5, 120)
self._dynamic_ax.tick_params(axis="y", labelcolor="black")
# self._dynamic_ax.set_navigate(True)
self._dynamic_ax2 = self._dynamic_ax.twinx()
self._dynamic_ax2.set_ylabel("Errors (%)", color="blue")
self._dynamic_ax2.set_ylim(-4, 100)
self._dynamic_ax2.tick_params(axis="y", labelcolor="blue")
#self._dynamic_ax2.set_navigate(True)
self.tab_graph.setLayout(self.tab_graph.layout)
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self._update_canvas)
self.timer.start(1000)
def _update_canvas(self):
self._dynamic_ax.clear()
self._dynamic_ax.plot(t, bitthrough, color="black")
self._dynamic_ax.set_xlabel("time (s)")
self._dynamic_ax.set_ylabel("Throughput (%)", color="black")
self._dynamic_ax.set_ylim(-5, 120) #self._dynamic_ax.get_ylim())
self._dynamic_ax.tick_params(axis="y", labelcolor="black")
self._dynamic_ax2.clear()
self._dynamic_ax2.plot(t, errors, color="blue")
self._dynamic_ax2.set_ylabel("Errors", color="blue")
self._dynamic_ax2.set_ylim(-4, 100) #self._dynamic_ax2.get_ylim())
self._dynamic_ax2.tick_params(axis="y", labelcolor="blue")
self._dynamic_ax.figure.canvas.draw_idle()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = App()
app.show()
qapp.exec_()
My real _update_canvas function:
def _update_canvas(self):
wh_green = [a <= b for a, b in zip(bitthrough, llvl)]
wh_orange = [a > b and a <= c
for a, b, c in zip(bitthrough, llvl, lvl)]
wh_red = [a > b for a, b, in zip(bitthrough, lvl)]
# self._dynamic_ax.clear()
# self._dynamic_ax2.clear()
self._dynamic_ax.fill_between(
t, 0, bitthrough, where=wh_red, color="red", interpolate=True
)
self._dynamic_ax.fill_between(
t, 0, bitthrough, where=wh_orange, color="orange", interpolate=True
)
self._dynamic_ax.fill_between(
t, 0, bitthrough, where=wh_green, color="green", interpolate=True
)
# self._dynamic_ax.plot(t, bitthrough, color="black")
# self._dynamic_ax.set_xlabel("time (s)")
# self._dynamic_ax.set_ylabel("Throughput (%)", color="black")
# #self._dynamic_ax.set_ylim(self._dynamic_ax.get_ylim())
# self._dynamic_ax.tick_params(axis="y", labelcolor="black")
# self._dynamic_ax2.plot(t, errors, color="blue")
# self._dynamic_ax2.set_ylabel("Errors", color="blue")
# #self._dynamic_ax2.set_ylim(self._dynamic_ax2.get_ylim())
# self._dynamic_ax2.tick_params(axis="y", labelcolor="blue")
self._plot1.set_ydata(bitthrough)
self._plot2.set_ydata(errors)
# logging.debug("redrawing graph!!")
self._dynamic_ax.figure.canvas.draw_idle()
The solution of #DizietAsahi doesn't work while using fill_between. The area gets overwritten and not cleared. So they are displayed on top of eachother.
My advice would be to not clear the figure at each update. Instead, store a reference to the Line2D artists created by plot() and update the {x|y}data (using set_data() or set_ydata()) in your update function.
import sys
from matplotlib.backends.qt_compat import QtCore, QtWidgets, QtGui
from matplotlib.backends.backend_qt5agg import (FigureCanvas, NavigationToolbar2QT as NavigationToolbar)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
buffer_size = 120
t = np.linspace(0, 100, buffer_size)
bitthrough = 120*np.random.random(size=(buffer_size,))
errors = bitthrough[::-1]
class App(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("PCANbus sniffer")
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.setMinimumSize(QtCore.QSize(640, 400))
self.show()
class MyTableWidget(QtWidgets.QWidget):
def __init__(self, parent):
super(QtWidgets.QWidget, self).__init__(parent)
self.layout = QtWidgets.QVBoxLayout(self)
self.tabs = QtWidgets.QTabWidget()
self.tab_graph = QtWidgets.QWidget()
self.tab_info = QtWidgets.QWidget()
self.tabs.addTab(self.tab_graph, "PCANbus occupation")
self.tabs.addTab(self.tab_info, "PCANbus information")
self.tab_graph.layout = QtWidgets.QVBoxLayout(self)
self.dynamic_canvas = FigureCanvas(Figure(figsize=(6, 4)))
self.tab_graph.layout.addWidget(self.dynamic_canvas)
self.toolbar = NavigationToolbar(self.dynamic_canvas, self)
self.tab_graph.layout.addWidget(self.toolbar)
self._dynamic_ax = self.dynamic_canvas.figure.subplots()
self._dynamic_ax.set_xlabel("time (s)")
self._dynamic_ax.set_xlim(-5, 125)
self._dynamic_ax.set_ylabel("Throughput (%)", color="black")
self._dynamic_ax.set_ylim(-5, 120)
self._dynamic_ax.tick_params(axis="y", labelcolor="black")
# self._dynamic_ax.set_navigate(True)
self._dynamic_ax2 = self._dynamic_ax.twinx()
self._dynamic_ax2.set_ylabel("Errors (%)", color="blue")
self._dynamic_ax2.set_ylim(-4, 100)
self._dynamic_ax2.tick_params(axis="y", labelcolor="blue")
#self._dynamic_ax2.set_navigate(True)
##
## Create plots here (initially empty)
##
self._plot1, = self._dynamic_ax.plot(t, np.empty(shape=(buffer_size,)), color="black")
self._plot2, = self._dynamic_ax2.plot(t, np.empty(shape=(buffer_size,)), color="blue")
self._fill1 = self._dynamic_ax.fill_between(t, 0, bitthrough, color="orange")
self.tab_graph.setLayout(self.tab_graph.layout)
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self._update_canvas)
self.timer.start(1000)
def _update_canvas(self):
bitthrough = 120*np.random.random(size=(buffer_size, ))
errors = bitthrough[::-1]
##
## update the content of the plots here, without clearing the figure
##
self._plot1.set_ydata(bitthrough)
self._plot2.set_ydata(errors)
self._fill1.remove()
self._fill1 = self._dynamic_ax.fill_between(t, 0, bitthrough, color="orange")
self._dynamic_ax.figure.canvas.draw_idle()
if __name__ == "__main__":
qapp = QtWidgets.QApplication(sys.argv)
app = App()
app.show()
qapp.exec_()
EDIT
I've added some code for handling fill_between().
fill_between() returns a PolyCollection which is a pain to update, so the best option there is to remove the PolyCollection and re-create it at each update (but not clear the whole figure).

Add/Delete plots independently on a Matplotlib figure

I want to generate a scatterplot (up to half a million points) and on top of that, add different statistics (e.g. Q1, median, Q3). The idea is to add/delete those statistics without replotting the scatterplot in order to speed up the process. So far I can add plots independently on the figure but I can't delete a specific plot.
When I uncheck the checkbox, I get the following error:
AttributeError: 'Graphics' object has no attribute 'vline1'
I understand that when I create the plot, I need to store/return the plot in order to call it later when I want to delete it but I don't know how to do that.
Here my current code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
class Mainwindow(QMainWindow):
def __init__(self, parent=None):
super(Mainwindow, self).__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.gridLayout = QGridLayout(centralWidget)
self.gridLayout.addWidget(self.canvas)
self.btn_plot = QCheckBox("Plot")
self.btn_line = QCheckBox("Line")
self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
self.btn_plot.clicked.connect(self.btnPlot)
self.btn_line.clicked.connect(self.btnLine)
def btnPlot(self):
self.checked = self.btn_plot.isChecked()
self.Graphics = Graphics('plot', self.checked, self.axes)
def btnLine(self):
self.checked = self.btn_line.isChecked()
self.Graphics = Graphics('line', self.checked, self.axes)
class Graphics:
def __init__(self, typeGraph, checked, axes):
self.typeGraph = typeGraph
self.checked = checked
self.axes = axes
if self.typeGraph == 'plot': self.drawPlot()
if self.typeGraph == 'line': self.drawLine()
def drawPlot(self):
if self.checked == True:
self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
else:
self.plot.remove()
self.axes.figure.canvas.draw()
def drawLine(self):
if self.checked == True:
self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
else:
self.vline1.remove()
self.vline2.remove()
self.axes.figure.canvas.draw()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
prog = Mainwindow()
prog.show()
sys.exit(app.exec_())
Problem is be because when you click it then it creates always new Graphics (in btnPlot/btnLine) which doesn't have previous values - plot, vline1, vline2. You have to create Graphics only once and later run only drawPlot(checked), drawLine(checked) to add or remove item.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.pyplot import Figure
class Mainwindow(QMainWindow):
def __init__(self, parent=None):
super(Mainwindow, self).__init__(parent)
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
self.canvas = FigureCanvas(self.fig)
self.gridLayout = QGridLayout(centralWidget)
self.gridLayout.addWidget(self.canvas)
self.btn_plot = QCheckBox("Plot")
self.btn_line = QCheckBox("Line")
self.gridLayout.addWidget(self.btn_plot, 1,0,1,1)
self.gridLayout.addWidget(self.btn_line, 2,0,1,1)
self.btn_plot.clicked.connect(self.btnPlot)
self.btn_line.clicked.connect(self.btnLine)
# create only once
self.Graphics = Graphics(self.axes)
def btnPlot(self):
# add or remove
self.Graphics.drawPlot(self.btn_plot.isChecked())
def btnLine(self):
# add or remove
self.Graphics.drawLine(self.btn_line.isChecked())
class Graphics:
def __init__(self, axes):
self.axes = axes
# create at start with default values (but frankly, now I don't need it)
self.plot = None
self.vline1 = None
self.vline2 = None
def drawPlot(self, checked):
if checked:
self.plot = self.axes.plot([10,20,30], [5,10,2], 'o')
else:
for item in self.plot:
item.remove()
self.axes.figure.canvas.draw()
def drawLine(self, checked):
if checked:
self.vline1 = self.axes.axvline(x=15, linestyle="dashed", color="#595959")
self.vline2 = self.axes.axvline(x=25, linestyle="dashed", color="#595959")
else:
self.vline1.remove()
self.vline2.remove()
self.axes.figure.canvas.draw()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
prog = Mainwindow()
prog.show()
sys.exit(app.exec())

How to make a line move with mouse inside a plot in a PyQt application

I want to show a given plot inside a PyQt application and show a line that moves along with the mouse cursor. However, the line shows up on the left of the plot and doesn't move at all when moving the cursor. I don't know why this is happening. Here's the code:
import numpy as np
from matplotlib.backends.qt_compat import QtWidgets
from matplotlib.backends.backend_qt5agg import FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
class SnaptoCursor(object):
def __init__(self, ax, x):
self.ax = ax
self.ly = ax.axvline(color='k')
self.x = x
self.txt = ax.text(0.7, 0.9, '', transform=ax.transAxes)
def mouse_move(self, event):
if event.inaxes:
indx = np.searchsorted(self.x, [event.xdata])[0]
x = self.x[indx]
self.ly.set_xdata(x)
self.txt.set_text('x=%1.2f' % x)
self.ax.figure.canvas.draw()
else:
pass
class App(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(App, self).__init__(parent)
self._main = QtWidgets.QWidget()
self.setCentralWidget(self._main)
self.figure = Figure(figsize=(10, 6.9))
self.canvas = FigureCanvas(self.figure)
self.canvas_ax = self.canvas.figure.subplots()
x = np.arange(0,40)
self.canvas_ax.plot(x, np.random.rand(40))
# Layout
layout = QtWidgets.QVBoxLayout(self._main)
layout.addWidget(self.canvas)
self.showMaximized()
##############################################
########## This doesnt seem to work ##########
##############################################
cursor = SnaptoCursor(self.canvas_ax, x)
plt.connect('motion_notify_event', cursor.mouse_move)
##############################################
##############################################
##############################################
if __name__ == '__main__':
app = QtWidgets.QApplication([])
ex = App()
ex.show()
app.exec_()
The two lines in question should be
self.cursor = SnaptoCursor(self.canvas_ax, x)
self.cid = self.canvas.mpl_connect('motion_notify_event', self.cursor.mouse_move)
because else the cursor as well as the callback registration do not exist in memory at the point you would expect them to be used. And also you shouldn't be using pyplot/plt when your figure isn't even created with pyplot.

How to send variables to a def through an event and return the variable

How do you send variables through events? I want to be able to return the x,y coordinates of a scatterplot datapoint that was clicked. Currently the program is able to perform the click event but I cant pull out the x, y data to use later on in the program. I added a Coordinate button as an example of using the data afterwards. Thanks in advance.
import sys
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
def onclick(event):
ind = event.ind[0]
data = event.artist.get_offsets()
xdata, ydata = data[ind,:]
print ((xdata, ydata))
return (xdata,ydata)# i want to return these
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
# a figure instance to plot on
self.figure = plt.figure()
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)
# Just some button connected to `plot` method
self.button = QPushButton('Plot')
self.button.clicked.connect(self.plot)
self.button2 = QPushButton('Coordinate')
self.button2.clicked.connect(self.coordinate)
# set the layout
layout = QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
layout.addWidget(self.button2)
self.setLayout(layout)
self.canvas.mpl_connect('pick_event', onclick)
def coordinate(self):
print(xdata, ydata)#these variables are not being returned
def plot(self):
x = [10,20,30,40,50]
y = [100,50,150,200,75]
size = 100
# instead of ax.hold(False)
self.figure.clear()
# create an axis
self.axes = self.figure.add_subplot(111)
# discards the old graph
self.axes.scatter(x,y,s=size,color='blue', picker=1)
self.axes.patch.set_facecolor('None')
# refresh canvas
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())

matplotlib and Qt: mouse scroll event key is always None

This has been fixed for now!
I am trying to make a GUI using PyQt4 with an embedded matplotlib canvas. When I scroll with the cursor over the canvas I would like to be able to control the behaviour based on an additional key press (control in this case). However, the key attribute of the mouseEvent linked to the 'scroll_event' is always None. I tested that my code correctly registers the key for a mouseEvent generated by 'button_press_event'.
In the example below the on_press method correctly prints the key that was pressed at the same time. Whereas on_scoll always prints None.
How can I get access the key that was pressed during the mouse scroll event?
Thanks in advance!
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import matplotlib
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
class GraphicTool(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.create_menu()
def on_press(self, event):
print event.key
def on_scroll(self, event):
print event.key
if event.key == 'ctrl':
# do something
pass
else:
# do something else
pass
def create_main_frame(self):
self.main_frame = QWidget()
# Create the mpl Figure and FigCanvas objects.
# 10x8 inches, 100 dots-per-inch
self.dpi = 100
self.fig = Figure((10.0, 8.0), dpi=self.dpi)
self.canvas = FigureCanvas(self.fig)
self.canvas.setParent(self.main_frame)
self.canvas.setFocusPolicy(Qt.ClickFocus)
self.canvas.setFocus()
self.axes = self.fig.add_subplot(111)
self.canvas.mpl_connect('scroll_event', self.on_scroll)
self.canvas.mpl_connect('button_press_event', self.on_press)
# Create the navigation toolbar, tied to the canvas
self.mpl_toolbar = NavigationToolbar(self.canvas,
self.main_frame)
vbox = QVBoxLayout()
vbox.addWidget(self.canvas)
vbox.addWidget(self.mpl_toolbar)
self.main_frame.setLayout(vbox)
self.setCentralWidget(self.main_frame)
def main():
app = QApplication(sys.argv)
viewer = GraphicTool()
viewer.show()
viewer.raise_()
app.exec_()
if __name__ == "__main__":
main()
The short example above actually does work as expected. However, when I incorporate it into a bigger project it fails. I will keep debugging to see if any other events are disturbing the scroll event.
I have checked your code. This is working as expected.
def on_scroll(self, event):
print event.xdata
print event.ydata
print event.key
if event.key == 'ctrl':
# do something
pass
else:
# do something else
pass
This is the output of your code. I have just added xdata and ydata to your code.
0.480977867785
0.57896567718
control
0.480977867785
0.57896567718
shift
0.480977867785
0.57896567718
shift

Categories

Resources