目前我有这段代码:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from matplotlib.font_manager import FontProperties
data = np.random.uniform(0, 1, 80).reshape(20, 4)
final_data = [['%.3f' % j for j in i] for i in data]
mpl.style.use('seaborn')
mpl.rc('xtick', labelsize = 7)
mpl.rc('ytick', labelsize = 7)
fig = plt.figure()
fig.subplots_adjust(left=0.1, wspace=0.1)
plt.subplot2grid((1, 4), (0, 0), colspan=3)
table_subplot = plt.subplot2grid((1, 4), (0, 3))
table = plt.table(cellText=final_data, colLabels=['A', 'B', 'C', 'D'], loc='center', cellLoc='center', colColours=['#FFFFFF', '#F3CC32', '#2769BD', '#DC3735'])
table.auto_set_font_size(False)
table.set_fontsize(7)
table.auto_set_column_width((-1, 0, 1, 2, 3))
for (row, col), cell in table.get_celld().items():
if (row == 0):
cell.set_text_props(fontproperties=FontProperties(weight='bold', size=7))
plt.axis('off')
plt.show()
将其作为输出生成:
我知道情节是空的,但我打算在那里添加一些数据,所以我需要记住这一点!我想在表格中再添加一行以有一个标题。这一行应该只在最后三列上,就像这样:
+-----------------+
| Header |
+-----+-----------------+
| A | B | C | D |
+-----+-----+-----+-----+
| ... | ... | ... | ... |
+-----+-----+-----+-----+
| ... | ... | ... | ... |
+-----+-----+-----+-----+
标题行的宽度应与 A、B 和 C 列的宽度总和相匹配。我一直在玩,但我无法得到它......有人可以帮助我吗?
相关分类