有一个类似的问题 - 但我不能让那里提出的解决方案有效.
这是一个带有长标题的示例图:
#!/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")
Run Code Online (Sandbox Code Playgroud)
它给了一个
AttributeError: 'module' object has no attribute 'tight_layout'
Run Code Online (Sandbox Code Playgroud)
如果我(matplotlib.pyplot).tight_layout()
用fig.tight_layout()
它替换它给出:
AttributeError: 'Figure' object has no attribute 'tight_layout'
Run Code Online (Sandbox Code Playgroud)
所以我的问题是 - 如何使标题符合情节?
Ado*_*obe 61
这是我最终使用的:
#!/usr/bin/env python3
import matplotlib
from matplotlib import pyplot as plt
from textwrap import wrap
data = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(data, data)
title = ax.set_title("\n".join(wrap("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.", 60)))
fig.tight_layout()
title.set_y(1.05)
fig.subplots_adjust(top=0.8)
fig.savefig("1.png")
Run Code Online (Sandbox Code Playgroud)
Tre*_*ney 17
matplotlib
是文档
中的官方答案plt.savefig(...)
似乎可以正常工作wrap
import matplotlib.pyplot as plt
x = [1,2,3]
y = [4,5,6]
# initialization:
fig, axes = plt.subplots(figsize=(8.0, 5.0))
# 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."
# lines:
axes.plot(x, y)
# set title
axes.set_title(myTitle, loc='center', wrap=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
plt.figure(figsize=(8, 5))
# 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."
# lines:
plt.plot(x, y)
# set title
plt.title(myTitle, loc='center', wrap=True)
plt.show()
Run Code Online (Sandbox Code Playgroud)
# 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(myTitle, loc='center', wrap=True)
Run Code Online (Sandbox Code Playgroud)
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.
一种方法是简单地改变标题的字体大小:
import pylab as plt
plt.rcParams["axes.titlesize"] = 8
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."
plt.title(myTitle)
plt.show()
Run Code Online (Sandbox Code Playgroud)
你联系的答案是其他一些涉及添加换行符的好解决方案.甚至还有一个基于图形调整大小的自动解决方案!
归档时间: |
|
查看次数: |
23652 次 |
最近记录: |