I am trying to get the pixel coordinates from each datapoint after I have converted the chart to a horizontal position. Attached is what I am using to get vertical coordinate data:
import numpy as np
import matplotlib.pyplot as plt
def pixelCoords(x_labels, y_values):
coordList = []
x_vals = []
i = 1
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()
ax.get_xbound()
xy_pixels = ax.transData.transform(np.vstack([x,y]).T)
xpix, ypix = xy_pixels.T
for xp, yp in zip(xpix, ypix):
coordList.append((round(xp, 2), round(yp, 2)))
plt.clf()
plt.close()
return coordList
x_labels = ['x1','x2','x3']
y_values = [30,40,50]
coordValues = pixelCoords(x_labels, y_values)
However I cannot seem to figure out how to get the coordinates for horizontal, I assume there is probably a simple formula to go from [xV, yV] to [xH, yH] but I cannot figure it out. Thanks!
I think this simple formula solves it correctly, if anyone sees an error please tell me, thanks! I simply use the same function but pass coordValues through the following loop:
for item in coordValues:
item[0] = round(((item[1]/480) * 640), 2)
item[1] = round((480 - ((item[0]/640) * 480)), 2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.