Home > Back-end >  Python Matplotlib -> give each x axis a nummeric Label
Python Matplotlib -> give each x axis a nummeric Label

Time:10-25

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:

   def plot_pics(self, figure,title, x, a1, a2, a3, labelx, labely):
   
     ax = figure.add_subplot(111)
     ax.plot(x,a1,'-o')
     ax.plot(x,a2,'-o')
     ax.plot(x,a3,'-o')
     ax.legend(['left region','center region','right region'])
     ax.set_xlabel(labelx)
     ax.set_ylabel(labely)
     ax.set_title(title)
     figure.canvas.draw_idle()

Plot

CodePudding user response:

Here is a minimal working example of what I think you want to achieve.

import numpy as np
import matplotlib.pyplot as plt

# Random plotting data
x_arr = np.arange(10)
rand_arr = np.random.random((10, 3))

# Plot everything
plt.plot(x_arr, rand_arr, '-o')

# Find maximas for every x-value
rand_max_arr = np.max(rand_arr, axis=1)
x_offset = 0.5
y_offset = 0.04
for x, y in zip(x_arr, rand_max_arr):
    plt.text(x - x_offset, y   y_offset, "point {:d}".format(x), bbox=dict(facecolor="white"))
plt.show()

It generates the following plot. enter image description here

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.

  • Related