我正在尝试将一系列脚注添加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)
There are of course a multitude of options how to make sure some text label is acutally within the figure when saving.
bbox_inches
argumentYou 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()
您可以使用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] 删除。
我来说两句