【发布时间】:2021-08-24 22:37:41
【问题描述】:
我正在尝试在 matplotlib 中创建一个具有以下结果的表,数据是从 MysQl 中提取的。
我在一个由 PYQT5 构建的应用程序中使用了这些图表
但不幸的是,这就是我得到的:
使用的代码:
def dashboard_fleet_statistics_fleets_visisted_current_year(self):
try:
mydb = con.connect(host= "localhost", user ="root", password='''''', db="fleet")
cursor = mydb.cursor()
cursor.execute('''SELECT (fleet_code) ,fleet_name,COUNT(fleet_code) AS "No Of Visits",(Year(date_of_visit)) AS "Year Of Visit"
FROM vehicle_tyre_parameters
WHERE fleet_code != "" AND Year(date_of_visit)= Year(curdate())
GROUP BY fleet_code''')
result4 = cursor.fetchall()
print(list(result4))
fleet_code=[]
fleet_name=[]
no_of_visits =[]
fleet_year=[]
for row in result4 :
fleet_code.append(row[0])
fleet_name.append(row[1])
no_of_visits.append(row[2])
fleet_year.append(row[3])
print(list(fleet_code))
print(list(fleet_name))
print(list(no_of_visits))
print(list(fleet_year))
fig, ax = plt.subplots()
values=[fleet_code,fleet_name,no_of_visits,fleet_year]
table = ax.table(cellText=values,rowLabels=['Fleet Code','Fleet Name','No of Visits','Year of Visit'] ,colWidths=[.5,.5],colLoc='center',loc='center',bbox=[-0.3, 1, 1, 0.275])
#modify table
table.set_fontsize(14)
table.scale(1,4)
ax.axis('off')
table[(1, 0)].set_facecolor("white")
table[(2, 0)].set_facecolor("gray")
plt.tight_layout()
#display table
plt.show()
感谢您帮助获得一张带有第一张图片的桌子!谢谢
【问题讨论】:
-
@lior 这是我从 mysql 服务器获得的数据: 获取 mysql 的结果:[('FC-1', 'Guirguis', 5, 2021), ('FC-6', ' Mohamed ', 1, 2021)] 循环获取数据,以下是循环的结果:- ['FC-1', 'FC-6'] ['Guirguis', 'Mohamed'] [5, 1] [2021, 2021]
标签: python matplotlib