python - Matplotlib scatter plot with 2 y-points per x-point -


i want create scatter plot using matplotlib.

i have array of ids x-axis. looks this:

x = [1, 2, 3, ...] #length = 418 

for y-axis, have array of tupples. looks this:

y = [(1, 1), (1, 0), (0, 0), (0, 1), ...] #length = 418 

i plot in scatter plot each id (on x-axis) shows red dot first value of corresponding tupple , blue dot second value of correspond tuple.

i tried code:

plt.plot(x, y, 'o') plt.xlim(xmin=900, xmax=1300) plt.ylim(ymin=-1, ymax=2) plt.yticks(np.arange(2), ('class 0', 'class 1')) plt.xlabel('id') plt.ylabel('class') plt.show() 

it shows result:

my_plot

how can make plot more clear?

also, when zoom in, notice points offset? how can place them directly above each other?

zoomed_plot

you need store data in 2 lists/tuples instead of 1 list of two-item tuples, because plt.plot needs input. best way reorganize data this, if stuck list of tuples (or need data format other reasons), can create lists on go:

plt.plot(x, [i (i,j) in y], 'ro') plt.plot(x, [j (i,j) in y], 'bo') 

note if want 2 different data lines 2 different colors, need 2 plt.plot(..) lines.

to avoid markers overlapping each other, either can change shape or size (or both):

plt.plot(x, [i (i,j) in y], 'rs', markersize = 12 ) plt.plot(x, [j (i,j) in y], 'bo', markeresize = 8) 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -