Скрыть аннотации bbox при наведении курсора мыши за пределы графикаPython

Программы на Python
Anonymous
Скрыть аннотации bbox при наведении курсора мыши за пределы графика

Сообщение Anonymous »

В настоящее время я использую matplotlib для построения линейного графика и mplcursors для аннотирования линейного графика. Моя текущая проблема сейчас заключается в том, как скрыть аннотацию bbox каждый раз, когда указатель мыши находится за пределами графика?
вот фрагмент моего кода:def update_plot(self):
if not self.csv_initialized:
print("CSV not yet initialized; skipping plot update.")
return

if self.monitoring_complete: # Check if monitoring is complete
print("Monitoring is complete; stopping plot updates.")
return

file_path = os.path.join(cwd, 'analysis\\monitoring\\CableSense_' + self.save_time + '.csv')
print(f"Attempting to read data from: {file_path}")

if not os.path.exists(file_path):
print(f"File not found: {file_path}")
return

try:
df = pd.read_csv(file_path)
print(f"Data read from CSV:\n{df.head()}")
self.ax.clear()

if 'Time' not in df.columns:
print("Error: 'Time' column not found in CSV data.")
return

# Convert Time column to datetime
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S')

for column in df.columns:
if column != 'Time':
line, = self.ax.plot(df['Time'], df[column], label=column)
# Add label to the end of the line
self.ax.annotate(column, xy=(df['Time'].iloc[-1], df[column].iloc[-1]),
xytext=(5, 0), textcoords='offset points', color=line.get_color())

self.ax.set_title('Inverter IR Values Over Time')
self.ax.set_xlabel('Time')
self.ax.set_ylabel('IR Value')

# Format the x-axis to show time properly
self.ax.xaxis.set_major_formatter(mdates.DateFormatter('%H:%M:%S'))
self.ax.xaxis.set_major_locator(mdates.AutoDateLocator())

self.ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), prop={'size': 6})
self.fig.autofmt_xdate() # Rotate date labels
self.canvas.draw()

# Enable hovering annotations with mplcursors
cursor = mplcursors.cursor(self.ax, hover=True)

# Customize the annotations displayed on hover
@cursor.connect("add")
def on_add(sel):
# Customize the hover text
sel.annotation.set_text(f"{sel.artist.get_label()}\n{sel.target[1]:.2f}")
sel.annotation.get_bbox_patch().set(fc="white", alpha=0.8)

except Exception as e:
print(f"Failed to read or update plot: {e}")

self.schedule_task(60000, self.update_plot) # Schedule next updat


Подробнее здесь: https://stackoverflow.com/questions/790 ... -the-graph

Вернуться в «Python»