Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib connecting the dots in scatter plot

I am trying to visualize some data regarding the time at which the process was running or alive and the time it was idle. For each process, I have a_x_axis the time at which process started running and a_live_for is the time it was alive after it woke up. I have two data points in for each process. I am trying to connect these two dots by a line by connecting 1st green dot with the first red dot and second green dot with the second red dot and so on, so I can see alive and idle time for each process in the large data set. I looked into scatter plot examples but could not find any way to solve this issue.

import matplotlib.pyplot as plt

a_x_axis = [32, 30, 40, 50, 60, 78]
a_live = [1, 3, 2, 1, 2, 4]

a_alive_for = [a + b for a, b in zip(a_x_axis, a_live)]

b_x_axis = [22, 25, 45, 55, 60, 72]
b_live = [1, 3, 2, 1, 2, 4]
b_alive_for = [a + b for a, b in zip(b_x_axis, b_live)]

a_y_axis = []
b_y_axis = []

for i in range(0, len(a_x_axis)):
    a_y_axis.append('process-1')
    b_y_axis.append('process-2')


print("size of a: %s" % len(a_x_axis))
print("size of a: %s" % len(a_y_axis))
plt.xlabel('time (s)')
plt.scatter(a_x_axis, [1]*len(a_x_axis))
plt.scatter(a_alive_for, [1]*len(a_x_axis))

plt.scatter(b_x_axis, [2]*len(b_x_axis))
plt.scatter(b_alive_for, [2]*len(b_x_axis))

plt.show()

enter image description here

like image 925
Zeeshan Hayat Avatar asked May 09 '26 07:05

Zeeshan Hayat


1 Answers

You need:

import matplotlib.pyplot as plt

a_x_axis = [32, 30, 40, 50, 60, 78]
a_live = [1, 3, 2, 1, 2, 4]

a_alive_for = [a + b for a, b in zip(a_x_axis, a_live)]

b_x_axis = [22, 25, 45, 55, 60, 72]
b_live = [1, 3, 2, 1, 2, 4]
b_alive_for = [a + b for a, b in zip(b_x_axis, b_live)]

a_y_axis = []
b_y_axis = []

for i in range(0, len(a_x_axis)):
    a_y_axis.append('process-1')
    b_y_axis.append('process-2')


print("size of a: %s" % len(a_x_axis))
print("size of a: %s" % len(a_y_axis))
plt.xlabel('time (s)')
plt.scatter(a_x_axis, [1]*len(a_x_axis))
plt.scatter(a_alive_for, [1]*len(a_x_axis))

plt.scatter(b_x_axis, [2]*len(b_x_axis))
plt.scatter(b_alive_for, [2]*len(b_x_axis))

for i in range(0, len(a_x_axis)):
    plt.plot([a_x_axis[i],a_alive_for[i]], [1,1], 'green')

for i in range(0, len(b_x_axis)):
    plt.plot([b_x_axis[i],b_alive_for[i]], [2,2], 'green')

plt.show()

Output:

enter image description here

like image 93
harvpan Avatar answered May 10 '26 22:05

harvpan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!