37289

Matplotlib: ax.format_coord() in 3D trisurf plot - return (x,y,z) instead of (azimuth, elevation)?

Question:

I was trying to redo this already answered question <a href="https://stackoverflow.com/questions/6748184/matplotlib-plot-surface-get-the-x-y-z-values-written-in-the-bottom-right-cor?rq=1" rel="nofollow">Matplotlib - plot_surface : get the x,y,z values written in the bottom right corner</a>, but wasn't capable of getting the same result, as stated there. So, I have a code like:

import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from plyfile import PlyData, PlyElement #Handle the "onclick" event def onclick(event): print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % ('double' if event.dblclick else 'single', event.button, event.x, event.y, event.xdata, event.ydata)) print(gety(event.xdata, event.ydata)) #copied from https://stackoverflow.com/questions/6748184/matplotlib-plot-surface-get-the-x-y-z-values-written-in-the-bottom-right-cor?rq=1 def gety(x,y): s = ax.format_coord(x,y) print(s) #here it prints "azimuth=-60 deg, elevation=30deg" out = "" for i in range(s.find('y')+2,s.find('z')-2): out = out+s[i] return float(out) #Read a PLY file and prepare it for display plydata = PlyData.read("some.ply") mesh = plydata.elements[0] triangles_as_tuples = [(x[0], x[1], x[2]) for x in plydata['face'].data['vertex_indices']] polymesh = np.array(triangles_as_tuples) #Display the loaded triangular mesh in 3D plot fig = plt.figure() ax = fig.gca(projection='3d') ax.plot_trisurf(mesh.data['x'], mesh.data['y'], mesh.data['z'], triangles=polymesh, linewidth=0.2, antialiased=False) fig.canvas.mpl_connect('button_press_event', onclick) plt.show()

With this, the triangular surface is properly displayed (although slowly). I can see the (x,y,z) coordinates of the surface in the bottom right corner, while I hover over the plot. But when I try to get these coordinates with the click of mouse (through the connected event handler), the ax.format_coord(x,y) fction returns not a string of cartesian coordinates, but a string of "azimuth=-60 deg, elevation=30deg", no matter where in the plot I click, until the surface is rotated. Then it returns another values. From this I suppose these are spherical coordinates of the current view, not the clicked point, from some reason...

Can someone find out, what am I doing wrong? How do I can get the cartesian coordinates on the surface?

FYI: This all is related to my previous question <a href="https://stackoverflow.com/questions/49883082/python-graphic-input-in-3d?noredirect=1#comment87581990_49883082" rel="nofollow">Python: Graphic input in 3D</a>, which was considered too broad and generic.

Answer1:

The mousebutton being pressed is the trigger for ax.format_coord to return the angular coordinates on a 3D plot instead of the cartesian ones. So an option is would be to let the ax.format_coord think that no button is pressed, in which case it would return the usual cartesian x,y,z coordinates as desired.

A bit of a hacky way to achieve this, even though you clicked the mousebutton, would be to set the ax.button_pressed (which stores the current mousebutton) to something unreasonable while calling that function.

def gety(x,y): # store the current mousebutton b = ax.button_pressed # set current mousebutton to something unreasonable ax.button_pressed = -1 # get the coordinate string out s = ax.format_coord(x,y) # set the mousebutton back to its previous state ax.button_pressed = b return s

Recommend

  • Adding color to Polar Scatter plot data points
  • how to determine device orientation from the sensors
  • How can I calculate number of hops to reach destination ( NS2 )?
  • XSLT selecting mulitple nodes with different namespaces
  • Vuetify external pagination not displaying page numbers
  • FloatingActionButton visible for sometime even if visibility is set to gone
  • How to index observable coordinates in an astropy Table
  • is it possible to display 3D data in Google Maps or Google Earth?
  • Invalid rgba arg “#” in matplotlib
  • Proper Mutational scale value in matplotlib
  • Label Areas in Python Matplotlib stackplot
  • matplotlib's colormap
  • Is the Azimuth on the equator equal to the one not on the equator?
  • How to draw cross background via css which is responsive
  • PowerShell execution policy subverted by powershell.exe parameter?
  • Zipping the contents of a folders in PHP
  • Advice on how to create this button shape
  • Copy folder content with Ansible
  • Rails 4.0.0 jQuery Mobile button icons not showing in production
  • How to share a database between ASP.NET MVC 5 application and .NET console application?
  • How do I get the number of jobs in a rq queue?
  • Recursion Control flow
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • Selectively hide background elements when overlayed with transparent div
  • ImportError: cannot import name Pubnub
  • In-place sed command not working
  • Appending Character to Character Array In C
  • Excel's Macro-Recorder usage
  • Grails calculated field in SQL
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • How to add date and time under each post in guestbook in google app engine
  • Running a C# exe file
  • How to handle AllServersUnavailable Exception
  • JSON with duplicate key names losing information when parsed
  • VBA Convert delimiter text file to Excel
  • Return words with double consecutive letters
  • How to pass list parameters for each object using Spring MVC?
  • Free memory of cv::Mat loaded using FileStorage API
  • Reading document lines to the user (python)