用matplotlib实现调度算法的可视化

2024-04-26 23:57:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种简单的方法(如果可能的话)来表示cpu上一系列任务的调度,就像幻灯片5here。在

我想有不同的线路,每一个任务,我可以代表到达时间,最后期限等。我想用matplotlib来做,但是现在我不知道什么是一个简单的方法。在


Tags: 方法matplotlib时间代表cpu调度线路幻灯片
1条回答
网友
1楼 · 发布于 2024-04-26 23:57:23

我首先检查matplotlib gallery是否有类似的绘图。这里subplot似乎是合适的,因此从this开始可能是一个选择。 当您想要移除一些脊椎(轴)时,您可以进一步检查this example。在

为了得到填充块,我将使用标准的fill_between或{}调用,并使用相应的数据点,例如this example。在

一个简单的例子可以是:

import matplotlib.pyplot as plt

cpu1_t = [0,1,1,3,3,4,5]
cpu1_p = [1,1,0,0,1,1,0]
cpu2_t = [0,1,1,3,3,4,5]
cpu2_p = [0,0,1,1,0,0,1]

fig = plt.figure()
# plot 1
ax1 = fig.add_subplot(211)
ax1.fill_between(cpu1_t, cpu1_p,0, color='b', edgecolor='k')
ax1.set_ylabel(r'$\tau_1$', size=14, rotation=0)
# plot 2
ax2 = fig.add_subplot(212)
ax2.fill_between(cpu2_t, cpu2_p,0, color='r', edgecolor='k')
ax2.set_ylabel(r'$\tau_2$', size=14, rotation=0)

# customize axis
for ax in [ax1, ax2]:
    ax.set_ylim(0,2)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.yaxis.set_ticks_position('left')
    ax.xaxis.set_ticks_position('bottom')

enter image description here

您可以进一步使用主栅格和次栅格、记号等。 当然,这只是创造这样一个情节的一种可能的方法。在

相关问题 更多 >