如何在matplotlib中获取轴上单个单元的长度(以像素为单位)?

are*_*ick 10 python matlab plot matplotlib

我想markersize在高度上等于一个单位.它似乎markersize是像素.如何获得"1个单位"(沿给定轴)的大小(以像素为单位)?

mat*_*fee 10

看看转换教程(哇,需要大量挖掘才能找到 - !)

特别是,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.
Run Code Online (Sandbox Code Playgroud)

您还可以进行各种其他变换 - 相对于数据的坐标,相对于轴的坐标,图形的比例,或图中的像素(变换教程非常好).

要在像素之间进行转换(一个点是1/72英寸),你可能可以matplotlib.transforms.ScaledTransform和它一起玩fig.dpi_scale_trans(我认为教程有关于此的内容).