首页 > 解决方案 > 如何修改表格的位置

问题描述

我可以将桌子放在右边,但事实证明桌子无法完全显示。我尝试使用ax, ax_table=plt.subplots(),但效果不佳。

我尝试使用表格中的“bbox”,但我在网上搜索后不太明白其中的含义。

如何修改表格的位置并将其放置在绘图的右上角(绘图外)?

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

马上 在此处输入图像描述

标签: pythonmatplotlib

解决方案


弄清楚了。发布答案。

希望它会有所帮助

我混淆了子图的使用。使用子图,它将自动生成一个子图。如果你只想添加一个表,你需要使用“ ax2.axis('off')”行。我还用这个修改了字体大小。

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

在此处输入图像描述


推荐阅读