我有一个图表,上面有情节外的传说.我有标题在图表的中心,我想保持它的中心.问题是,标题只围绕着图表,而不是图表和图例作为一个组.所以它让我的情节看起来总是扭曲的: [1]:https://i.stack.imgur.com/dOlpS.png

有没有一种方法可以将图表移到一边,使标题显示在两者的中心?

最小生殖率:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,1000)
y1 = np.sin(x)
y2 = np.cos(x)

plt.plot(x,y1)
plt.plot(x,y2)
plt.title("A very long title that makes the graph shorter than the title itself, it's so long it makes me sick to the core")
plt.legend(["sin x", "cos x"], bbox_to_anchor = [1,0.5], loc = "center left", fontsize = 20)
plt.show()

推荐答案

您的方法相当于在matplotlib的对象框架中执行以下操作.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(constrained_layout=True)

ax.plot(x,y1)
ax.plot(x,y2)
ax.legend(["sin x", "cos x"], bbox_to_anchor = [1,0.5], loc = "center left", fontsize = 20)
ax.set_title("A very long title that makes the graph shorter than the title itself,\n it's so long it makes me sick to the core.\n")

ax_title

基本上,标题被分配给实际的情节(ax对象).您需要的是将标题分配给整个图像,这将包括情节和图例(即图形对象).这将是新的结果.

import matplotlib.pyplot as plt

fig, ax = plt.subplots(constrained_layout=True)

ax.plot(x,y1)
ax.plot(x,y2)
ax.legend(["sin x", "cos x"], bbox_to_anchor = [1,0.5], loc = "center left", fontsize = 20)
fig.suptitle("A very long title that makes the graph shorter than the title itself,\n it's so long it makes me sick to the core.\n")

fig_title

Python相关问答推荐

如何从FDaGrid实例中删除某些函数?

GL pygame无法让缓冲区与vertextPointer和colorPointer一起可靠地工作

通过优化空间在Python中的饼图中添加标签

acme错误-Veritas错误:模块收件箱没有属性linear_util'

Python上的Instagram API:缺少client_id参数"

删除任何仅包含字符(或不包含其他数字值的邮政编码)的观察

为什么以这种方式调用pd.ExcelWriter会创建无效的文件格式或扩展名?

ThreadPoolExecutor和单个线程的超时

索引到 torch 张量,沿轴具有可变长度索引

SQLAlchemy bindparam在mssql上失败(但在mysql上工作)

启用/禁用shiny 的自动重新加载

如何杀死一个进程,我的Python可执行文件以sudo启动?

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

Python避免mypy在相互引用中从另一个类重定义类时失败

为什么调用函数的值和次数不同,递归在代码中是如何工作的?

交替字符串位置的正则表达式

用SymPy在Python中求解指数函数

如果不使用. to_list()[0],我如何从一个pandas DataFrame中获取一个值?

PYTHON中的selenium不会打开 chromium URL

FileNotFoundError:[WinError 2]系统找不到指定的文件:在os.listdir中查找扩展名