我如何适合长标题? [英] How do I fit long title?

查看:108
本文介绍了我如何适合长标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个类似的问题-但我无法在那里提出解决方案工作.

There's a similar question - but I can't make the solution proposed there work.

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

Here's an example plot with a long title:

#!/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(),它会给出:

and if I replace (matplotlib.pyplot).tight_layout() with fig.tight_layout() it gives:

 AttributeError: 'Figure' object has no attribute 'tight_layout'

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

So my question is - how do I fit the title to the plot?

推荐答案

这是我最终使用的内容:

Here's what I've finally used:

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

这篇关于我如何适合长标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆