将数据框列内容堆叠在一起

问题描述 投票:0回答:1

我这里有一个示例数据框

   Job Number  Job Size  Arrival Time  Runtime  Finish Time  Wait Time
0           1       135           112     1252         1364          0
1           2        43           112     1252         2616       1252
2           3       123           112     1252         3868       2504
OS Size: 24
Memory Size:  1024

有没有一种方法使用 matplotlib 将作业大小列内容堆叠到单个条中,并以操作系统大小和内存大小作为 y 限制?

示例:

___ 1024
24 (OS Size)
135
43
123
___ 0

我尝试添加这样的 ylim: ax.set_ylim = (0, memory_size) 没有任何变化,最大值似乎是所有作业大小的总和

    def mainInput1_window(self):
    self.clearWidgets()
    self.basicWidgetList = []

    self.entry_frame = Frame(root, bg="black")
    self.entry_frame.place(x=50, y=110)
    self.basicWidgetList.append(self.entry_frame)

    # Adding a picture to the input screen
    self.header = Button(root, image=image_4, bg="#000000", relief="flat", borderwidth=0)
    self.header.place(x=0, y=0)
    self.basicWidgetList.append(self.header)

    self.os_sizeLBL = Label(root, text="OS Size", font=('Poppins', 18, 'bold'), bg="#d3a075", height=2, width=19, borderwidth=4, relief="raised")
    self.os_sizeLBL.place(x=90, y=180)
    self.basicWidgetList.append(self.os_sizeLBL)

    self.entry_os_size = Entry(root, font=('Poppins', 30, 'bold'), justify="center", width=18)
    self.entry_os_size.place(x=390, y=180)
    self.basicWidgetList.append(self.entry_os_size)

    self.memory_sizeLBL = Label(root, text="Memory Size", font=('Poppins', 18, 'bold'), bg="#ecd3bf", height=2, width=19, borderwidth=4, relief="raised")
    self.memory_sizeLBL.place(x=90, y=250)
    self.basicWidgetList.append(self.memory_sizeLBL)

    self.entry_memory_size = Entry(root, font=('Poppins', 30, 'bold'), justify="center", width = 18)
    self.entry_memory_size.place(x=390, y=250)
    self.basicWidgetList.append(self.entry_memory_size)

    self.job_numLBL = Label(root, text="Number of Jobs", font=('Poppins', 18, 'bold'), bg="#eec894", height=2, width=19, borderwidth=4, relief="raised")
    self.job_numLBL.place(x=90, y=320)
    self.basicWidgetList.append(self.job_numLBL)

    self.entry_job_num = Entry(root, font=('Poppins', 30, 'bold'), justify="center", width = 18)
    self.entry_job_num.place(x=390, y=320)
    self.basicWidgetList.append(self.entry_job_num)

    self.job_entries = []
    self.job_frame = Frame(self.entry_frame, bg="black")
    self.job_frame.grid(row=6, columnspan=5)
    self.basicWidgetList.append(self.job_frame)

    self.set_jobs_button = Button(root, text="Set Jobs", command=self.create_job_entries, font=('Poppins', 18, 'bold'), width=20, bg="#b8804c", height=2, borderwidth=4, relief="sunken")
    self.set_jobs_button.place(x=330, y=450)
    self.basicWidgetList.append(self.set_jobs_button)

    self.back_button = Button(root, text="Back", command = self.mainscreen,
        font =('Poppins', 18, 'bold'), fg = 'white', width=20, bg = '#4b3621', height = 2, borderwidth = 4, relief = 'sunken')

    self.back_button.place(x = 328, y = 550)
    self.basicWidgetList.append(self.back_button)

def hide_initial_inputs(self):
    self.os_sizeLBL.place_forget()
    self.entry_os_size.place_forget()
    self.memory_sizeLBL.place_forget()
    self.entry_memory_size.place_forget()
    self.job_numLBL.place_forget()
    self.entry_job_num.place_forget()
    self.set_jobs_button.place_forget()
    self.header.place_forget()

def create_job_entries(self):
    try:
        num_jobs = int(self.entry_job_num.get())
        for widget in self.job_frame.winfo_children():
            widget.destroy()
        self.job_entries.clear()

        self.hide_initial_inputs()

        for i in range(num_jobs):
            Label(self.job_frame, text=f"Job {i + 1}", font=('Poppins', 10, 'bold'), bg="#000000", fg="#FFFFFF", width=9, relief="flat").grid(row=i, column=0)
            Label(self.job_frame, text=f"Job Size:", font=('Poppins', 9, 'bold'), bg="#D3A075", width=12, relief="groove").grid(row=i, column=1, padx=0)  # Add space on the right side
            entry_job_size = Entry(self.job_frame, font=('Poppins', 10, 'bold'), justify="center", width=15)
            entry_job_size.grid(row=i, column=2, padx=20)
            Label(self.job_frame, text=f" Arrival Time (ms):", font=('Poppins', 9, 'bold'), bg="#ECD3BF", width=15, relief="groove").grid(row=i, column=3, padx=0)  # Add space on the left side
            entry_arrival = Entry(self.job_frame, font=('Poppins', 10, 'bold'), justify="center", width=15)
            entry_arrival.grid(row=i, column=4, padx=20)
            Label(self.job_frame, text=f"Run Time (ms):", font=('Poppins', 9, 'bold'), bg="#EEC894", width=15, relief="groove").grid(row=i, column=5, padx=0)  # Add space on the left side
            entry_runtime = Entry(self.job_frame, font=('Poppins', 10, 'bold'), justify="center", width=15)
            entry_runtime.grid(row=i, column=6, padx=20)
            self.job_entries.append((entry_job_size, entry_arrival, entry_runtime))

        # Create Compute Wait Times button after job entries are created
        self.compute_button = Button(root, text="Show Summary Table and Memory Map", command=self.summarytable, font=('Poppins', 18, 'bold'), width=30, bg="#b8804c", height=1, borderwidth=4, relief="sunken")
        self.compute_button.place(x=260, y=30)
        self.basicWidgetList.append(self.compute_button)

    except ValueError:
        messagebox.showerror("Error", "Please enter a valid number of jobs.")


def summarytable(self):
    try:
        os_size = int(self.entry_os_size.get())
        memory_size = int(self.entry_memory_size.get())
        jobs = []

        for entry_job_size, entry_arrival, entry_runtime in self.job_entries:
            job_size = int(entry_job_size.get())
            arrival_time = int(entry_arrival.get())
            runtime = int(entry_runtime.get())
            jobs.append({"Job Number": len(jobs) + 1, "Job Size": job_size, "Arrival Time": arrival_time, "Runtime": runtime})

        # Sorting jobs by arrival time
        jobs = sorted(jobs, key=lambda x: x["Arrival Time"])

        current_time = 0
        for job in jobs:
            job["Finish Time"] = max(current_time, job["Arrival Time"]) + job["Runtime"]
            job["Wait Time"] = max(current_time - job["Arrival Time"], 0)
            current_time = job["Finish Time"]

        # Create a dataframe to display the jobs
        df = pd.DataFrame(jobs)
        self.display_table(df)
        print(df)

    except ValueError:
        messagebox.showerror("Error", "Please enter valid integers for OS size, Memory size, and job details.")

def display_table(self, df):
    self.clearWidgets()
    self.basicWidgetList = []

    self.sumtablelabel = Label(root, text="Summary Table", font=('Poppins', 18, 'bold'), bg="#ecd3bf", height=2, width=19,
                                borderwidth=4, relief="raised")
    self.sumtablelabel.place(x=140, y=15)
    self.basicWidgetList.append(self.sumtablelabel)

    self.memmaplabel = Label(root, text="Memory Map", font=('Poppins', 18, 'bold'), bg="#ecd3bf", height=2,
                               width=19,
                               borderwidth=4, relief="raised")
    self.memmaplabel.place(x=600, y=15)
    self.basicWidgetList.append(self.memmaplabel)
    # Add headers

    headers = ["Job Number", "Job Size", "Arrival Time", "Runtime", "Finish Time", "Wait Time"]
    for col_num, header in enumerate(headers):
        header_label = Label(root, text=header, font=('Poppins', 10, 'bold'), bg="#eec894", width=10, borderwidth=4, relief="raised")
        header_label.grid(row=0, column=col_num, padx=(7,3), pady= (85,1))
        self.basicWidgetList.append(header_label)

    # Add rows
    for row_num, row in df.iterrows():
        for col_num, value in enumerate(row):
            cell_label = Label(root, text=value, font=('Poppins', 10, 'bold'), bg="#FFFFFF", width=10, borderwidth=4, relief="raised")
            cell_label.grid(row=row_num + 1, column=col_num, padx=(7,3))
            self.basicWidgetList.append(cell_label)
python pandas dataframe matplotlib graph
1个回答
0
投票

IIUC,你可以使用这样的东西

import matplotlib.pyplot as plt
import pandas as pd

data = {
    "Job Size": [135, 43, 123],
    # other columns...
}

df = pd.DataFrame(data)

os_size = 24
memory_size = 1024

job_sizes = df["Job Size"].to_list()
cumulative_sizes = [0]
for size in job_sizes:
    cumulative_sizes.append(cumulative_sizes[-1] + size)

_, ax = plt.subplots()
for i, size in enumerate(job_sizes):
    ax.bar(1, size, bottom=cumulative_sizes[i], label=f"Job {i + 1}")

ax.bar(1, os_size, bottom=cumulative_sizes[-1], color="red", label="OS")

ax.set_ylim(0, memory_size)

ax.set_ylabel("Memory Size")
ax.set_xticks([])

plt.legend()
plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.