Monday, 17 December 2018

Double header in Matplotlib Table

I need to plot a table in matplotlib. The problem is some columns have one-level headers, some columns have double-level headers.

Here's what I need:

Table needed

Here's simple example for one-level headers:

df = pd.DataFrame()
df['Animal'] = ['Cow', 'Bear']
df['Weight'] = [250, 450]
df['Favorite'] = ['Grass', 'Honey']
df['Least Favorite'] = ['Meat', 'Leaves']
df

enter image description here

fig = plt.figure(figsize=(9,2))
ax=plt.subplot(111)
ax.axis('off') 
table = ax.table(cellText=df.values, colColours=['grey']*df.shape[1], bbox=[0, 0, 1, 1], colLabels=df.columns)
plt.savefig('Table.jpg')

Last chunk of code produces next picture:

enter image description here

What changes do I need to make to have table I need?



from Double header in Matplotlib Table

No comments:

Post a Comment