python - 如何修改表格的位置
问题描述
我可以将桌子放在右边,但事实证明桌子无法完全显示。我尝试使用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()
解决方案
弄清楚了。发布答案。
希望它会有所帮助
我混淆了子图的使用。使用子图,它将自动生成一个子图。如果你只想添加一个表,你需要使用“ 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()
推荐阅读
- android - 我应该将哪个类用于每日通知/提醒 - AlarmManager、JobScheduler、WorkManager?
- maven - 为什么我不能更改项目 facet 的版本?
- python - 如何在 Pandas 中对日期时间列使用条件逻辑?
- javascript - 如何让 mouseenter 函数显示数据
- javascript - 在javascript中将数组拆分为列
- javascript - 如何在突变时使用 urql 更新 grapqhl 缓存,其中初始查询响应不包含所需的 __typename?
- sql - sql按月和按月分组
- java - 不小心将 Google AdView 小部件放入我的应用程序中,现在即使删除它也无法使用
- constructor - 如何解决 Haxe 中的“重复构造函数”错误?
- android - 如何在 Kotlin 中延迟动画?