在matplotlib中创建表 [英] Creating tables in matplotlib

查看:96
本文介绍了在matplotlib中创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用matplotlib制作表,并且设法获取了数据,但是我正在为最终的格式而苦苦挣扎.我需要编辑图形的大小以包含我的所有数据,因为某些数据会被砍掉.这是我当前的代码:

I'm trying to make a table using matplotlib and I've managed to get my data in but I'm struggling with the final formatting. I need to edit the size of the figure to include all my data as some is getting chopped off. Here is my current code:

for struct, energy, density in clust_data:
    fig=plt.figure()
    ax = plt.gca()
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    colLabels=("Structure", "Energy", "Density")
    rows=len(clust_data)
    cellText=[]
    for row in clust_data:
        cellText.append(row)
    the_table = ax.table(cellText=cellText,
              colLabels=colLabels,
              loc='center')
    plt.savefig("table.png")

哪个创建了一个这样的表(我也不完全确定如何通过某些行来获得这些行):

Which creates a table like so (I'm not completely sure how to get ride of the lines through certain rows either):

任何帮助将不胜感激!

推荐答案

您应该能够通过以下操作解决问题:

You should be able to solve your problems doing the following:

  • 图形尺寸(编辑):

  • 测量一个单元格的高度和宽度(例如hcell=0.3wcell=1)
  • 获取/了解行数和列数(以您的情况为len(clust_data)+1和3)
  • 以正确的尺寸创建图形(您可能需要一些额外的填充)

  • Measure how high and wide is a cell (e.g. hcell=0.3, wcell=1)
  • Get/know the number of rows and columns (in your case len(clust_data)+1 and 3)
  • create the figure with the correct size (you might want some extra padding)

fig = plt.figure(figsize=(3*wcell+wpad, nrows*hcell+hpad))

两行中的线是轴刺.

ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)

仅隐藏轴标签和刻度,而不隐藏轴刺. 您必须隐藏它们或将它们涂成白色

just hide the axis labels and ticks, not the axes spines. You have to hide them or colour them in white

在下面查看完整的解决方案

无论如何:在我看来,您正在做很多无用的操作. 从您的代码中,在我看来clust_data已经是具有正确形状的列表的列表,并且cellText在填充后将与clust_data相同.
此外,尽量不要混合使用matplotlib的OO和pyplot接口.

In any case: it looks to me that you are doing a whole lot of useless operations. From your piece of code it seems to me that clust_data is already a list of lists with the correct shape and that cellText after being filled is going to be the same of clust_data.
Furthermore, try not to mix the OO and pyplot interface of matplotlib.

以下代码应与您的代码等效

The following code should be equivalent to yours

fig=plt.figure()
ax = fig.add_subplot(111)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
colLabels=("Structure", "Energy", "Density")
the_table = ax.table(cellText=clust_data,
          colLabels=colLabels,
          loc='center')
plt.savefig("table.png")


完整解决方案

复杂的方式

您必须隐藏轴的刺(例如将其颜色设置为白色),并为其赋予较低的zorder,然后添加具有较高的zorder


full solution

Convoluted way

You have to hide the axes spines (e.g. setting their color white) and give them low zorder then add the table with higher zorder

colLabels=("Structure", "Energy", "Density")
nrows, ncols = len(clust_data)+1, len(colLabels)
hcell, wcell = 0.3, 1.
hpad, wpad = 0, 0    

fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
#remove axis ticks and labels
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
#hide the spines
for sp in ax.spines.itervalues():
    sp.set_color('w')
    sp.set_zorder(0)
#do the table
the_table = ax.table(cellText=clust_data,
          colLabels=colLabels,
          loc='center')
#put the table in front of the axes spines 
#for some reason zorder is not a keyword in ax.table
the_table.set_zorder(10)
plt.savefig("table.png")

简单方法(信用@JoeKington)

只需关闭轴

Simple way (credit @JoeKington)

Just switch off the axis

colLabels=("Structure", "Energy", "Density")
nrows, ncols = len(clust_data)+1, len(colLabels)
hcell, wcell = 0.3, 1.
hpad, wpad = 0, 0    
fig=plt.figure(figsize=(ncols*wcell+wpad, nrows*hcell+hpad))
ax = fig.add_subplot(111)
ax.axis('off')
#do the table
the_table = ax.table(cellText=clust_data,
          colLabels=colLabels,
          loc='center')
plt.savefig("table.png")

这篇关于在matplotlib中创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆