我如何适合长冠军?

Ado*_*obe 22 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(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")
Run Code Online (Sandbox Code Playgroud)

它给了一个

 AttributeError: 'module' object has no attribute 'tight_layout'
Run Code Online (Sandbox Code Playgroud)

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

 AttributeError: 'Figure' object has no attribute 'tight_layout'
Run Code Online (Sandbox Code Playgroud)

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

Ado*_*obe 61

这是我最终使用的:

#!/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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Tre*_*ney 17

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • 以下也有效
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()
Run Code Online (Sandbox Code Playgroud)

笔记

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

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.


Hoo*_*ked 7

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

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

你联系的答案是其他一些涉及添加换行符的好解决方案.甚至还有一个基于图形调整大小的自动解决方案!

  • @gerrit解决方案的优点取决于个人用户.对于一些人,像你自己一样,最终目标可能是学术出版物.对于其他人,标题可以用于存储元数据,例如系统参数,因此美学不太重要.这肯定是一个问题的答案,"我如何适应情节的标题". (4认同)