【问题标题】:Replacing data position in table - Matplotlib替换表中的数据位置 - Matplotlib
【发布时间】: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


【解决方案1】:

我无法真正访问您的数据,但实现您附加的示例非常简单。我首先将所有信息放在 pandas 数据框中,然后使用此 neat trick 允许您将数据框转换为 matplotlib 表。剩下的就是在玩设计。

这是一个代码sn-p:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Create figure
fig, ax = plt.subplots(figsize = (25,2))
fig.patch.set_visible(False)
ax.axis('off')
ax.axis('tight')

# Make some data and put in dataframe
data = [['FC-1', 'NAME', 5, 2021], ['FC-6', 'NAME', 5, 2021]]
df = pd.DataFrame(data, columns=['Fleet code', 'Fleet name', 'Number of visits', 'Year of visit'])

# Create the table from the dataframe:
tab = ax.table(cellText=df.values, colLabels=df.columns, loc='center', cellLoc='center', 
               colColours = ['lightsteelblue', 'lightsteelblue', 'lightsteelblue', 'lightsteelblue'],
               cellColours = [['w','w','w','w'], ['lightgray','lightgray','lightgray','lightgray']])

# Design the table:
# Fonts:
tab.auto_set_font_size(False)
tab.set_fontsize(20)
tab.scale(2, 2)

fig.tight_layout()

plt.show()

这是最终结果:

【讨论】:

    猜你喜欢
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    • 2016-01-06
    • 2022-07-31
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多