0

From a data file in hdf5 format I am plotting three lines using

if (nbnd<3):
    color=f'C{nbnd}'
    ax.plot(x, y, c=color, lw=2.0, alpha=0.8, label = lbl[nbnd] if nbnd < 3 and i == 0 else None)

I want to grep maximum value of nbnd=2 along Y-axis and write that value on top of the line (corresponding to nbnd=2) with an arrow. A sample is shown in the plot where 0.032 with arrow is manually done while I need this kind of notation in matplotlib automatically.

A similar accepted answer is given here: https://stackoverflow.com/questions/43374920/how-to-automatically-annotate-maximum-value-in-pyplot [https://i.sstatic.net/TPwp7.png] But I could not get it done for my work.

I used

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

ax4.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+5),
            arrowprops=dict(facecolor='black', shrink=0.05),
            )

It gives me below error:

   ax4.annotate('local max', xy=(xmax, ymax), xytext=(xmax, ymax+5),
NameError: name 'xmax' is not defined

enter image description here

4
  • What type are the x and y array you use? Are they list or np.ndarray? Commented Jun 2, 2020 at 10:41
  • 1
    I have a hdf5 format file that I am plotting. I have updated the post. Commented Jun 2, 2020 at 10:46
  • The error NameError: name 'xmax' is not defined indicates that you didn't run above code as written. Clearly xmax gets a value in the third line. Commented Jun 2, 2020 at 11:18
  • What changes I need to do? Commented Jun 2, 2020 at 11:25

0

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.