簡體   English   中英

Matplotlib:3D trisurf圖中的ax.format_coord()-返回(x,y,z)而不是(方位角,高程)?

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

我正在嘗試重做這個已經回答的問題Matplotlib-plot_surface:獲取x,y,z值寫在右下角 ,但不能得到相同的結果,如此處所述。 因此,我有一個類似的代碼:

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()

這樣,可以正確顯示三角形表面(盡管很慢)。 將鼠標懸停在繪圖上時,可以在右下角看到曲面的(x,y,z)坐標。 但是,當我嘗試通過單擊鼠標(通過連接的事件處理程序)獲得這些坐標時,ax.format_coord(x,y)函數返回的不是笛卡爾坐標的字符串,而是“方位角= -60度”的字符串,標高= 30deg”,無論我在圖中單擊何處,直到旋轉曲面為止。 然后,它返回另一個值。 出於某種原因,我認為這些是當前視圖的球面坐標,而不是單擊的點...

有人可以找出我在做什么錯嗎? 如何獲得表面上的笛卡爾坐標?

僅供參考:這一切都與我之前的問題Python:3D中的圖形輸入有關 ,這被認為過於籠統。

被壓的mousebutton是觸發ax.format_coord返回的3D繪圖,而不是那些笛卡爾的角坐標。 因此,一種選擇是讓ax.format_coord認為沒有按下任何按鈕,在這種情況下,它將按需要返回通常的笛卡爾x,y,z坐標。

即使您單擊了鼠標按鈕,也可以通過一些小ax.button_pressed來將ax.button_pressed (存儲當前的鼠標按鈕)設置為在調用該函數時不合理的方式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM