Zeeshan Hayat
Zeeshan Hayat

Reputation: 431

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

Upvotes: 3

Views: 4408

Answers (2)

SpghttCd
SpghttCd

Reputation: 10860

scatter is just not the tool for plotting lines, it's plot. And it accepts 2D-arrays of x- and y-coordinates, so you don't have to manually iterate over lists. So you would need sth like

plt.plot([a_x_axis, a_alive_for], [[1]*n,[1]*n], 'green')

with n = len(a_x_axis).

However, you could structure your data much better in numpy arrays or pandas dataframes where you can set titles for columns, too. (Is it that, what you wanted to achieve by appending 'process-x' to your data lists...?)

Also, the colors of your markers seem to me not chosen by purpose; if you want to have them the same like the lines you could even leave scatter completely away.

Upvotes: 0

harpan
harpan

Reputation: 8631

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

Upvotes: 1

Related Questions