如何删除 PGF 图的 matplotlib 图例中多余的空格
我正在使用 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 进行一些修改,例如 borderpad
和 columnspacing
,但这没有帮助。
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:
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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论