Matplotlib 我如何适应长标题?

Matplotlib 我如何适应长标题?,matplotlib,Matplotlib,有一个-但我不能使那里提出的解决方案起作用 下面是一个标题较长的示例图: #!/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

有一个-但我不能使那里提出的解决方案起作用

下面是一个标题较长的示例图:

#!/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:“模块”对象没有属性“紧密布局”
如果我将
(matplotlib.pyplot).tight_layout()
替换为
fig.tight_layout()
,它给出:

AttributeError:'Figure'对象没有属性'tight\u layout'

因此,我的问题是-如何使标题适合情节?

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

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


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

以下是我最后用到的:

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

我倾向于采用@Adobe的解决方案:

plt.title("First Title\n%s" % "\n".join(wrap("Second Title", width=60)))
  • 是来自
    matplotlib
    3.1.2版文档的正式答案
  • 也很有用
  • 在Jupyter中显示内联绘图不起作用,因为除非您这样做
    • plt.savefig(…)
      似乎可以正常使用
      wrap
导入matplotlib.pyplot作为plt
x=[1,2,3]
y=[4,5,6]
#初始化:
图,轴=plt.子批次(图尺寸=(8.0,5.0))
#标题:
myTitle=“一些我真的真的真的真的真的真的真的很长的标题,我真的真的真的真的很需要-而且只是不能-只是不能-使它变得任何-简单地任何-更短-在所有。”
#线路:
坐标轴绘图(x,y)
#定名
轴。设置标题(myTitle,loc='center',wrap=True)
plt.show()

  • 以下方法也适用
plt.图(figsize=(8,5))
#标题:
myTitle=“一些我真的真的真的真的真的真的真的很长的标题,我真的真的真的真的很需要-而且只是不能-只是不能-使它变得任何-简单地任何-更短-在所有。”
#线路:
平面图(x,y)
#定名
plt.title(myTitle,loc='center',wrap=True)
plt.show()
  • 不推荐使用以下添加轴的方法
MatplotlibDeprecationWarning:使用与以前的轴相同的参数添加轴当前将重用以前的实例。在将来的版本中,将始终创建并返回一个新实例。同时,可以通过向每个轴实例传递唯一的标签来抑制此警告,并确保将来的行为。

在文本标题中添加
\n
之前 之后
tight_布局
仅出现在matplotlib的最后两个版本中。你用的是什么版本?我认为
tight\u layout
是在1.1中添加的,尽管它可能是1.0。@乔·金顿:也许你是对的:复制你的布局也会出现同样的错误。我正在下载最新的源代码。@Adobe自定义matplotlib的“转到”页面在这里:,不过如果您已经知道要查找的内容,它会有所帮助!这是可行的,但这是一个糟糕的解决方案。在您的示例中,标题的字体甚至比记号标签的字体更小。这将使标题在出版物或演示文稿中很难阅读。@gerrit解决方案的优点取决于个人用户。对于像你这样的一些人来说,最终的目标可能是出版一本学术刊物。对于其他人,标题可用于存储元数据,例如系统参数,因此美学不那么重要。这当然是对“我如何将标题与情节相匹配”这一问题的回答。
# 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)
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')