use*_*355 24 python matplotlib
我没有成功地使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()
Run Code Online (Sandbox Code Playgroud)
And*_*lev 47
AFAIK,您不能仅使用原生功能在绘图上任意放置表格.你可以做的是利用文本渲染的可能性.但是,为了做到这一点,您应该在系统中拥有工作环境.如果你有一个,你应该能够生成如下图:matplotlibmatplotliblatexlatex
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()
Run Code Online (Sandbox Code Playgroud)
结果是:

请记住,这是一个快速的例子; 你应该能够通过播放文本坐标来正确放置桌子.如果您需要更改字体等,请参阅文档.
更新:更多 pyplot.table
根据文档,plt.table为当前轴添加一个表.从显而易见的来源来看,图表上的表位置是根据轴确定的.Y坐标可以用关键字top(上图),upper(上半部分),center(中间),lower(下半部分)和bottom(下图)控制.X协调与控制的关键字left和right.这两个作品的任意组合,例如任何一个top left,center right并且bottom可以使用.
因此,最接近您想要的图表可以使用:
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()
Run Code Online (Sandbox Code Playgroud)
这给了你

希望这可以帮助!
我不确定以前的答案是否满足你的问题,我也在寻找一种方法来添加表格,并找到克服"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)
Run Code Online (Sandbox Code Playgroud)
结果是(我没有发布图像的声誉所以这里是一个链接):http://www.viresco.pl/media/test.png.
请注意,bbox单位已标准化(不是绘图比例).
| 归档时间: |
|
| 查看次数: |
34817 次 |
| 最近记录: |