Here's an example of a matplotlib function that takes in the start and end times of a task, as well as the thread number, and plots each task as a rectangle with the x-axis being time and the y-axis being the thread number:
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def plot_tasks(tasks, threads):
"""
Plots tasks as rectangles on a matplotlib figure.
Parameters:
tasks (list): A list of tuples, where each tuple contains the start and end time of a task.
e.g. [(1, 5), (6, 8), (10, 15)]
threads (list): A list of integers representing the thread number on which the task was executed.
e.g. [1, 2, 1]
"""
# Create a new figure and a new axes.
fig, ax = plt.subplots()
# Set the labels for the x and y axes.
ax.set_xlabel("Time")
ax.set_ylabel("Thread")
# Set the x and y limits of the plot.
ax.set_xlim(0, max(max(task) for task in tasks))
ax.set_ylim(0, max(threads) + 1)
# Iterate through each task and plot a rectangle.
for i, (start, end) in enumerate(tasks):
rect = patches.Rectangle((start, threads[i]), end - start, 1, linewidth=1, edgecolor='r', facecolor="none")
ax.add_patch(rect)
# Show the plot.
plt.show()
You can use this function by passing in a list of tuples representing the start and end times of each task, and a list of integers representing the thread number on which each task was executed.
For example:
tasks = [(1, 5), (6, 8), (10, 15)]
threads = [1, 2, 1]
plot_tasks(tasks, threads)
This will create a plot with three rectangles representing the three tasks, with the x-axis being time, and the y-axis being the thread number.
Note: the above example uses rectangles with no fill color, if you want to use fill color for the rectangles you can change the facecolor parameter to the desired color.