sparrow sparrow - 2 months ago 8
Python Question

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:
enter image description here

Answer

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)

enter image description here

Note: Check also this answer for more information.

Comments