我如何适合长冠军?

时间:2012-04-27 13:30:32

标签: matplotlib

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

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

5 个答案:

答案 0 :(得分:52)

这是我最终使用的内容:

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

答案 1 :(得分:6)

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

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

enter image description here

在您回答的答案中,其他几个好的解决方案涉及添加换行符。甚至还有automatic solution根据图形调整大小!

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

enter image description here

  • 以下内容也适用
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.

答案 4 :(得分: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')