将 figtext 添加到子图底部的问题

horcle_buzz

我正在尝试将一系列脚注添加plt.figtext到一组子图中,当我使用plt.savefig()命令或%matplotlib notebook保存到文件选项打印到文件时,无法让它们全部显示出来

脚注列表很长,看起来像(渲染到屏幕时,效果很好):所需的数字和脚注输出

但是,当我打印到文件时,它们会被切断:不需要的输出

我将子图的图形大小设置为plt.figure(figsize=(7,10))并用于plt.tight_layout()格式化子图,以便轴不会与标题混搭。

I have been trying for the life of me to find a way to extend the size of the output to account for the footnotes AND the subplots, but have had absolutely no luck. This does not seem like an unreasonable use case, by any stretch of the imagination.

EDIT

The text is generated after each subplot is created, along the lines of:

plt.legend(loc=6, fontsize=10)
#plt.legend(bbox_to_anchor=(1.0, 1), loc=2, borderaxespad=0.)
plt.xlim(0, 45)
plt.ylim(2.5, 5.5)
plt.xlabel('Distance (km)', alpha=.75)
plt.ylabel('Pace (min/km)', alpha=.75)
plt.title('Top 4 Male Finishers and SA 2016 Twin Cities Marathon Timed$^1$ Splits$^2$')
plt.figtext(0.814, 0.01, '1 Provided by mtecResults', horizontalalignment='right', fontsize=6) 
plt.figtext(0.88, 0.0, '2 Mean time per unit distance between two points', horizontalalignment='right', fontsize=6) 
plt.figtext(0.845, -0.01, '3 SA Finished 670 out of 4716 Males', horizontalalignment='right', fontsize=6)
plt.figtext(0.845, -0.02, '4 SA Finished 754 out of 4756 Males', horizontalalignment='right', fontsize=6)
ImportanceOfBeingErnest

There are of course a multitude of options how to make sure some text label is acutally within the figure when saving.

1. the bbox_inches argument

You may choose not to use plt.tight_layout(), because this ignores text added as a text label. You can then use the bbox_inches argument to plt.savefig:

plt.savefig("output.png", bbox_inches = "tight")

This method increases the figure size until all text is included.

Placing the text would best be done using verticalalignment ="top" and placing the test close to y=0 in figure coordinates. Example:

import matplotlib.pyplot as plt
plt.gca()

text = """1 Provided by mtecResults
2 Mean time per unit distance between two points
3 SA Finished 670 out of 4716 Males
4 SA Finished 754 out of 4756 Males"""

plt.figtext(0.05,0.00, text, fontsize=8, va="top", ha="left")

plt.savefig(__file__+".png", bbox_inches = "tight")
plt.show()

在此处输入图片说明

2. 为图形添加边距。

您可以使用plt.subplots_adjust(bottom=0.4)在轴周围留出更多空间,然后可以用文本填充。对于其他维度,根据文本所在的位置使用参数top, left, right
此选项要求将文本定位在图中。它不会改变图形大小,但会减小轴的大小。

放置文本最好使用verticalalignment ="bottom"并将测试放置在图形坐标中靠近但高于 y=0 的位置。例子:

import matplotlib.pyplot as plt

plt.gca()
plt.subplots_adjust(bottom=0.2)

text = """1 Provided by mtecResults
2 Mean time per unit distance between two points
3 SA Finished 670 out of 4716 Males
4 SA Finished 754 out of 4756 Males"""

plt.figtext(0.05,0.01, text, fontsize=8, va="bottom", ha="left")

plt.savefig(__file__+"2.png")
plt.show()

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Matplotlib; 将圆添加到子图-问题/困惑

来自分类Dev

将新的子行添加到父行时出现问题;新行始终添加到工作表底部

来自分类Dev

如何将轴添加到子图?

来自分类Dev

将标签添加到matplotlib中的子图

来自分类Dev

如何将pylab.plot行放在pylab.figtext上?

来自分类Dev

将总行添加到枢轴底部

来自分类Dev

将数据添加到范围的底部

来自分类Dev

将标签添加到WebView的底部

来自分类Dev

将总行添加到枢轴底部

来自分类Dev

将输入添加到表格底部

来自分类Dev

将条形图添加到geom_density的底部

来自分类Dev

如何将两个图添加到子图?

来自分类Dev

将子级添加到父级实体而不加载完整的子级图

来自分类Dev

将点添加到RgoogleMaps图

来自分类Dev

将 % 符号添加到 R 图

来自分类Dev

figtext datetime函数matplotlib

来自分类Dev

将第二个数据系列添加到Matplotlib中的子图

来自分类Dev

在不使用Plotly Express的情况下将垂直矩形添加到绘图子图

来自分类Dev

将子图添加到程序会更改直方图的显示方式

来自分类Dev

将 3D 子图添加到 2D 集

来自分类Dev

将颜色条添加到 Julia PyPlot 中的子图

来自分类Dev

将节点添加到列表的问题

来自分类Dev

将jQuery添加到HTML问题

来自分类Dev

将图像添加到 Openlayers 的问题

来自分类Dev

将元素添加到子列表

来自分类Dev

将子视图添加到UIScrollView

来自分类Dev

将子视图添加到UITableViewCell

来自分类Dev

将子元素添加到XML

来自分类Dev

将图标添加到子页面