31

有一个类似的问题- 但我无法使那里提出的解决方案起作用。

这是一个带有长标题的示例图:

#!/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'

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

4

5 回答 5

88

这是我最终使用的:

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

在此处输入图像描述

于 2012-05-17T11:30:37.783 回答
8

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

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

在此处输入图像描述

在您链接的答案中,还有其他几个涉及添加换行符的好解决方案。甚至还有一个自动解决方案可以根据图形调整大小!

于 2012-04-27T13:38:57.917 回答
6
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.

于 2020-04-23T18:42:31.383 回答
2

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

plt.title("First Title\n%s" % "\n".join(wrap("Second Title", width=60)))
于 2019-09-29T15:03:34.880 回答
0

添加\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')
于 2021-01-07T11:37:40.203 回答