Matplotlib 图例高度(以像素为单位)
我需要知道图例的大小(以像素为单位)。我似乎只能从任何函数获得 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为您还没有绘制画布。
在绘制画布之前,像素值根本不存在于 matplotlib 中(或者更确切地说,它们存在,与屏幕或其他输出无关)。
造成这种情况的原因有很多,但我现在会跳过它们。可以说,matplotlib 尝试尽可能保持通用性,并且通常会避免使用像素值,直到绘制出内容。
举一个简单的例子:
但是,这仅代表图例在屏幕上绘制时的高度(以像素为单位)!如果保存图形,它将以与在屏幕上绘制的不同的 dpi(默认情况下为 100)保存,因此以像素为单位的事物的大小将会不同。
有两种方法可以解决此问题:
快速而肮脏:在输出像素值之前绘制图形的画布,并确保在保存时显式指定图形的 dpi(例如
fig.savefig('temp.png', dpi=fig.dpi)
。推荐,但稍微复杂一些:将回调连接到绘制事件,并且仅在绘制图形时使用像素值。 推荐,但稍微复杂一点
作为后一种方法的简单示例:
请注意第一个和第二个示例的图例高度打印内容的不同 (在我的系统上,第二个为 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:
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:
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)
.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:
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.