简体   繁体   English

你如何获得以英寸为单位的 matplotlib 绘图区域的大小?

[英]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:解决办法是:

  1. 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.这使得更新的纵横比实际上修改了边界框大小,因此我们可以找出它是什么。

  2. 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()获取绘图区域的大小 - 这将获取绘图区域的大小,不包括轴标签、刻度等。

  3. 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.

相关问题 在 matplotlib 中使用 bbox_inches='tight' 时,如何获得以像素为单位的精确大小的图像? - How do I get an image w/ the exact size in pixels, when using bbox_inches='tight' in matplotlib? 如何“缩小” matplotlib 中的 plot,保持所有尺寸比例相同,但以英寸为单位减小尺寸? - How to "zoom out" a plot in matplotlib, keeping all the size ratios the same, but reducing the size in inches? Python / PyQt4:如何找到显示器的尺寸(以英寸为单位)? - Python/PyQt4: How do you find the SIZE of a monitor (in inches)? 如何使用matplotlib定义图中子图的尺寸(以英寸为单位)? - How you can define the dimensions of a subplot (in inches) within a figure with matplotlib? 在 matplotlib 中获取 Axes 绘图区域的真正限制? - Get true limit of plot area of an Axes in matplotlib? 如何从matplotlib图中删除点? - How do you remove a point from matplotlib plot? 您如何将 plot 月份和年份数据转换为 matplotlib 中的条形图? - How do you plot month and year data to bar chart in matplotlib? 你如何在 matplotlib 的散点图中偏移文本? - How do you offset text in a scatter plot in matplotlib? 如何在matplotlib中设置图的外部bg颜色 - How do you set the outer bg colour of a plot in matplotlib 如何使用 Matplotlib 中的字典绘制标记数据? (Python) - How do you plot labelled data by using a dictionary in Matplotlib? (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM