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

11

我想把markersize设置为高度单位为1。看起来markersize是以像素为单位的。我怎样才能知道在像素中“1个单位”(沿给定轴线)有多大?


你可能会发现这个链接有用,但不是重复的:https://dev59.com/PlbUa4cB1Zd3GeqPCt4o。 - Alex
1个回答

13
请查看转换教程,特别是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点=1/72英寸),您可以尝试使用matplotlib.transforms.ScaledTransformfig.dpi_scale_trans(教程上可能有相关内容)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接