簡體   English   中英

如何在matplotlib中獲取軸上單個單元的長度(以像素為單位)?

[英]How can I get the length of a single unit on an axis in matplotlib, in pixels?

我想使markersize大小等於單個單位的高度。 看來, markersize以像素為單位。 如何獲得“1個單位”(沿給定軸)的大小(以像素為單位)?

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

特別是, 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.ScaledTransformfig.dpi_scale_trans (我認為該教程有相關內容)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM