[英]How can I get the length of a single unit on an axis in matplotlib, in pixels?
看看轉換教程 (哇,需要大量挖掘才能找到 - !)
特別是, axes.transData.transform(points)
返回像素坐標,其中(0,0)是視口的左下角。
import matplotlib.pyplot as plt
# set up a figure
fig = plt.figure()
ax = fig.add_subplot(111)
x = np.arange(0, 10, 0.005)
y = np.exp(-x/2.) * np.sin(2*np.pi*x)
ax.plot(x,y)
# what's one vertical unit & one horizontal unit in pixels?
ax.transData.transform([(0,1),(1,0)])-ax.transData.transform((0,0))
# Returns:
# array([[ 0., 384.], <-- one y unit is 384 pixels (on my computer)
# [ 496., 0.]]) <-- one x unit is 496 pixels.
您還可以進行各種其他變換 - 相對於數據的坐標,相對於軸的坐標,圖形的比例,或圖中的像素(變換教程非常好)。
要在像素和點之間進行轉換(一個點是1/72英寸),您可以使用matplotlib.transforms.ScaledTransform
和fig.dpi_scale_trans
(我認為該教程有相關內容)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.