移动表格位置matplotlib python

时间:2018-07-31 05:53:00

标签: python matplotlib plot figure

我创建了一个figure,它显示了一个形状,并使用table显示了matplotlib。问题是它是如何产生的。它们彼此重叠。形状是按比例缩放的,所以我不想更改它。我想知道如何更改绘图的整体大小或移动表格的位置。

import matplotlib.pyplot as plt
import matplotlib as mpl

fig, ax = plt.subplots(figsize = (10,6))
ax.axis('equal')
plt.style.use('ggplot')
ax.grid(False)

xy = 0,0
circle = mpl.patches.Circle(xy, 160, lw = 3, edgecolor = 'black', color = 'b', alpha = 0.1, zorder = 5)
ax.add_patch(circle)   

col_labels=['A','B','C','D','E']
row_labels=['diff','total']

table_vals=[['','','','',''],['','','','','']]

the_table = plt.table(cellText=table_vals,
    colWidths = [0.05]*5,
    rowLabels=row_labels,
    colLabels=col_labels,
    bbox = [0.8, 0.4, 0.2, 0.2])

ax.autoscale()
plt.show()

enter image description here

2 个答案:

答案 0 :(得分:1)

在表中添加# Verbosity ifeq ($(V),) Q := @ MQ := --quiet ECHO := echo OUT := &> /dev/null else ifeq ($(V),1) Q := MQ := ECHO := echo OUT := else $(error V: invalid value ($(V))) endif foo: $(Q)$(ECHO) 'making $@' && \ some-command $@ $(OUT) bar: cuz $(Q)$(MAKE) $(MQ) $@ ... 参数。 (而不是loc)

bbox

With BBOX

the_table = plt.table(cellText=table_vals, colWidths = [0.05]*5, rowLabels=row_labels, colLabels=col_labels, bbox = [0.2, 0.4, 0.4, 0.02]) 参数有4个输入:X,Y,Width和Height。因此,X和Y是左下角的坐标。上面的高度太小了。

编辑:创建一个可供玩耍的房间

我们的想法是以相同的方式缩小bbox

ax

编辑2:尝试将表格放在右侧。就像我说的,您需要使用box的值,我花了大约10次尝试来获得这个值。我将spyder用作IDE,因此速度非常快。

box = ax.get_position()
a.set_position([box.x0, box.y0, box.width * 0.9, box.height])

输出:

Table on the right

答案 1 :(得分:0)

将工作台置于轴外

您可以使用loc="right"将工作台置于轴的右侧。 fig.subplots_adjust(right=0.8)之类的东西会留出足够的空间。

import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('ggplot')

fig, ax = plt.subplots(figsize = (10,6))
fig.subplots_adjust(right=0.8)
ax.axis('equal')

ax.grid(False)

xy = 0,0
circle = mpl.patches.Circle(xy, 160, lw = 3, edgecolor = 'black', 
                            facecolor = 'b', alpha = 0.1, zorder = 5)
ax.add_patch(circle)   

col_labels=['A','B','C','D','E']
row_labels=['diff','total']

table_vals=[['','','','',''],['','','','','']]

the_table = plt.table(cellText=table_vals,
          colWidths = [0.05]*5,
          rowLabels=row_labels,
          colLabels=col_labels,
          loc='right', zorder=3)

ax.autoscale()
plt.show()

enter image description here

将桌子放在自己的轴上

您可以将表格放置在现有轴旁边的新轴中。优点是不需要再使用列宽或子图参数。

import matplotlib.pyplot as plt
import matplotlib as mpl
plt.style.use('ggplot')

fig, (ax, ax_table) = plt.subplots(ncols=2, figsize = (10,6),
                                  gridspec_kw=dict(width_ratios=[3,1]))
ax.axis('equal')
ax_table.axis("off")

ax.grid(False)

xy = 0,0
circle = mpl.patches.Circle(xy, 160, lw = 3, edgecolor = 'black', 
                            facecolor = 'b', alpha = 0.1, zorder = 5)
ax.add_patch(circle)   

col_labels=['A','B','C','D','E']
row_labels=['diff','total']

table_vals=[['','','','',''],['','','','','']]

the_table = ax_table.table(cellText=table_vals,
          rowLabels=row_labels,
          colLabels=col_labels,
          loc='center')

ax.autoscale()
plt.show()

enter image description here