Ami*_*ene 2 python matplotlib dataframe
我有一个数据框,我想通过 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))
Run Code Online (Sandbox Code Playgroud)
我的数据框(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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4097 次 |
| 最近记录: |