如何在 Matplotlib 中的绘图上放置表格?

发布于 2024-12-21 19:02:46 字数 441 浏览 0 评论 0原文

我没有成功地让 matplotlib 表命令正常工作。这是我想做的一个例子:

任何人都可以帮助表构建代码吗?

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()

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-12-28 19:02:46

AFAIK,您不能仅使用本机 matplotlib 功能在 matplotlib 绘图上任意放置表格。您可以做的是利用latex文本渲染的可能性。然而,为了做到这一点,您的系统中应该有工作latex环境。如果你有的话,你应该能够生成如下图:

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()

结果是:

在此处输入图像描述

请采取请记住,这是一个快速而肮脏的例子;您应该能够通过使用文本坐标来正确放置表格。如果您需要更改字体等,另请参阅文档

更新:有关的更多信息pyplot.table

根据文档plt.表将表格添加到当前轴。从来源来看,很明显,图表上的表格位置是根据轴确定的。 Y 坐标可以通过关键字 top(上图)、upper(上半部分)、center 进行控制(在中心)、lower(在下半部分)和bottom(下图)。 X 坐标由关键字 leftright 控制。两种作品的任意组合,例如top leftcenter rightbottom都可以使用。

因此,可以使用以下方法制作最接近您想要的图表:

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()

这将为您提供

在此处输入图像描述

希望这会有所帮助!

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()

The result is:

enter image description here

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.

UPDATE: more on pyplot.table

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()

And this gives you

enter image description here

Hope this helps!

饭团 2024-12-28 19:02:46

我不确定以前的答案是否满足您的问题,我也在寻找一种向绘图添加表格的方法,并发现了克服“loc”限制的可能性。您可以使用边界框来控制表格的确切位置(这仅包含表格创建,但应该足够了):

        tbl = ax1.table(
            cellText=[["a"], ["b"]],
            colWidths=[0.25, 0.25],
            rowLabels=[u"DATA", u"WERSJA"],
            loc="bottom", bbox=[0.25, -0.5, 0.5, 0.3])

        self.ui.main_plot.figure.subplots_adjust(bottom=0.4)

结果是(我没有发布图像的声誉,所以这里是一个链接): http://www.viresco.pl/media/test.png

请注意,bbox 单位已标准化(不在绘图比例中)。

I'm not sure if the previous answers satisfies your question, I was also looking for a way of adding a table to the plot and found the possibility to overcome the "loc" limitation. You can use bounding box to control the exact position of the table (this only contains the table creation but should be enough):

        tbl = ax1.table(
            cellText=[["a"], ["b"]],
            colWidths=[0.25, 0.25],
            rowLabels=[u"DATA", u"WERSJA"],
            loc="bottom", bbox=[0.25, -0.5, 0.5, 0.3])

        self.ui.main_plot.figure.subplots_adjust(bottom=0.4)

And the result is (I don't have the reputation to post images so here is a link): http://www.viresco.pl/media/test.png.

Note that the bbox units are normalized (not in plot scale).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文