0

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

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]))

Sign up to request clarification or add additional context in comments.

5 Comments

Please add some description about your answer. It maybe short but useful to understand your answer.
@TheBigH: Hey TheBigH! I need to know how to plot these two data sets in the same figure? That is my main question.
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 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]))
@TheBigH : Thanks that worked. Please put your answer as an answer not as a comment. So I can accept it, also you get points. :)
Done. Sorry, I'm new to stack overflow and still figuring out how things work here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.