如何删除 PGF 图的 matplotlib 图例中多余的空格

发布于 2025-01-12 22:32:19 字数 1921 浏览 3 评论 0原文

我正在使用 matplotlib 创建一个相当小的图,它需要包含某个图例。然而,虽然空间紧张,但由于某种原因,matplotlib 使图例过宽,在图例的右侧部分添加了大量空白:

在此处输入图像描述

有办法解决此问题吗?虽然我无法给出重现该图的所有代码,但以下是一些可能相关的部分:

text_width_in_inches = 5.95114 # Used to scale PGF plots
very_small_font_size = 5
small_font_size = 7
medium_font_size = 9
thin_line_width = 0.75
small_marker_size = 3.75
...
def set_plot_width(fig, text_width_ratio):
    old_width, old_height = fig.get_size_inches()
    new_width = text_width_ratio*text_width_in_inches
    new_height = (new_width/old_width)*old_height
    fig.set_size_inches(new_width, new_height)
...
matplotlib.use("pgf")
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'axes.labelsize': small_font_size,
    'axes.titlesize': medium_font_size,
    'xtick.labelsize': small_font_size,
    'ytick.labelsize': small_font_size,
    'legend.fontsize': small_font_size,
    'lines.linewidth': thin_line_width,
    'lines.markersize': small_marker_size,
})
...
plt.legend(fontsize=very_small_font_size)
plt.xlabel("Mode storage order")
plt.ylabel("Normalized compression factor")
plt.ylim(miny, maxy)
set_plot_width(plt.gcf(), 0.4 if len(mode_storage_orders) <= 10 else 0.6)
plt.savefig(figure_path + dataset_name.replace(" ", "-") + "-core-flattenings.pgf", bbox_inches="tight")
plt.close()

如您所见,我将绘图渲染为 PGF 文件,以便可以将其直接包含在我的 Latex 文档中但是,这可能是问题的一部分:当我切换回默认的“TkAgg”后端时,删除一些与 Latex/PGF 相关的 rcParams 设置并将绘图保存到 PNG 文件中,我得到以下结果:

在此处输入图像描述

解决了图例问题,但这当然不是我想要的。

最后,我还尝试对某些图例 kwargs 进行一些修改,例如 borderpadcolumnspacing,但这没有帮助。

I'm creating a fairly small plot with matplotlib which needs to include a certain legend. However, while space is tight, for some reason matplotlib makes the legend excessively wide, adding a lot of whitespace to the right part of the legend:

enter image description here

Is there a way to fix this? Although I can't give all the code to reproduce this figure, here are some parts that might be relevant:

text_width_in_inches = 5.95114 # Used to scale PGF plots
very_small_font_size = 5
small_font_size = 7
medium_font_size = 9
thin_line_width = 0.75
small_marker_size = 3.75
...
def set_plot_width(fig, text_width_ratio):
    old_width, old_height = fig.get_size_inches()
    new_width = text_width_ratio*text_width_in_inches
    new_height = (new_width/old_width)*old_height
    fig.set_size_inches(new_width, new_height)
...
matplotlib.use("pgf")
matplotlib.rcParams.update({
    "pgf.texsystem": "pdflatex",
    'font.family': 'serif',
    'text.usetex': True,
    'pgf.rcfonts': False,
    'axes.labelsize': small_font_size,
    'axes.titlesize': medium_font_size,
    'xtick.labelsize': small_font_size,
    'ytick.labelsize': small_font_size,
    'legend.fontsize': small_font_size,
    'lines.linewidth': thin_line_width,
    'lines.markersize': small_marker_size,
})
...
plt.legend(fontsize=very_small_font_size)
plt.xlabel("Mode storage order")
plt.ylabel("Normalized compression factor")
plt.ylim(miny, maxy)
set_plot_width(plt.gcf(), 0.4 if len(mode_storage_orders) <= 10 else 0.6)
plt.savefig(figure_path + dataset_name.replace(" ", "-") + "-core-flattenings.pgf", bbox_inches="tight")
plt.close()

As you can see, I'm rendering the plot to a PGF file so that it can be included directly in my Latex document with matching fonts etc. However, this might be part of the problem: when I switch back to my default "TkAgg" back-end, remove a few Latex/PGF-related rcParams settings and save the plot to a PNG file, I get the following result:

enter image description here

which fixes the legend problem, but is not what I want of course.

Finally, I also tried messing a bit with certain legend kwargs, such as borderpad and columnspacing, but this didn't help.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文