Python 在 matplotlib 中创建表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17232683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating tables in matplotlib
提问by Jsg91
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:
我正在尝试使用 matplotlib 制作一个表格,并且我已经设法输入了我的数据,但是我正在为最终的格式而苦苦挣扎。我需要编辑图形的大小以包含我的所有数据,因为有些数据被切断了。这是我当前的代码:
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):

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

Any help is greatly appreciated!
任何帮助是极大的赞赏!
采纳答案by Francesco Montesano
You should be able to solve your problems doing the following:
您应该能够通过执行以下操作来解决您的问题:
Figure size (edit):
- 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)+1and 3) create the figure with the correct size (you might want some extra padding)
fig = plt.figure(figsize=(3*wcell+wpad, nrows*hcell+hpad))
- Measure how high and wide is a cell (e.g.
The lines within the two rows are the axes spines.
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
图大小(编辑):
- 测量单元格的高度和宽度(例如
hcell=0.3,wcell=1) - 获取/知道行数和列数(在您的情况下为
len(clust_data)+13) 创建具有正确大小的图形(您可能需要一些额外的填充)
fig = plt.figure(figsize=(3*wcell+wpad, nrows*hcell+hpad))
- 测量单元格的高度和宽度(例如
两行内的线是轴脊。
ax.xaxis.set_visible(False) ax.yaxis.set_visible(False)只需隐藏轴标签和刻度,而不是轴脊。您必须隐藏它们或将它们涂成白色
see full solution below
请参阅下面的完整解决方案
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_datais already a list of lists with the correct shape and that cellTextafter being filled is going to be the same of clust_data.
Furthermore, try not to mix the OO and pyplot interface of matplotlib.
无论如何:在我看来,您正在做很多无用的操作。从你的一段代码来看,在我看来这clust_data已经是一个具有正确形状的列表列表,cellText填充后将与clust_data.
此外,尽量不要混合使用 matplotlib 的 OO 和 pyplot 接口。
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")
Edit: full solution
编辑:完整的解决方案
Convoluted way
曲折的方式
You have to hide the axes spines (e.g. setting their color white) and give them low zorderthen add the table with higher zorder
您必须隐藏轴刺(例如将它们的颜色设置为白色)并将它们设置为低zorder然后添加更高的表格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")
Simple way (credit @JoeKington)
简单的方法(信用@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")
回答by Alejandro
It is just a curiosity. You can print your table from latex. If you try this code,
这只是一种好奇。你可以用乳胶打印你的桌子。如果您尝试此代码,
import matplotlib.pyplot as plt
import numpy as np
table = r'\begin{table} \begin{tabular}{|l|l|l|} \hline $\alpha$ & $\beta$ & $\gamma$ \ \hline 32 & $\alpha$ & 123 \ \hline 200 & 321 & 50 \ \hline \end{tabular} \end{table}'
plt.plot(np.arange(100))
plt.text(10,80,table, size=50)
plt.show()
you will see a beatifull table in the top left of the plot. Now, it is almost straight-forward to write a function to transform your data into a string like the previous latex table.
你会在图的左上角看到一个漂亮的表格。现在,编写一个函数来将数据转换为像之前的乳胶表一样的字符串几乎是直接的。

