我试图用matplotlib在堆栈图中绘制大量的曲线,使用python。要阅读图表,我需要显示图例,但是如果我用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()你知道如何解决这个问题吗?或者你知道另一种方法来做这样的事情吗?
发布于 2020-06-17 13:50:27
尚不清楚为什么mplcursors不接受堆栈图。但是,您可以使用更原始的matplotlib功能复制该行为:
import matplotlib.pyplot as plt
import numpy as np
def update_annot(label, x, y):
annot.xy = (x, y)
annot.set_text(label)
def on_hover(event):
visible = annot.get_visible()
is_outside_of_stackplot = True
if event.inaxes == ax:
for coll, label in zip(stckplt, labels):
contained, _ = coll.contains(event)
if contained:
update_annot(label, event.x, event.y)
annot.set_visible(True)
is_outside_of_stackplot = False
if is_outside_of_stackplot and visible:
annot.set_visible(False)
fig.canvas.draw_idle()
data = np.random.randint(1, 5, size=(4, 40))
fig, ax = plt.subplots()
labels = ('curve1', 'line2', 'curvefever', 'whatever')
stckplt = ax.stackplot(range(data.shape[1]), data, labels=labels)
ax.autoscale(enable=True, axis='x', tight=True)
# ax.legend()
annot = ax.annotate("", xy=(0, 0), xycoords="figure pixels",
xytext=(20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="yellow", alpha=0.6),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
plt.connect('motion_notify_event', on_hover)
plt.show()

https://stackoverflow.com/questions/62424057
复制相似问题