问题描述
在使matplotlib表命令正常工作方面没有取得任何成功.这是我想做的事的一个例子:
I'm not having any success in getting the matplotlib table commands to work. Here's an example of what I'd like to do:
任何人都可以提供表格构造代码的帮助吗?
Can anyone help with the table construction code?
import pylab as plt
plt.figure()
ax=plt.gca()
y=[1,2,3,4,5,4,3,2,1,1,1,1,1,1,1,1]
plt.plot([10,10,14,14,10],[2,4,4,2,2],'r')
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[11,12,13,21,22,23,31,32,33]
# the rectangle is where I want to place the table
plt.text(11,4.1,'Table Title',size=8)
plt.plot(y)
plt.show()
推荐答案
AFAIK,您不能任意仅使用本机matplotlib
功能将表放置在matplotlib
图上.您可以做的是利用 latex
文本呈现的可能性.但是,要执行此操作,您的系统中应具有有效的latex
环境.如果您有一个,则应该能够生成如下图:
AFAIK, you can't arbitrarily place a table on the matplotlib
plot using only native matplotlib
features. What you can do is take advantage of the possibility of latex
text rendering. However, in order to do this you should have working latex
environment in your system. If you have one, you should be able to produce graphs such as below:
import pylab as plt
import matplotlib as mpl
mpl.rc('text', usetex=True)
plt.figure()
ax=plt.gca()
y=[1,2,3,4,5,4,3,2,1,1,1,1,1,1,1,1]
#plt.plot([10,10,14,14,10],[2,4,4,2,2],'r')
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[11,12,13,21,22,23,31,32,33]
table = r'''\begin{tabular}{ c | c | c | c } & col1 & col2 & col3 \\\hline row1 & 11 & 12 & 13 \\\hline row2 & 21 & 22 & 23 \\\hline row3 & 31 & 32 & 33 \end{tabular}'''
plt.text(9,3.4,table,size=12)
plt.plot(y)
plt.show()
结果是:
请记住,这是个简单的例子.您应该可以通过使用文本坐标来正确放置桌子.如果您需要更改字体等,也请参考文档.
Please take in mind that this is quick'n'dirty example; you should be able to place the table correctly by playing with text coordinates. Please also refer to the docs if you need to change fonts etc.
更新:有关pyplot.table
UPDATE: more on pyplot.table
根据文档,plt.table
将表添加到当前轴.从源头上很明显,图表上的表格位置是相对于轴确定的. Y
坐标可以用关键字top
(上图),upper
(在上半部分),center
(在中间),lower
(在下半部分)和bottom
(下图). X
坐标由关键字left
和right
控制.两种作品的任意组合,例如top left
,center right
和bottom
中的任何一个都可以使用.
According to the documentation, plt.table
adds a table to current axes. From sources it's obvious, that table location on the graph is determined in relation to axes. Y
coordinate can be controlled with keywords top
(above graph), upper
(in the upper half), center
(in the center), lower
(in the lower half) and bottom
(below graph). X
coordinate is controlled with keywords left
and right
. Any combination of the two works, e.g. any of top left
, center right
and bottom
is OK to use.
因此可以使用以下方法制作出最接近您想要的图形:
So the closest graph to what you want could be made with:
import matplotlib.pylab as plt
plt.figure()
ax=plt.gca()
y=[1,2,3,4,5,4,3,2,1,1,1,1,1,1,1,1]
#plt.plot([10,10,14,14,10],[2,4,4,2,2],'r')
col_labels=['col1','col2','col3']
row_labels=['row1','row2','row3']
table_vals=[[11,12,13],[21,22,23],[31,32,33]]
# the rectangle is where I want to place the table
the_table = plt.table(cellText=table_vals,
colWidths = [0.1]*3,
rowLabels=row_labels,
colLabels=col_labels,
loc='center right')
plt.text(12,3.4,'Table Title',size=8)
plt.plot(y)
plt.show()
这给你
希望这会有所帮助!
这篇关于如何在Matplotlib中的图表上放置表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!