1

Sorry, I'm a beginner and have no clue about plotting. I have four points: a, b, c and d. How do I plot them using matplotlib?

a = [1, 2]
b = [2, 4]
c = [2.5, 4]
d = [4.5, 5]

Thanks for your help!

2 Answers 2

2

The accepted answer is perfectly correct. Another possible way that I personally find a little cleaner would be to use Numpy to transpose the datapoints.

import numpy as np
import matplotlib.pyplot as plt

a = [1, 2]
b = [2, 4]
c = [2.5, 4]
d = [4.5, 5]

l = np.array([a,b,c,d]) #[[1,2], [2,4], [2.5,4], [4.5,5]]
datapoints = l.T        #[[1, 2, 2.5, 4.5], [2, 4, 4, 5]]

plt.scatter(datapoints[0], datapoints[1])

plt.show()

Even cleaner than this would be to do

x,y = l.T
plt.scatter(x, y)
Sign up to request clarification or add additional context in comments.

Comments

1

Easy, use plt.scatter():

import matplotlib.pyplot as plt

a = [1, 2]
b = [2, 4]
c = [2.5, 4]
d = [4.5, 5]

l = [a,b,c,d]

plt.scatter([i[0] for i in l], [i[1] for i in l])

plt.show()

Output:

enter image description here

2 Comments

Thanks for your help. How do I label the points a, b, c, d?
You can use plt.text to label points, e.g. to label a you can do plt.text(a[0], a[1], 'a'). You can also wrap this in a loop if you create a list of names

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.