gpt4 book ai didi

matplotlib - 我如何适应长标题?

转载 作者:行者123 更新时间:2023-12-03 11:31:22 26 4
gpt4 key购买 nike

有一个 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'

如果我更换 (matplotlib.pyplot).tight_layout()fig.tight_layout()它给:

 AttributeError: 'Figure' object has no attribute 'tight_layout'

所以我的问题是 - 我如何使标题适合情节?

最佳答案

这是我最终使用的:

#!/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")

enter image description here

关于matplotlib - 我如何适应长标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10351565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com