我正在尝试使用 python 在带有 matplotlib 的堆栈图中绘制大量曲线。要阅读图表,我需要显示图例,但如果我用legend
方法显示它,我的图表是不可读的(因为图例的数量及其大小)。
我发现这mplcursors
可以帮助我通过图表本身的弹出窗口来做到这一点。它适用于“简单”的情节,但不适用于stackplot
.
这是带有堆栈图的警告消息:
/usr/lib/python3.7/site-packages/mplcursors/_pick_info.py:141: UserWarning: Pick support for PolyCollection is missing.
warnings.warn(f"Pick support for {type(artist).__name__} is missing.")
这是与此错误相关的代码(这只是概念证明):
import matplotlib.pyplot as plt
import mplcursors
import numpy as np
data = np.outer(range(10), range(1, 5))
timestamp = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
tmp = list()
tmp.append(data[:, 0])
tmp.append(data[:, 1])
tmp.append(data[:, 2])
tmp.append(data[:, 3])
print(data)
print(tmp)
fig, ax = plt.subplots()
ax.stackplot(timestamp, tmp, labels=('curve1', 'line2', 'curvefever', 'whatever'))
ax.legend()
mplcursors.cursor()
cursor = mplcursors.cursor(hover=True)
@cursor.connect("add")
def on_add(sel):
print(sel)
label = sel.artist.get_label()
sel.annotation.set(text=label)
plt.show()
您是否知道如何解决该问题,或者您是否知道另一种方法来做类似的事情?