![](/img/trans.png)
[英]How to show more categories in a Matplotlib line plot of a Pandas DataFrame Pivot Table?
[英]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.