简体   繁体   中英

Format a table that was added to a plot using pandas.DataFrame.plot

I'm producing a bar graph with a table using pandas.DataFrame.plot.

Is there a way to format the table size and/or font size in the table to make it more readable?

My DataFrame (dfexe):

City    State   Waterfalls  Lakes   Rivers
LA  CA  2   3   1
SF  CA  4   9   0
Dallas  TX  5   6   0

Create a bar chart with a table:

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

Output: 在此处输入图片说明

Here is an answer.

# 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)

在此处输入图片说明

Note: Check also this answer for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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