Python Matplotlib 如何仅获取表格 [英] Python Matplotlib how to get table only

查看:44
本文介绍了Python Matplotlib 如何仅获取表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了

一个疑问:我将如何在表格上设置填充,以免在顶部和左侧被切掉?

解决方案

可以将表添加到轴内或轴外的不同位置.这是由 loc 参数决定的.在这种情况下,您似乎不想使工作台位于轴的外部,而位于轴的内部.因此不要使用任何"top", "bottom", "left", "right",但是相反,例如 loc =上中心" .

然后您可以通过 ax.axis("off") 隐藏它自己的轴.为了不使行标题因图形空白而被裁剪,您可以决定不使用 tight layout .

完整示例:

 将matplotlib.pyplot导入为plt将numpy导入为np列=('Last','High','Low','Chg.','Chg.%','Time','T?')行= ['金','银','铜','铝']数据列表= np.random.randint(10,90,size =(len(rows),len(columns)))scatter_x = (1, 2, 3)scatter_y = (1224.53, 1231.76, 1228.70)无花果= plt.figure(1)fig.subplots_adjust(left=0.2,top=0.8,wspace=1)#Table-主表ax = plt.subplot2grid((4,3), (0,0), colspan=2, rowspan=2)ax.table(cellText = data_list,rowLabels =行,colLabels = columns,loc ="upper center")ax.axis("off")#Gold Scatter - 向右小分散plt.subplot2grid((4,3), (0,2))plt.scatter(scatter_x,scatter_y)plt.ylabel('Gold Last')fig.set_size_inches(w=6, h=5)plt.show()

I modified the example code and got the table to work the way I want, however, there's still a box where the graph would go below the table. I want to get rid of that box. Note that the table has 5 rows including column lables and 8 columns including row lables.

The relevant code:

columns = ('Last', 'High', 'Low', 'Chg.', 'Chg. %', 'Time', 'T?')
rows = ['Gold', 'Silver', 'Copper', 'Aluminum']
scatter_x = (1, 2, 3)
scatter_y = (1224.53, 1231.76, 1228.70)
fig = plt.figure(1)
gridspec.GridSpec(4,3)

#Table - Main table
plt.subplot2grid((4,3), (0,0), colspan=2, rowspan=2)
plt.table(cellText=data_list,
          rowLabels=rows,
          colLabels=columns,
          loc='top')
plt.subplots_adjust(left=0.2,top=0.8)
plt.yticks([])
plt.xticks([])

#Gold Scatter - Small scatter to the right
plt.subplot2grid((4,3), (0,2))
plt.scatter(scatter_x, scatter_y)
plt.ylabel('Gold Last')


fig.tight_layout()
fig.set_size_inches(w=6, h=5)
fig_name = 'plot.png'
fig.savefig(fig_name)
plt.show()

And it produces this:

A tack on question: how would I set the padding on the table so it's not cut off at the top and left?

解决方案

The table can be added at different positions in- or outside the axes. This is determined by the loc argument. In this case it seems you do not want to have the table outside the axes, but inside it. Therefore do not use any of "top", "bottom", "left", "right", but instead e.g. loc="upper center".

You can then hide the axes it self, via ax.axis("off"). In order not to have the row headers be cropped by the figure margin, you may decide not to use tight layout.

Complete example:

import matplotlib.pyplot as plt
import numpy as np

columns = ('Last', 'High', 'Low', 'Chg.', 'Chg. %', 'Time', 'T?')
rows = ['Gold', 'Silver', 'Copper', 'Aluminum']

data_list = np.random.randint(10,90, size=(len(rows), len(columns)))
scatter_x = (1, 2, 3)
scatter_y = (1224.53, 1231.76, 1228.70)

fig = plt.figure(1)
fig.subplots_adjust(left=0.2,top=0.8, wspace=1)

#Table - Main table
ax = plt.subplot2grid((4,3), (0,0), colspan=2, rowspan=2)
ax.table(cellText=data_list,
          rowLabels=rows,
          colLabels=columns, loc="upper center")

ax.axis("off")

#Gold Scatter - Small scatter to the right
plt.subplot2grid((4,3), (0,2))
plt.scatter(scatter_x, scatter_y)
plt.ylabel('Gold Last')

fig.set_size_inches(w=6, h=5)
plt.show()

这篇关于Python Matplotlib 如何仅获取表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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