打印Matplotlib标题在表摘录上方形成大熊猫数据框架

发布于 2025-02-10 01:34:38 字数 945 浏览 1 评论 0原文

我有一个数据框,可以根据输入而更改大小。 我使用Matplotlib以表格的形式绘制此数据框架。 这是我的代码:

fig, ax = plt.subplots()
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')

the_table = ax.table(cellText=df_to_plot.values, colLabels=df_to_plot.columns, loc='center')
the_table.auto_set_font_size(False)  # To have a correct display
the_table.set_fontsize(7)
ax.set_title(f"{title}\n", y=1, pad=10)
fig.tight_layout() # I tried with and without
fig.savefig(save_name, bbox_inches='tight') # save_name end with .pdf

当我拥有带有许多行的数据框时出现的问题,标题与表重叠。 我试图将y = 1打印为无花果的顶部,但它静止。 我尝试设置pad,但它不会更改任何内容。 我尝试使用.thight_layout()对其进行纠正,但会引发一个错误:用户浏览:不应用的紧密布局。

我的表不超过PDF文件,但没有位于标题的顶部。 ”

我希望标题在我的桌子上方,无论其大小如何。

I have a data frame that can change size depending on the input.
I plot this data frame in the form of a table using matplotlib.
Here's my code :

fig, ax = plt.subplots()
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')

the_table = ax.table(cellText=df_to_plot.values, colLabels=df_to_plot.columns, loc='center')
the_table.auto_set_font_size(False)  # To have a correct display
the_table.set_fontsize(7)
ax.set_title(f"{title}\n", y=1, pad=10)
fig.tight_layout() # I tried with and without
fig.savefig(save_name, bbox_inches='tight') # save_name end with .pdf

My problem appears when i have a dataframe with many rows, the title overlaps the table.
I tried to put y=1 to print the title a the top of the fig but it stills overlaps.
I tried to set a pad but it doesn't change anything.
I tried to use .thight_layout() to correct it but it throws a error : UserWarning: Tight layout not applied.

My table does not exceed the pdf file but there is no place at the top for the title.Result of the matplotlib plot with the title overlaping

I want the title to be just above my table, regardless of its size.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2025-02-17 01:34:38

使用bbox参数可为您提供最佳控制权,以最佳控制 table

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 1000, size=(88,7)))

fig, ax = plt.subplots(figsize=(210/25.4, 297/25.4), layout='constrained')
fig.suptitle('The Title')
ax.axis('off')
table = ax.table(cellText=df.values, colLabels=df.columns, bbox=[0,0,1,1])
table.set_fontsize(7)
fig.savefig('table.png', bbox_inches='tight') 

Using the bbox parameter gives you best control over placing the table:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 1000, size=(88,7)))

fig, ax = plt.subplots(figsize=(210/25.4, 297/25.4), layout='constrained')
fig.suptitle('The Title')
ax.axis('off')
table = ax.table(cellText=df.values, colLabels=df.columns, bbox=[0,0,1,1])
table.set_fontsize(7)
fig.savefig('table.png', bbox_inches='tight') 

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文