打印Matplotlib标题在表摘录上方形成大熊猫数据框架
我有一个数据框,可以根据输入而更改大小。 我使用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()
对其进行纠正,但会引发一个错误:用户浏览:不应用的紧密布局。
我希望标题在我的桌子上方,无论其大小如何。
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.
I want the title to be just above my table, regardless of its size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
bbox
参数可为您提供最佳控制权,以最佳控制 table :Using the
bbox
parameter gives you best control over placing the table: