Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plot data points in python using pylab

enter image description hereI have data which I have to plot

X = [0,1,2,3,4,5] Y = [6,7,8,9,10,11,12,13,14,15]

X belongs to class1, so I would want them to be plotted green in color and Y belongs to class2, so I would want them to be plotted blue in color.

What I did was,

import pylab as pl
pl.plot(X,'go')
pl.plot(Y,'bo')
pl.show()

But this is plotting X against Y. All I want to display in my graph is just the points X and Y in green and blue colors respectively.

How can I accomplish this?

like image 581
user1946217 Avatar asked Nov 04 '22 00:11

user1946217


1 Answers

It doesn't plot X against Y, if only because X and Y are not the same length. Let's say x is the variable (horizontal axis) and y the result (vertical axis). Normally you write pl.plot(x,y), where x and y are lists of the same length. If you give only one list, it assumes you only gave y and matplotlib will make an x-axis for you, something like this:

import pylab as pl

y1 = [0,1,2,3,4,5] 
y2 = [6,7,8,9,10,11,12,13,14,15]

x1 = range(len(y1))
x2 = range(len(y2))

pl.plot(x1, y1,'go')
pl.plot(x2, y2,'bo')
pl.show()

So I think in your case you should define the x-axis.

like image 118
Robbert Avatar answered Nov 15 '22 11:11

Robbert