【问题标题】:How to modify the position of a table如何修改表格的位置
【发布时间】:2019-02-02 19:47:45
【问题描述】:

我可以将表格放在右侧,但结果表格无法完全显示。我尝试使用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()

现在

【问题讨论】:

  • 我看到很多其他人对此重复,这只是答案之一。把它挖出来!
  • 您有不同的选择:使用桌子上的 bbox 重新定位它,使用绘图上的 bbox 使其更小。您还可以创建 2 个不同大小的轴,一个用于绘图,一个用于表格。
  • @Mathieu 我尝试了您提到的选项,但没有运气。是bbox的参数问题吗?我尝试了一些 bbox 设置,还尝试创建 2 个轴。如果我创建 2 个轴,这个数字就会失控......

标签: python matplotlib


【解决方案1】:

想通了。 发布答案。

希望对你有帮助

我混淆了子图的使用。使用子图,它将自动生成一个子图。如果您只想添加一个表,则需要使用“ax2.axis('off')”行。我还用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()

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多