I want to plot two data point categories in the same matplotlib
plot using two different colours.
Following code plot one category of data points with labels.
import matplotlib
import pylab as plt
x = [-0.39615277,-0.31426806,-0.17823952,-0.43836375,-0.26388058,-0.52400482, -0.26388058, -0.32637322]
y = [0.28005737,0.44953214, 0.26899154, 0.36850831, -0.34592143, -0.24640466, -0.34592143, -0.45966878]
n=['romeo','juliet','happy','dagger','live','die','free','hampshier']
fig, ax = plt.subplots()
ax.scatter(x, y)
for i, txt in enumerate(n):
ax.annotate(txt, (x[i],y[i]))
matplotlib.pyplot.show()
But I have following data set which has to be plotted in the same figure with a different colour.
x1 = [-0.31086574,-0.40733041,-0.59446137,-0.60304575,-0.1428143]
y1 = [0.36293322,0.54074246, 0.20005441, -0.6953914, -0.22866156]
n1=['d1','d2','d3','d4','d5']
How to achieve this?
enter image description here
1 Answer 1
Note: this post has been edited to remove my first unhelpful answer and replace it with a proper one. New here, sorry. :)
Plotting two scatter plots will plot them on the same figure. So where you have ax.scatter(x,y)
, put the second scatter plot ax.scatter(x1,y1,c='red')
directly afterwards and it will be on the same figure. Later you can annotate the second set of points with
for i, txt in enumerate(n1):
ax.annotate(txt, (x1[i],y1[i]))
-
Please add some description about your answer. It maybe short but useful to understand your answer.Rachcha– Rachcha2014年02月25日 04:38:34 +00:00Commented Feb 25, 2014 at 4:38
-
@TheBigH: Hey TheBigH! I need to know how to plot these two data sets in the same figure? That is my main question.Nilani Algiriyage– Nilani Algiriyage2014年02月25日 04:42:54 +00:00Commented Feb 25, 2014 at 4:42
-
Sorry, I thought you were asking how to plot the points in different colors. Plotting two scatter lots will plot them on the same figure. So where you have
ax.scatter(x,y)
, put the second scatter plotax.scatter(x1,y1,c='red')
directly afterwards and it will be on the same figure. Later you can annotate the second set of points withfor i, txt in enumerate(n1): ax.annotate(txt, (x1[i],y1[i]))
TheBigH– TheBigH2014年02月25日 04:52:06 +00:00Commented Feb 25, 2014 at 4:52 -
@TheBigH : Thanks that worked. Please put your answer as an answer not as a comment. So I can accept it, also you get points. :)Nilani Algiriyage– Nilani Algiriyage2014年02月25日 06:00:33 +00:00Commented Feb 25, 2014 at 6:00
-
Done. Sorry, I'm new to stack overflow and still figuring out how things work here.TheBigH– TheBigH2014年02月25日 06:06:43 +00:00Commented Feb 25, 2014 at 6:06