有一个similar question-但我不能使那里提出的解决方案工作。
下面是一个带有长标题的示例图:
#!/usr/bin/env python
import matplotlib
import matplotlib.pyplot
import textwrap
x = [1,2,3]
y = [4,5,6]
# initialization:
fig = matplotlib.pyplot.figure(figsize=(8.0, 5.0))
# lines:
fig.add_subplot(111).plot(x, y)
# title:
myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."
fig.add_subplot(111).set_title("\n".join(textwrap.wrap(myTitle, 80)))
# tight:
(matplotlib.pyplot).tight_layout()
# saving:
fig.savefig("fig.png")
它给出了一个
AttributeError: 'module' object has no attribute 'tight_layout'
如果我用fig.tight_layout()
替换(matplotlib.pyplot).tight_layout()
,则得到:
AttributeError: 'Figure' object has no attribute 'tight_layout'
所以我的问题是-我如何适合标题的情节?
5条答案
按热度按时间euoag5mw1#
以下是我最终使用的:
vsnjm48y2#
matplotlib
文档的官方答案plt.savefig(...)
似乎与wrap
一起工作注意事项
MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.
jucafojl3#
一种方法是简单地更改标题的字体大小:
在你链接的答案中,还有其他几个好的解决方案,涉及到添加换行符。甚至有一个automatic solution,大小的基础上小康的数字!
h43kikqp4#
我更喜欢以这种方式调整@Adobe的解决方案:
deikduxw5#
在文本标题中添加
\n
之前
之后