【发布时间】:2016-02-25 19:14:57
【问题描述】:
我从这个问题开始chart and table,解决方案确实将表格放在图表下方。
但是,问题的第二部分是在同一个(打印的)页面上使用多个图表。从上一个问题中提取答案并添加另一个子图表明该表覆盖了以下图表。
tight_layout 没有帮助(实际上使情况变得更糟)。
我确实尝试过保留一行(因此总行数是 3,首先从 0 开始,然后从 2 开始),但这留下了一个很大的丑陋空白,因为表格甚至没有接近填满一整行。
GridSpec 似乎也没有帮助,或者至少我不知道如何让它解决这个问题。
months = [1,2,3,4,5,6,7,8,9,10,11,12]
by_year = [
(2012, (8,6,8,9,1,2,8,9,4,3,2,6)),
(2013, (5,6,2,9,6,2,8,9,4,3,2,6)),
(2014, (7,6,3,9,4,2,8,9,4,3,2,6)),
]
colors = ['r','g','b','y']
import pylab as plt
fig = plt.figure()
ax = plt.subplot2grid((2,1), (0,0))
ax2 = plt.subplot2grid((2,1), (1,0))
#0.9 to leave a small space between groups of bars
import numpy as NP
N = 0.95 / (len(by_year))
cell_text = []
for i, (year, row) in enumerate(by_year):
print i, year, row
idx = NP.arange(0, len(months)) + N * i
# * 0.8 to put a small gap between bars
ax.bar(idx, row, color=colors[i], width=N * 0.8, label=year)
cell_text.append(row)
tbl = ax.table(cellText=cell_text, rowLabels=[x[0] for x in by_year], loc='bottom', colLabels=months)
prop = tbl.properties()
ax.set_xticks([])
lgd = ax.legend(loc='upper left', bbox_to_anchor=(1, 1.0))
fig.show()
【问题讨论】:
标签: python matplotlib charts axis-labels