Skip to content

Get the z coord in event #11309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mko010 opened this issue May 25, 2018 · 16 comments
Closed

Get the z coord in event #11309

mko010 opened this issue May 25, 2018 · 16 comments
Labels
status: needs clarification Issues that need more information to resolve.

Comments

@mko010
Copy link

mko010 commented May 25, 2018

Bug report

Bug summary

Matplotlib doesn´t allow get the z coord as event.zdata
Code for reproduction

import matplotlib
matplotlib.use('TkAgg')
from tkinter import filedialog
from tkinter import *
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from nibabel.loadsave import read_img_data
from nibabel.loadsave import load as load_nii
from viewers import OrthoSlicer3D
from matplotlib.figure import Figure
import numpy as np

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("Orthoslicer3D for brain segmentation")

f = Figure()

sagital = f.add_subplot(221)
sagital.set_position([0,0,0.5,0.5])
sagital.set_axis_off()

coronal = f.add_subplot(222)
coronal.set_axis_off()
coronal.set_position([0,0.5,0.5,0.5])

axial = f.add_subplot(223)
axial.set_position([0.5,0.5,0.5,0.5])
axial.set_axis_off()

axes = (sagital, coronal, axial)


# a tk.DrawingArea
canvas = FigureCanvasTkAgg(f, master=root)
canvas.get_tk_widget().pack(side=Tk.RIGHT, fill=Tk.BOTH, expand=1)

toolbar = NavigationToolbar2TkAgg(canvas, root)
toolbar.update()
canvas._tkcanvas.pack(side=Tk.RIGHT, fill=Tk.BOTH, expand=0)

def _load():
    data = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("nii files","*.nii"),("gz files", "*.gz"),("all files","*.*")))
    data_load = load_nii(data)
    data_read = read_img_data(data_load)
    data_read = np.asanyarray(data_read)
    OrthoSlicer3D(data_read, axes=axes).show()


coords = []


def _onclick(event):
    coords.append((event.xdata, event.ydata, event.zdata))
    return coords

canvas.mpl_connect('button_press_event', _onclick)

buttonLoad = Tk.Button(master=root, text='Load', command=_load)
buttonLoad.pack(side=Tk.LEFT, expand = 1)

Tk.mainloop()

Actual outcome

TypeError: () missing 1 required positional argument: 'z'

# If applicable, paste the console output here
#
#

Expected outcome

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 2.2.2
  • Matplotlib backend (print(matplotlib.get_backend())):
  • Python version: 3.5
  • Jupyter version (if applicable):
  • Other libraries:

I have installed matplotlib from pip and python from source.

@WeatherGod
Copy link
Member

WeatherGod commented May 25, 2018 via email

@dstansby dstansby added the status: needs clarification Issues that need more information to resolve. label May 25, 2018
@mko010
Copy link
Author

mko010 commented May 26, 2018

Traceback (most recent call last):
  File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process
    proxy(*args, **kwargs)
  File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__
    return mtd(*args, **kwargs)
  File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\backend_bases.py", line 2888, in mouse_move
    s = event.inaxes.format_coord(event.xdata, event.ydata)
TypeError: <lambda>() missing 1 required positional argument: 'z'

[timhoffm edited: use blockqoute for better readability]

@WeatherGod
Copy link
Member

WeatherGod commented May 26, 2018 via email

@mko010
Copy link
Author

mko010 commented May 26, 2018

there the complete code

Traceback (most recent call last):
  File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process
    proxy(*args, **kwargs)
  File "C:\Users\migue\Documents\IM\TFG\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__
    return mtd(*args, **kwargs)
  File "C:/Users/migue/Documents/IM/TFG/pruebsd.py", line 58, in _onclick
    coords.append((event.xdata, event.ydata, event.zdata))
AttributeError: 'MouseEvent' object has no attribute 'zdata'

@ImportanceOfBeingErnest
Copy link
Member

AttributeError: 'MouseEvent' object has no attribute 'zdata'

I guess the question is why would you expect the MouseEvent to have an attribute zdata??

In the limit this could be understood as a feature request to use the 3D projection and make such attribute available, similar to how the format_coords provides a z-coordinate for use in the status bar.
Personally I'm not convinced that this is useful (similar to how the coordinates in the statusbar are not useful for 3D plots in my opinion).

@mko010
Copy link
Author

mko010 commented May 28, 2018

Well, I need the x, y and z data, from navigation toolbar for draw that points into the image, then How can I do that?

@ImportanceOfBeingErnest
Copy link
Member

One possible option is shown in this Stackoverflow question. A cleaner solution would of course simply replicate the code from format_coords.

@mko010
Copy link
Author

mko010 commented May 29, 2018

But that solution use a fig.gca, and I need use subplots, because otherwise Orthoslicer doesn't work

@ImportanceOfBeingErnest
Copy link
Member

You can replace fig.gca() by any axes object you like.

@mko010
Copy link
Author

mko010 commented May 30, 2018

data
Is not it easier to take the string in []?

@ImportanceOfBeingErnest
Copy link
Member

That [91] seems to be from a 2D plot, like imshow. A 3D matplotlib plot would show z=91. But, yes, that is exactly how the linked stackoverflow question does it.

@mko010
Copy link
Author

mko010 commented May 30, 2018

AttributeError: 'AxesSubplot' object has no attribute 'button_pressed' When apply def gety(x,y):

@ImportanceOfBeingErnest
Copy link
Member

ImportanceOfBeingErnest commented May 30, 2018

Note that the matplotlib issue tracker is not a personal help desk. If you think that there is a bug in matplotlib, a minimal runnable example is required.
Also note that one cannot give support for some third party package like Orthoslicer here.

As said, this issue can be kept open as a wishlist/feature request for a "projected" event.*data for 3D plots.

@mko010
Copy link
Author

mko010 commented May 30, 2018

I know and I'm sorry

@WeatherGod
Copy link
Member

WeatherGod commented May 30, 2018 via email

@mko010
Copy link
Author

mko010 commented May 31, 2018

True and thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs clarification Issues that need more information to resolve.
Projects
None yet
Development

No branches or pull requests

5 participants