error can't move rectangl patch with mouse

hi,

i try to move rectangel patch with mouse all time when i move mouse the rectangle can’t move with and i have this error :

File “/home//nct.py”, line 16, in on_press
ax.lines =
NameError: name ‘ax’ is not defined

that my code with resulta but i need to know how can move rectangle with mouse i don’t know how fix problem

import numpy as np
import netCDF4
from netCDF4 import Dataset
import matplotlib.pyplot as plt
import matplotlib.patches as patches

#lecture du netcdf

def on_press(event):
xpress, ypress = event.xdata, event.ydata
w = rect.get_width()
h = rect.get_height()
rect.set_xy((xpress-w/2, ypress-h/2))

ax.lines = []
ax.axvline(xpress, c='b')
ax.axhline(ypress, c='b')

fig.canvas.draw()

fic=‘Mmultic3kmNC4_msg04_201905080000.nc’

path=‘/net/glaz/export/data2/Majda/data/’

nc = netCDF4.Dataset(path+fic,‘r’)
IR=nc.variables[‘IR_108’][:]
x = y = 500

Display the image

fig = plt.figure()
axes = plt.subplot(111)
axes.imshow(IR,origin=‘lower’, cmap=plt.cm.gist_yarg)
fig.canvas.mpl_connect(‘button_press_event’,on_press)

rect = patches.Rectangle((x,y),800,800,linewidth=1,edgecolor=‘g’,facecolor=‘none’)

Add the patch to the Axes

axes.add_patch(rect)

plt.show()

and that what i get :

thank in advance

The error is exactly correct, ax is not defined. What did you think “ax” was supposed to be? axvline and axhline are both part of matplotlib.pyplot, so you want

plt.axvline(xpress, c=‘b’)

plt.axhline(ypress, c=‘b’)

but I can’t guess what the “ax.lines = ” is supposed to do. Did you copy this code from somewhere else?

···

On Jun 13, 2019, at 2:34 AM, Maj majdaelyacouby1990@gmail.com wrote:

i try to move rectangel patch with mouse all time when i move mouse the rectangle can’t move with and i have this error :

File “/home//nct.py”, line 16, in on_press
ax.lines =
NameError: name ‘ax’ is not defined

that my code with resulta but i need to know how can move rectangle with mouse i don’t know how fix problem

Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

yes i see the example here : https://stackoverflow.com/questions/56271104/how-can-move-rectangle-patch-with-click-mouse-python3

and i try to do the same thing with plot

i forget to add example in my problem

Le ven. 14 juin 2019 à 09:05, Tim Roberts timr@probo.com a écrit :

···

On Jun 13, 2019, at 2:34 AM, Maj majdaelyacouby1990@gmail.com wrote:

i try to move rectangel patch with mouse all time when i move mouse the rectangle can’t move with and i have this error :

File “/home//nct.py”, line 16, in on_press
ax.lines =
NameError: name ‘ax’ is not defined

that my code with resulta but i need to know how can move rectangle with mouse i don’t know how fix problem

The error is exactly correct, ax is not defined. What did you think “ax” was supposed to be? axvline and axhline are both part of matplotlib.pyplot, so you want

plt.axvline(xpress, c=‘b’)

plt.axhline(ypress, c=‘b’)

but I can’t guess what the “ax.lines = ” is supposed to do. Did you copy this code from somewhere else?

Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/2409D9C7-6790-48C0-9CEE-02AD64FDD070%40probo.com.

For more options, visit https://groups.google.com/d/optout.

Majda El yacouby wrote:

yes i see the example here : python 3.x - how can move rectangle patch with click mouse python3 - Stack Overflow

and i try to do the same thing with plot

Can you really not see the difference between that example and the code you wrote? I could tell you how to fix it, but I really think you need to be able to spot the problem yourself.

Look at the original code. In their on_press event, they refer to two global variables: ax, and fig. Those globals are created by the code later on in the file. In your code, you create "fig", but you decided not to create "ax". You used a different name. That's the problem.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.