Home > Enterprise >  matplotlib draw a table below the bar chart label
matplotlib draw a table below the bar chart label

Time:10-01

I'm drawing a table below the bar chart in matplotlib with the following code. However, I found the table text and the bar chart's x label are mixed. Is there a way to move the table down below the bar chart's x label? (I don't want to turn the x label into table's column text because some labels have very long text)

enter image description here

import numpy as np
import matplotlib.pyplot as plt

labels = ['a', 'longlonglong', 'bbb', 'ccc', 'dddddddd', 'eeeee', 'ffff', 'ggggggggg']
code_size = ['5KB', '83KB', '1.7MB', '18KB', '1MB', '18KB', '4MB', '55KB']

step1 = [0.75, 1.22, 20.27, 0.49, 5.52, 11.76, 2.30, 0.64]
step2 = [0.89, 3.62, 18.69, 0.22, 9.61, 14.06, 1.28, 0.27]
width = 0.8

fig, ax = plt.subplots()
ax.bar(labels, step1, width, label='Step 1')
ax.bar(labels, step2, width, bottom=step1, label='Step 2')
ax.set_ylabel('Time used (s)')
ax.set_title('Time and size')
ax.legend()
plt.xticks(rotation = 45)

plt.table(cellText=[code_size],
          rowLabels=['Code size'],
          loc='bottom')

plt.subplots_adjust(bottom=0.05)
plt.show()

CodePudding user response:

It is easiest to use subplots to place graphs and tables. I am answering this question by referring to a enter image description here

  • Related