问题
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()
回答1:
Position the table outside the axes
You may use loc="right"
to position the table right of the axes. Something like fig.subplots_adjust(right=0.8)
will leave enough space for it.
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()
Put the table in its own axes
You may put the table in a new axes next to the existing one. The advantage is that there is no need to then play with the column width or subplot parameters.
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()
回答2:
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])
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:
来源:https://stackoverflow.com/questions/51606234/move-table-position-matplotlib-python