更改显示在 matplotlib 图右上角的值

Changing values displayed in top right corner of matplotlib figure

如何更改matplot图形右上角显示的值?默认情况下,它显示当前光标位置的坐标,但我更喜欢它显示当前 x 光标坐标的显示数据值。我在附图中标记了这些值。 diagram

这是一个简单的代码。请告诉我如何解决此示例中描述的问题:

import numpy as np
from matplotlib import pyplot as plt
import soundfile as sf

data_mono = []
x_click = 0

# Loding an audio signal
data, sps = sf.read("stereo_v2.wav")

# Signal loaded by sf.read() is stereo (after plotting there are going to be
# 2 values for every sample) so we need to make it mono. This is what a loop
# down below is doing.
for i in range(0,len(data)):
    bufor = ((data[i][0] + data[i][1])/2)
    data_mono.append(bufor)

fig, ax = plt.subplots()
ax = plt.plot(data_mono)

# Below function is creating a vertical line in a place of left mouse click.
# Right click is deleting recently created line.
def click(event):
    global x_click
    x_click = event.xdata
    if event.button == plt.MouseButton.LEFT:
        global line
        line = plt.axvline(event.xdata)
    elif event.button == plt.MouseButton.RIGHT:
        plt.gca().lines[-1].remove()

# "Connecting" the cursor to above function.
cid = fig.canvas.mpl_connect('button_press_event', click)

# Loop displaying the plot and refreshing it every 0.05 second. When x coordinate
# of registered click is bigger than 0.9999 of the maximal x value loop
# will break.
while True:
    plt.pause(0.05)
    if x_click >= (len(data_mono))*0.9999:
        break

您必须检索 x-value 并将其用作声音数据列表的索引。由于您必须检查索引的有效性,我们现在定义一个函数 NavigCoordin():

import numpy as np
from matplotlib import pyplot as plt
import soundfile as sf 


data_mono = []
x_click = 0

# Loding an audio signal
data, sps = sf.read("test.wav")

# Signal loaded by sf.read() is stereo (after plotting there are going to be
# 2 values for every sample) so we need to make it mono. This is what a loop
# down below is doing.
for i in range(0,len(data)):
    bufor = ((data[i][0] + data[i][1])/2)
    data_mono.append(bufor)

fig, ax = plt.subplots(figsize=(15, 4))
#don't overwrite your axis object - you need it later on
ax.plot(data_mono) 

#the function to determine the x-index and the corresponding y-value from the array 
#and return a string depending on the x-value
def NavigCoordin(x, y):
    if x>=0 and x<=len(data_mono):
        return f"x: {int(x+0.5)}, y: {data_mono[int(x+0.5)]:.4f}"
    else:
        return "outside the range"

#link the function to the axis object
ax.format_coord = NavigCoordin

# Below function is creating a vertical line in a place of left mouse click. 
# Right click is deleting recently created line.
def click(event):
    global x_click
    x_click = event.xdata
    if event.button == plt.MouseButton.LEFT:
        global line
        line = ax.axvline(event.xdata)
    elif event.button == plt.MouseButton.RIGHT:
        ax.lines[-1].remove()

# "Connecting" the cursor to above function.
cid = fig.canvas.mpl_connect('button_press_event', click)

# Loop displaying the plot and refreshing it every 0.05 second. When x coordinate
# of registered click is bigger than 0.9999 of the maximal x value loop
# will be break.
while True:
    plt.pause(0.05)
    if x_click >= (len(data_mono))*0.9999:
        break

示例输出: