繁体   English   中英

如何在matplotlib表上显示数据框索引名称?

[英]How to show dataframe index name on a matplotlib table?

我有一个数据框,希望通过matplotlib在PDF文件中显示为表格。

我的代码看起来像这样(我想绕过我之前做的事情,因为我认为问题与数据无关):

table = ax_table.table(cellText=str_df.values, rowLabels=str_df.index, cellLoc='center',
                       colColours=['gainsboro'] * len(table_col), colLabels=table_col, loc='center',
                       colWidths= [0.12]*(len(table_col)), cellColours = img.to_rgba(df_for_table.values))

我的数据框(str_df)索引名称是“ Area”,我想在matplotlib表中显示它,而不要使用空格(红色圆圈):

在此处输入图片说明

有人知道这是否可能吗?

谢谢。

您可以在相应位置添加一个单元格,并使用dataframe.index.name作为其文本。

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

df = pd.DataFrame(np.random.randint(1,50, size=(8,4)), columns=list("ABCD"))
df.index.name = "Name"


fig, ax = plt.subplots()
table = ax.table(cellText=df.values, rowLabels=df.index, cellLoc='center',
                 colColours=['gainsboro'] * len(df), colLabels=df.columns, loc='center',
                 colWidths= [0.12]*(len(df.columns)))
w, h = table[0,1].get_width(), table[0,1].get_height()
table.add_cell(0, -1, w,h, text=df.index.name)
plt.show()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM