简体   繁体   中英

Adding a crosshair or marker to a matplotlib contour plot

I'm plotting a NumPy array as a contour plot using matplotlib:

import numpy as np
import matplotlib.pyplot as plt

plt.contour(array, linewidths = 1, colors = 'k')
plt.contourf(array, cmap = plt.cm.jet)
plt.colorbar()
plt.show()

I would like to add a 'crosshair' or another marker to denote the maximum value in the array which is given by:

maxi = np.max(array)

How should I go about doing this?

You can simply plot the cross if you know the position.

[row, col] = numpy.where(array==np.max(array))
plt.plot(col, row, 'b+')

To change the markersize check this .

I added code to show a cross hair for the b+ marker using 6 increments for the xs and ys vertical and horizontal lines

b1=1.8027335249990852
xs=[4]*6
ys=np.linspace(0,int(4*b1),6)
ys2=[int(4*b1)]*6
xs2=np.linspace(0,4,6)

plt.plot(xs, ys,'k-', linestyle = ":", lw=1)
plt.plot(xs2, ys2,'k-', linestyle = ":", lw=1)
plt.plot(4, 4*b1, 'b+')
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM