0

How can I make that when I plot a function (based on a np.array) certain values have their coordinates in the plot? I know how to change color and other little things with code lines like:

line1, = plt.plot(t, f, '*-', label='force', color='#4F81BD')  # blue
line2, = plt.plot(t, a, 'o-', label='acceleration', color='#C0504D')  # red

but for example if I have a "peak" in the plot line, I don't know how to make their coordinates to appear in the same plot

1
  • What do you mean "make their coordinates to appear"? Do you mean xticks on the axis? a special marker? A text tag? Something else? Maybe add an illustration of what you're aiming for... Commented Dec 2, 2020 at 6:10

1 Answer 1

2

This code snippet might help you:

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111)

x=[1,2,3,4,5,6,7,8,9,10]
y=[1,1,1,2,10,2,1,1,1,1]
line, = ax.plot(x, y)

ymax = max(y)
xpos = y.index(ymax)
xmax = x[xpos]

#Labeling the graph (ymax+1 is defining the distance from the word to the point)   
ax.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+1))

ax.set_ylim(0,20)
plt.show()

Output:

enter image description here

I hope I could help you out a bit.

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

3 Comments

maybe I'm being exaggerated but OP explicitly says that plot is based on a np.array, so I think you must use it instead of list
You are not exaggerating. Thanks for mentioning that. The above code is indeed not working with 2D, 3D etc. arrays (e.g. [[1, 2, 3], [4, 5, 6]]). But I guess you can modify the code to your likings.
That actually was very helpful, thank you both

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.