在Matplotlib中,format_coord
显示x和y坐标,如果鼠标悬停在图形画布上的任何位置。仅当鼠标指针位于曲线上方时,如何限制显示仅显示坐标?
from matplotlib.axes import Axes
class MyAxes(Axes):
name = 'MyAxes'
def format_coord(self, x, y):
# Massage your data here -- good place for scalar multiplication
return 'time=%s Damped oscillation=%s' % (x, y)
from matplotlib.projections import projection_registry
projection_registry.register(MyAxes)
import numpy as np
import matplotlib.pyplot as plt
x1 = np.linspace(0.0, 5.0)
x2 = np.linspace(0.0, 2.0)
y1 = np.cos(2 * np.pi * x1) * np.exp(-x1)
y2 = np.cos(2 * np.pi * x2)
plt.subplot(2, 1, 1, projection="MyAxes")
plt.plot(x1, y1, 'ko-')
plt.title('Oscillation')
plt.ylabel('Oscillation')
plt.show()