'How to adjust the plot.table() under exactly bars of barplot?

I am trying to adjust the plot.table() under exactly attachment to the Bar plot.So that each bar of bar-plot could be exactly under the each column plot table.

import numpy as np
import matplotlib.pyplot as plt


data = np.array([[100.,  70.924, 100.,  97.386],
                 [99.8485,  99.8864,  99.9242,  70.8485],
                 [80., 100., 100.,  99.5833],
                 [99.8485,  99.8864,  99.9242,  99.8485],
                 [99.8864,  99.8864,  99.9242,  99.9242]])
text_columns = np.array([['', '', '', '', '', '', '', '', '', '', '', '✓', '', '', '', '',
                          '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '✓', '',
                          '✓', '✓', '✓', ''],
                         ['', '', '', '', '', '', '', '', '', '✓', '', '', '', '', '', '',
                          '', '', '', ''],
                         ['✓', '✓', '✓', '✓', '✓', '✓', '✓', '✓', '', '✓', '', '', '✓',
                          '✓', '', '✓', '', '', '', ''],
                         ['✓', '✓', '✓', '✓', '✓', '✓', '✓', '✓', '', '✓', '', '', '✓', '',
                          '', '✓', '', '', '', ''],
                         ['', '', '', '✓', '', '', '', '✓', '✓', '', '', '✓', '', '', '',
                          '✓', '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '', '', '✓', '', '', '', '', '',
                          '✓', '✓', '✓', ''],
                         ['', '', '', '', '', '', '', '', '✓', '', '✓', '', '', '', '✓',
                          '', '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
                          '✓', '✓', '✓', ''],
                         ['', '', '', '', '', '', '', '', '', '', '✓', '', '', '✓', '', '',
                          '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '', '', '', '✓', '', '', '', '',
                          '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '✓', '', '', '', '', '', '', '',
                          '', '', '', ''],
                         ['✓', '✓', '✓', '', '✓', '✓', '✓', '', '', '', '', '', '✓', '',
                          '✓', '', '', '', '', ''],
                         ['', '', '', '', '', '', '', '', '', '', '', '', '', '✓', '', '',
                          '', '', '', '']], dtype='<U1')
row_labels = ['f1','f2','f3','f4','f5','f6','f7','f8','f9','f10','f11','f12','f13','f14']
length = len(data1)
x_labels = ['M1', 'M2', 'M3', 'M4', 'M5']

plt.rcParams["figure.figsize"] = [8, 4]
# Set plot parameters
fig, ax = plt.subplots()
width = 0.2  # width of bar
x = np.arange(length)

ax.bar(x, data[:, 0], width, label='DT', )
ax.bar(x + width, data[:, 1], width, label='ET',)
ax.bar(x + (2 * width), data[:, 2], width, label='RF')
ax.bar(x + (3 * width), data[:, 3], width, label='KNN')

ax.set_ylabel('accuracy')
ax.set_ylim(0, 75)
ax.xaxis.tick_top()
ax.set_xticks(x + width + width/2)
ax.set_xticklabels(x_labels)
ax.set_title('Models with Features')
ax.legend()
plt.grid(True, 'major', 'y', ls='--', lw=.5, c='k', alpha=.3)
the_table = ax.table(cellText=text_columns,
                      rowLabels=row_labels,
                      loc="bottom")
the_table.scale(1, 1.5)
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.legend()
fig.tight_layout()
plt.show()

it's my output image I removed the space between bar graph and plot.table(). Each column of plot table doesn't match with bar graph plot. so Finally it looks like below my output image



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source