[英]How do you get the size of a matplotlib plot area in inches?
I am making figures for a paper, and I want to put a label some fixed distance from the top-left corner of the plot.我正在为论文制作图形,我想在距图左上角一定距离处放置一个标签。 ax.text(x, y, label, transform=ax.transAxes)
nearly does this, but specifies the position as a fraction of the size of the plot. ax.text(x, y, label, transform=ax.transAxes)
几乎可以做到这一点,但将位置指定为绘图大小的一部分。 If I could get the absolute size of the plot area I could use that to convert.如果我可以获得绘图区域的绝对大小,我可以使用它进行转换。 For example, in the following script how could I get the height and width?例如,在以下脚本中,我如何获得高度和宽度?
Edit: I want the height and width just of the plot (not of the whole figure), and excluding the labels, ticks, etc.编辑:我只想要情节的高度和宽度(而不是整个图形),不包括标签、刻度等。
from matplotlib import pyplot as plt
import numpy as np
data = np.random.rand(10,10)
fig, ax = plt.subplots()
ax.pcolormesh(data)
ax.set_aspect("equal")
# get width and height of plot here?
plt.show()
Your response above is good, but perhaps better:你上面的回答很好,但也许更好:
import matplotlib.pyplot as plt
import matplotlib.transforms as mtransforms
fig, ax = plt.subplots()
trans = (fig.dpi_scale_trans +
mtransforms.ScaledTranslation(0, 1, ax.transAxes))
ax.set_aspect(1)
ax.text(0.1, -0.2, 'Boo', transform=trans)
plt.show()
You can read more at: https://matplotlib.org/tutorials/advanced/transforms_tutorial.html#plotting-in-physical-coordinates您可以在以下位置阅读更多信息: https : //matplotlib.org/tutorials/advanced/transforms_tutorial.html#plotting-in-physical-coordinates
I did manage to find a way to do this.我确实设法找到了一种方法来做到这一点。 It was made more complicated because I used set_aspect()
- set_aspect()
modifies the bounding box of ax
, but by default doesn't apply the modification until ax
is drawn (eg by plt.show()
), which messes up attempts to get the bounding box size.它变得更加复杂,因为我使用了set_aspect()
- set_aspect()
修改了ax
的边界框,但默认情况下在绘制ax
之前不会应用修改(例如通过plt.show()
),这会plt.show()
尝试获取边界框大小。
The solution is:解决办法是:
To avoid problems from set_aspect()
, call apply_aspect()
before trying to get the bounding box size.为避免set_aspect()
出现问题,请在尝试获取边界框大小之前调用apply_aspect()
。 This makes the updated aspect ratio actually modify the bounding box size so we can find out what it is.这使得更新的纵横比实际上修改了边界框大小,因此我们可以找出它是什么。
Get the size of the plot area with ax.get_window_extent()
- this gets the size of just the plot area, excluding axis labels, ticks, etc.使用ax.get_window_extent()
获取绘图区域的大小 - 这将获取绘图区域的大小,不包括轴标签、刻度等。
The result of ax.get_window_extent()
is in 'display units', which we can convert to inches using fig.dpi
. ax.get_window_extent()
的结果是“显示单位”,我们可以使用fig.dpi
将其转换为英寸。
So adding the kind of label I wanted as an example:所以添加我想要的那种标签作为例子:
from matplotlib import pyplot as plt
import numpy as np
data = np.random.rand(10,10)
fig, ax = plt.subplots()
ax.pcolormesh(data)
ax.set_aspect("equal")
ax.apply_aspect()
bbox = ax.get_window_extent()
# dpi used to convert from display units to inches
dpi = fig.dpi
height = bbox.height / dpi # in inches
width = bbox.width / dpi # in inches
x = 0.2 # in inches
y = 0.1 # in inches
ax.text(x / width, 1.0 - y / height, "(a)", verticalalignment="top", color="w", transform=ax.transAxes)
plt.show()
which gives:这使:
Edit: I tested this solution with matplotlib-3.3.2.编辑:我用 matplotlib-3.3.2 测试了这个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.