0

I am able to place the table on the right, but it turns out the table could not display completely. I try to use ax, ax_table=plt.subplots() but it does not go well.

I try to use the "bbox" in the table but I don't quite understand the meaning after I search the Internet.

How do I modify the position of a table and place it on the upper right of the plot (outside the plot)?

import csv
x1=["2018-08-27 15:21:49","2018-08-27 15:21:52","2018-08-27 15:21:53","2018-08-27 15:21:56"]
y1=["5523","3512","6732","3383"]
L=[]
with open("test.csv") as f:
    reader=csv.reader(f)
    for row in reader:
        print(row)
        L.append(row)
    print(L)

fig,ax=plt.subplots()
plt.subplots_adjust(left=0.2, bottom=0.25)

col_lables=['tiestamp','loadingtime']
# row_lables=['row1','row2','row3']
table_val=L
row_colors=['red','gold','green']

##bbox=[left, bottom, width, height]
my_table=plt.table(cellText=table_val,colWidths=[0.3]*5,colLabels=col_lables,
                   colColours=row_colors,loc='right')


plt.title("Static Graph & Table")

x11=[datetime.strptime(d,"%Y-%m-%d %H:%M:%S") for d in x1]
y11=[float(i) for i in y1]

date_format=mdates.DateFormatter("%Y-%m-%d %H:%M:%S")
ax.xaxis.set_major_formatter(date_format)
ax.xaxis.set_major_locator(mdates.DayLocator())

plt.xlabel("Date")
plt.ylabel("Loading time")

plt.plot(x11,y11)
ax.yaxis.set_ticks(y11)
ax.xaxis.set_ticks(x11)

plt.gcf().autofmt_xdate()
# plt.grid()
plt.show()

Right now enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Winnie-c
  • 179
  • 2
  • 13
  • Possible duplicate of [Matplotlib table falls outside plot area](https://stackoverflow.com/questions/27929348/matplotlib-table-falls-outside-plot-area) – Mathieu Aug 28 '18 at 08:52
  • I've seen many others duplicate on this, it's only one of the answers. Dig it up! – Mathieu Aug 28 '18 at 08:52
  • You have different option: use the bbox on the table to reposition it, use the bbox on the plot to make it smaller. You could also create 2 axes, with different size, place one for the plot, one for the table. – Mathieu Aug 28 '18 at 08:53
  • @Mathieu I tried the options you mentioned but with no luck. Is the parameter of bbox issue? I tried a few of bbox set up, and also tried to create 2 axes. If I create the 2 axes, the figure will go out of control... – Winnie-c Aug 28 '18 at 11:01

1 Answers1

1

Figured it out. Posting the answer.

Hope it will help

I mixed up with the usage of the subplots. With using the subplots, it will generate a suplot automatically. If you want to add a table only , you need to use the line "ax2.axis('off')". I also modified the font size with this.

fig,(ax1,ax2)=plt.subplots(1,2,figsize=(10,6))

col_lables=['tiestamp','loadingtime']
# row_lables=['row1','row2','row3']
table_val=L
row_colors=['red','gold','green']

ax2.axis('off')
##bbox=[left, bottom, width, height]
my_table=ax2.table(cellText=table_val,colWidths=[0.3]*4,colLabels=col_lables,
                   colColours=row_colors,loc='best', bbox=[0,0.5,1,0.3])
my_table.set_fontsize(24)
my_table.scale(2,2)

ax1.set_title("Static Graph & Table")

x11=[datetime.strptime(d,"%Y-%m-%d %H:%M:%S") for d in x1]
y11=[float(i) for i in y1]

date_format=mdates.DateFormatter("%Y-%m-%d %H:%M:%S")
ax1.xaxis.set_major_formatter(date_format)
ax1.xaxis.set_major_locator(mdates.DayLocator())

ax1.set_xlabel("Date")
ax1.set_ylabel("Loading time")

ax1.plot(x11,y11)

ax1.yaxis.set_ticks(y11)
ax1.xaxis.set_ticks(x11)

plt.gcf().autofmt_xdate()

# plt.grid()
plt.show()

enter image description here

Winnie-c
  • 179
  • 2
  • 13