matplotlib 如何设置长标题?

c7rzv4ha  于 2023-06-23  发布在  其他
关注(0)|答案(5)|浏览(127)

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

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

euoag5mw

euoag5mw1#

以下是我最终使用的:

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

vsnjm48y

vsnjm48y2#

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()

  • 下面的方法也适用
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()

注意事项

  • 不推荐使用以下添加轴的方法
# 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)

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.

jucafojl

jucafojl3#

一种方法是简单地更改标题的字体大小:

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()

在你链接的答案中,还有其他几个好的解决方案,涉及到添加换行符。甚至有一个automatic solution,大小的基础上小康的数字!

h43kikqp

h43kikqp4#

我更喜欢以这种方式调整@Adobe的解决方案:

plt.title("First Title\n%s" % "\n".join(wrap("Second Title", width=60)))
deikduxw

deikduxw5#

在文本标题中添加\n

之前

axs[0, 0].set_title('pure imshow of 2D-array (RGB)')
axs[0, 1].set_title('Mean filter')                                     # imshow by default applies a standard colormap,  viridis cmap, which is greenish. 
axs[0, 2].set_title('pure imshow of 2D-array (R-channel)')             # 
axs[1, 0].set_title('imshow of 2D-array with R values cmap="Grey_r"')
axs[1, 1].set_title('imshow of 2D-array with R values cmap="Reds_r"')
axs[1, 2].set_title('imshow of 3D-array with coordinates 1 and 2 \n(i.e.: channels G and B) set to 0')

之后

axs[1, 0].set_title('imshow of 2D-array \n with R values cmap="Grey_r"')
axs[1, 1].set_title('imshow of 2D-array \n with R values cmap="Reds_r"')
axs[1, 2].set_title('imshow of 3D-array with coordinates 1 and 2 \n channels G and B) set to 0')

相关问题