[英]Pixel coordinates of matplotlib scatter plot
我正在參考這篇文章並實施解決方案,但是我得到了非常大的值。 感謝您的幫助,附上代碼。
import numpy as np
import matplotlib.pyplot as plt
x_labels = ['x1','x2','x3']
y_values = [30,40,50]
coordList = []
x_vals = []
i = 0
fig, ax = plt.subplots()
for item in x_labels:
x_vals.append(i)
i+=1
points, = ax.plot(x_vals, y_values)
x, y = points.get_data()
print(x, y)
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T
for xp, yp in zip(xpix, ypix):
coordList.append(f'{xp}, {yp}')
print(coordList)
這是一個結果 coordList:
['80 0.0,39969.6' , '576.0,37382.4', '1072.0,34425.6', '1568.0,31838.399999999998', '2064.0,29620.799999999996', '2560.0,26663.999999999996', '3056.0,24815.999999999996', '3552.0,21859.199999999997',' 4048.0, 19271.999999999996']
您看到的是軸邊界的內部自動設置之前的原始轉換。 為了強制更新轉換,您需要通過例如get_xbounds()
獲取邊界或首先通過調用fig.canvas.draw()
完全更新圖形(在鏈接示例中,更新由ax.axis([-1, 10, -1, 10])
確保ax.axis([-1, 10, -1, 10])
)。
ax.get_xbound()
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
結果(對於我的顯示):
[0 1 2] [30 40 50]
['102.54545454545455, 69.59999999999997', '328.0, 237.59999999999997', '553.4545454545454, 405.59999999999997']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.