Matplotlib 图例高度(以像素为单位)

发布于 2024-10-17 09:26:16 字数 398 浏览 4 评论 0原文

我需要知道图例的大小(以像素为单位)。我似乎只能从任何函数获得 height = 1....我尝试了以下方法

,这返回 1。

height = legend.get_frame().get_bbox_to_anchor().height

这返回 [0,0],[1.,1.]

box = legend.get_window_extent().get_points()

这也返回 [0,0 ],[1.,1.]

box = legend.get_frame().get_bbox().get_points()

所有这些都返回 1,即使图例的大小发生变化!这是怎么回事?

I need to know the size of the legend in pixels. I seem to only be able to get height = 1. from any function... I've tried the following

this returns 1.

height = legend.get_frame().get_bbox_to_anchor().height

this returns [0,0],[1.,1.]

box = legend.get_window_extent().get_points()

this also returns [0,0],[1.,1.]

box = legend.get_frame().get_bbox().get_points()

all of these return 1, even if the size of the legend changes! what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2024-10-24 09:26:16

这是因为您还没有绘制画布。

在绘制画布之前,像素值根本不存在于 matplotlib 中(或者更确切地说,它们存在,与屏幕或其他输出无关)。

造成这种情况的原因有很多,但我现在会跳过它们。可以说,matplotlib 尝试尽可能保持通用性,并且通常会避免使用像素值,直到绘制出内容。

举一个简单的例子:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), label='Test')
legend = ax.legend(loc='upper left')

print 'Height of legend before canvas is drawn:'
print legend.get_window_extent().height

fig.canvas.draw()

print 'Height of legend after canvas is drawn:'
print legend.get_window_extent().height

但是,这仅代表图例在屏幕上绘制时的高度(以像素为单位)!如果保存图形,它将以与在屏幕上绘制的不同的 dpi(默认情况下为 100)保存,因此以像素为单位的事物的大小将会不同。

有两种方法可以解决此问题:

  1. 快速而肮脏:在输出像素值之前绘制图形的画布,并确保在保存时显式指定图形的 dpi(例如 fig.savefig('temp.png', dpi=fig.dpi)

  2. 推荐,但稍微复杂一些:将回调连接到绘制事件,并且仅在绘制图形时使用像素值。 推荐,但稍微复杂一点

作为后一种方法的简单示例:

import matplotlib.pyplot as plt

def on_draw(event):
    fig = event.canvas.figure
    ax = fig.axes[0] # I'm assuming only one subplot here!!
    legend = ax.legend_ 
    print legend.get_window_extent().height

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), label='Test')
legend = ax.legend(loc='upper left')

fig.canvas.mpl_connect('draw_event', on_draw)

fig.savefig('temp.png')

请注意第一个和第二个示例的图例高度打印内容的不同 (在我的系统上,第二个为 31.0,第一个为 24.8,但这取决于 您的 .matplotlibrc 文件

差异是由于默认 fig.dpi (默认情况下为 80 dpi)和保存图形时的默认分辨率(100 dpi,默认情况下)。

无论如何,希望这是有道理的。

This is because you haven't yet drawn the canvas.

Pixel values simply don't exist in matplotlib (or rather, they exist, have no relation to the screen or other output) until the canvas is drawn.

There are a number of reasons for this, but I'll skip them at the moment. Suffice it to say that matplotlib tries to stay as general as possible, and generally avoids working with pixel values until things are drawn.

As a simple example:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), label='Test')
legend = ax.legend(loc='upper left')

print 'Height of legend before canvas is drawn:'
print legend.get_window_extent().height

fig.canvas.draw()

print 'Height of legend after canvas is drawn:'
print legend.get_window_extent().height

However, this is only going to represent the height of the legend in pixels as it is drawn on the screen! If you save the figure, it will be saved with a different dpi (100, by default) than it is drawn on the screen, so the size of things in pixels will be different.

There are two ways around this:

  1. Quick and dirty: draw the figure's canvas before outputting pixel values and be sure to explicitly specify the dpi of the figure when saving (e.g. fig.savefig('temp.png', dpi=fig.dpi).

  2. Recommended, but slightly more complicated: Connect a callback to the draw event and only work with pixel values when the figure is drawn. This allows you to work with pixel values while only drawing the figure once.

As a quick example of the latter method:

import matplotlib.pyplot as plt

def on_draw(event):
    fig = event.canvas.figure
    ax = fig.axes[0] # I'm assuming only one subplot here!!
    legend = ax.legend_ 
    print legend.get_window_extent().height

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10), label='Test')
legend = ax.legend(loc='upper left')

fig.canvas.mpl_connect('draw_event', on_draw)

fig.savefig('temp.png')

Notice the different in what is printed as the height of the legend for the first and second examples. (31.0 for the second vs. 24.8 for the first, on my system, but this will depend on the defaults in your .matplotlibrc file)

The difference is due to the different dpi between the default fig.dpi (80 dpi, by default) and the default resolution when saving a figure (100 dpi, by default).

Hopefully that makes some sense, anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文