matplotlib pandas to table

import numpy as npimport matplotlib.pyplot as plttitle_text = 'Loss by Disaster'footer_text = 'June 24, 2020'fig_background_color = 'skyblue'fig_border = 'steelblue'data =  [            [         'Freeze', 'Wind', 'Flood', 'Quake', 'Hail'],            [ '5 year',  66386, 174296,   75131,  577908,  32015],            ['10 year',  58230, 381139,   78045,   99308, 160454],            ['20 year',  89135,  80552,  152558,  497981, 603535],            ['30 year',  78415,  81858,  150656,  193263,  69638],            ['40 year', 139361, 331509,  343164,  781380,  52269],        ]# Pop the headers from the data arraycolumn_headers = data.pop(0)row_headers = [x.pop(0) for x in data]# Table data needs to be non-numeric text. Format the data# while I'm at it.cell_text = []for row in data:    cell_text.append([f'{x/1000:1.1f}' for x in row])# Get some lists of color specs for row and column headersrcolors = plt.cm.BuPu(np.full(len(row_headers), 0.1))ccolors = plt.cm.BuPu(np.full(len(column_headers), 0.1))# Create the figure. Setting a small pad on tight_layout# seems to better regulate white space. Sometimes experimenting# with an explicit figsize here can produce better outcome.plt.figure(linewidth=2,           edgecolor=fig_border,           facecolor=fig_background_color,           tight_layout={'pad':1},           #figsize=(5,3)          )# Add a table at the bottom of the axesthe_table = plt.table(cellText=cell_text,                      rowLabels=row_headers,                      rowColours=rcolors,                      rowLoc='right',                      colColours=ccolors,                      colLabels=column_headers,                      loc='center')# Scaling is the only influence we have over top and bottom cell padding.# Make the rows taller (i.e., make cell y scale larger).the_table.scale(1, 1.5)# Hide axesax = plt.gca()ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)# Hide axes borderplt.box(on=None)# Add titleplt.suptitle(title_text)# Add footerplt.figtext(0.95, 0.05, footer_text, horizontalalignment='right', size=6, weight='light')# Force the figure to update, so backends center objects correctly within the figure.# Without plt.draw() here, the title will center on the axes and not the figure.plt.draw()# Create image. plt.savefig ignores figure edge and face colors, so map them.fig = plt.gcf()plt.savefig('pyplot-table-demo.png',            #bbox='tight',            edgecolor=fig.get_edgecolor(),            facecolor=fig.get_facecolor(),            dpi=150            )

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source