使用 pandas.DataFrame.plot 格式化添加到绘图中的表

spa*_*row 0 python matplotlib pandas

我正在使用 pandas.DataFrame.plot 生成带有表格的条形图。

有没有办法格式化表格中的表格大小和/或字体大小以使其更具可读性?

我的数据帧(dfexe):

City    State   Waterfalls  Lakes   Rivers
LA  CA  2   3   1
SF  CA  4   9   0
Dallas  TX  5   6   0
Run Code Online (Sandbox Code Playgroud)

创建带有表格的条形图:

myplot = dfex.plot(x=['City','State'],kind='bar',stacked='True',table=True)
myplot.axes.get_xaxis().set_visible(False)
Run Code Online (Sandbox Code Playgroud)

输出: 在此处输入图片说明

Rom*_*ain 5

这是一个答案。

# Test data
dfex = DataFrame({'City': ['LA', 'SF', 'Dallas'],
 'Lakes': [3, 9, 6],
 'Rivers': [1, 0, 0],
 'State': ['CA', 'CA', 'TX'],
 'Waterfalls': [2, 4, 5]})

myplot = dfex.plot(x=['City','State'],kind='bar',stacked='True',table=True)
myplot.axes.get_xaxis().set_visible(False)
# Getting the table created by pandas and matplotlib
table = myplot.tables[0]
# Setting the font size
table.set_fontsize(12)
# Rescaling the rows to be more readable
table.scale(1,2)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

注意:另请查看答案以获取更多信息。