我正试图用matplotlib中的表创建一个水平条形图。我就快到了,但没能把桌子和图表对齐。到目前为止我得到的:
import matplotlib
matplotlib.use('Agg')
import numpy as np
import matplotlib.pyplot as plt
# Example data
appsol = ['llolLl', 'nnM', 'lllld bbbblnl', 'x2x', 'foobar', 'EXZ', 'Flups', 'Flaps', 'Foobar Barfooment', 'ABC', 'FABAS', 'common', 'AQT', 'Faberjak', 'simsalsa', 'LESS', 'Wermut']
y_pos = np.arange(len(appsol)) - .3
y_pos_2 = np.arange(len(appsol)) - .1
y_pos_3 = np.arange(len(appsol)) + .1
y_pos_4 = np.arange(len(appsol)) + .3
num_tickets = [4, 4,3,2,6,7,8,1,4,4,3,2,6,7,8,1,9]
num_tickets_2 = [7,6,5,4,3,4,2,1,2,4,1,0,3,0,2,1,0]
num_tickets_3 = [1,2,1,1,1,2,2,3,1,1,2,1,3,1,1,2,3]
num_tickets_4 = [8,7,6,2,13,6,8,9,7,6,5,4,3,6,8,9,12]
bar_width = .2
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
# correct yticks
plt.yticks(y_pos_2, appsol)
plt.barh(y_pos, num_tickets, bar_width, align='center', alpha=0.4, color='r')
plt.barh(y_pos_2, num_tickets_2, bar_width, align='center', alpha=0.4, color='b')
plt.barh(y_pos_3, num_tickets_3, bar_width, align='center', alpha=0.4, color='y')
plt.barh(y_pos_4, num_tickets_4, bar_width, align='center', alpha=0.4, color='g')
plt.yticks(y_pos, appsol)
plt.xlabel('Numbers')
plt.title('Horizontal Bar Chart with table')
# Table
empty_labels = ['' for a in appsol ]
plt.yticks(y_pos_2, empty_labels)
plt.tick_params(\
axis='y', # changes apply to the x-axis
which='both', # both major and minor ticks are affected
left='off', # ticks along the bottom edge are off
right='off', # ticks along the top edge are off
labelbottom='off')
# Adjust layout to make room for the table:
plt.subplots_adjust(left=0.4, bottom=0.2)
cell_text = []
i = len(num_tickets) - 1
for j in num_tickets:
cell_text.append([num_tickets[i], num_tickets_2[i], num_tickets_3[i], num_tickets_4[i]])
i -= 1
row_lables = appsol
column_labels = ['So Huge\nMice', 'Elephants', 'Reptiles', 'Germs']
the_table = ax.table(cellText=cell_text,
rowLabels=row_lables,
colLabels=column_labels,
loc='left')
the_table.set_fontsize(15)
the_table.scale(.5,5.0)
plt.savefig('barh_graph.png')
plt.close('all')
这几乎产生了我想要的,只是表行没有与图形的条对齐。所以我需要一种方法,要么将图表向下移动半个表行,要么将表向上移动半个表行。我该怎么做?
我找到了一个解决方法:调整图形的y边界:
当然,这些数字只适用于这些数据维度。 所以,如果有人有更好的解决方案,我想看看。 为了完整起见,下面是增强版(还更改了字体大小并添加了图例):
这就是它的样子:
我认为,如果创建两个子块并将表添加到左侧子块,而不是从现有的单个子块派生新的子块,则会更容易。如果将表添加到现有子块中,则可以使用
bbox
沿y方向将其从0拉伸到1(完全拉伸)。由于表格有一个标题,将右绘图的ylim
设置为(0,n_项)将使两者正确对齐,并添加一个轻微的偏移量,因为条的偏移量也为0.4(外部条的偏移量+半条宽度)。如果元素的数量发生变化,这将自动工作。如果仔细观察,您可能会注意到这些条从表的顶部到底部,您可以对它们进行一些调整,使它们从表的上下单元格的中心开始。
相关问题 更多 >
编程相关推荐