I want to give each x Point a Label. From 0 to Inf.
The Label should be visible at the point, that is the highest.
Function:
JavaScript
x
12
12
1
def plot_pics(self, figure,title, x, a1, a2, a3, labelx, labely):
2
3
ax = figure.add_subplot(111)
4
ax.plot(x,a1,'-o')
5
ax.plot(x,a2,'-o')
6
ax.plot(x,a3,'-o')
7
ax.legend(['left region','center region','right region'])
8
ax.set_xlabel(labelx)
9
ax.set_ylabel(labely)
10
ax.set_title(title)
11
figure.canvas.draw_idle()
12
Advertisement
Answer
Here is a minimal working example of what I think you want to achieve.
JavaScript
1
18
18
1
import numpy as np
2
import matplotlib.pyplot as plt
3
4
# Random plotting data
5
x_arr = np.arange(10)
6
rand_arr = np.random.random((10, 3))
7
8
# Plot everything
9
plt.plot(x_arr, rand_arr, '-o')
10
11
# Find maximas for every x-value
12
rand_max_arr = np.max(rand_arr, axis=1)
13
x_offset = 0.5
14
y_offset = 0.04
15
for x, y in zip(x_arr, rand_max_arr):
16
plt.text(x - x_offset, y + y_offset, "point {:d}".format(x), bbox=dict(facecolor="white"))
17
plt.show()
18
It generates the following plot.
For testing purposes I create 3 arrays of 10 random numbers each. Afterwards you have to find the maximum for each x-point and attach a text to the point via plt.text()
, whereas the coordinates are the x-point and the found maximum. The offsets are used to move the text so it does only minimally interfere with the plotted maximas themselves.