gpt4 book ai didi

python - 在python中绘制多进程图

转载 作者:行者123 更新时间:2023-11-30 22:15:16 25 4
gpt4 key购买 nike

我试图以线条的形式针对每个进程名称绘制多个进程数据。一条线代表进程处于事件状态的时间,间隙代表进程空闲的时间。

x_axis_data1 = [1,2,3,4,5,6,7,8]
x_axis_data2 = [1,2,3,4,5,6,7,8]
x_axis_data3 = [1,2,3,4,5,6,7,8]

alive_for_time = 6
sleep_for_time = 2

plt.plot(x_axis_data1, "process1", dashes = [alive_for_time,
sleep_for_time])
plt.plot(x_axis_data2, "process2", dashes = [alive_for_time,
sleep_for_time])
plt.plot(x_axis_data3, "process3", dashes = [alive_for_time,
sleep_for_time])


plt.show()

我不知道如何在 pyplot 中绘制字符串。现在,我遇到了这个错误:

ValueError: Illegal format string "process1"; two marker symbols   

最佳答案

您应该使用 label 关键字和 legend()功能。

一些评论:

  • label 是一个关键字,用于描述最后调用 legend() 函数时将出现的字符串。
  • 破折号不是表示您想要的内容的好方法,因为它们位于“图形单位”(像素),而您很可能需要“数据单位”(时间)。尝试缩放图表,您会发现破折号不会相应缩放。
import matplotlib.pyplot as pltx_axis_data1 = [1,2,3,4,5,6,7,8]x_axis_data2 = [1,2,3,4,5,6,7,8]x_axis_data3 = [1,2,3,4,5,6,7,8]alive_for_time = 6sleep_for_time = 2plt.plot(x_axis_data1, [1] * len(x_axis_data1), dashes = [alive_for_time,sleep_for_time], label="process1")plt.plot(x_axis_data2, [2] * len(x_axis_data2), dashes = [alive_for_time, sleep_for_time], label="process2")plt.plot(x_axis_data2, [3] * len(x_axis_data3), dashes = [alive_for_time, sleep_for_time], label="process3")plt.legend()plt.show()

enter image description here

关于python - 在python中绘制多进程图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50331539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com