使用matplotlib 画带有表格的图

这篇博客介绍了如何使用Python的Matplotlib库在折线图下方添加表格。通过示例代码展示了如何设置颜色、隐藏刻度线、创建表格,并调整图表空间以容纳表格。内容包括数据准备、图形绘制、表格添加以及保存图像的步骤。
摘要由CSDN通过智能技术生成

新收到的需求,画折线图之后下面附上原始数据,于是去搜了下,记录下结果。

使用plot函数画好图片之后,增加表格,其中cellText为2D list,可以用DataFrame格式的数据生成。

ax.axes.xaxis.set_visible(False)

ax.tick_params(left=False)  # 隐藏刻度线

the_table = ax.table(cellText=text,
                      rowLabels=['F', 'Total'],
                      colLabels=t.index.tolist(),
                      loc='bottom')

取其中一个例子copy如下:
 

# importing necessary packagess
import numpy as np
import matplotlib.pyplot as plt


# input data values
data = [[322862, 876296, 45261, 782372, 32451],
        [58230, 113139, 78045, 99308, 516044],
        [89135, 8552, 15258, 497981, 603535],
        [24415, 73858, 150656, 19323, 69638],
        [139361, 831509, 43164, 7380, 52269]]

# preparing values for graph
columns = ('Gokul', 'Kwality', 'Bakhri', 'Arun', 'Amul')
rows = ['%d months' % x for x in (50, 35, 20, 10, 5)]
values = np.arange(0, 2500, 500)
value_increment = 1000

# Adding pastel shades to graph
colors = plt.cm.Oranges(np.linspace(22, 3, 12))
n_rows = len(data)
index = np.arange(len(columns)) + 0.3
bar_width = 0.4

# Initialing vertical-offset for the graph.
y_offset = np.zeros(len(columns))

# Plot bars and create text labels for the table
cell_text = []
for row in range(n_rows):
    plt.bar(index, data[row], bar_width, bottom=y_offset, color=colors[row])
    y_offset = y_offset + data[row]
    cell_text.append(['%1.1f' % (x / 1000.0) for x in y_offset])

# Reverse colors and text labels to display table contents with
# color.
colors = colors[::-1]
cell_text.reverse()

# Add a table at the bottom
the_table = plt.table(cellText=cell_text,
                    rowLabels=rows,
                    rowColours=colors,
                    colLabels=columns,
                    loc='bottom')

# make space for the table:
plt.subplots_adjust(left=0.2, bottom=0.2)
plt.ylabel("Rise in Rs's".format(value_increment))
plt.yticks(values * value_increment, ['%d' % val for val in values])
plt.xticks([])
plt.title('Cost of Milk od diff. brands')

# plt.show()-display graph
# Create image. plt.savefig ignores figure edge and face color.
fig = plt.gcf()
plt.savefig('pyplot-table-original.png',
            bbox_inches='tight',
            dpi=150)
 

 

参考文档:

(1条消息) 使用pandas以及matplotlib画出带有表格的折线图_这般女子的博客-CSDN博客_pandas 折线图

Matplotlib Table in Python With Examples - Python Pool

Matplotlib.pyplot.table() function in Python - GeeksforGeeks

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值