Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move table position matplotlib python

I have created a figure that displays a shape and table using matplotlib. The problem is how its produced. They overlap each other. The shape is to scale so I don't want to alter it. I was wondering how I can alter the overall size of the plot or move the position of the table.

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


1 Answers

Add the bbox argument with your table. (instead of loc)

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

With BBOX

The bbox argument takes 4 inputs: X, Y, Width, Height. Thus X and Y are the coordinates of the bottom left corner. Above, the height was far too small.

EDIT: Create room to play with

The idea is to make the ax smaller in the same manner.

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

EDIT 2: Trying to put the table on the right. As I said, you need to play with the box values, took me about 10 tries to get this. I'm using spyder as an IDE, so it's really fast.

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 = [1.1, 0.5, 0.35, 0.1])

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

ax.autoscale()
plt.show()

Output:

Table on the right

like image 107
Mathieu Avatar answered Jun 08 '26 23:06

Mathieu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!