【发布时间】:2020-02-13 13:36:12
【问题描述】:
我正在尝试同时在同一个图形(或图形)中显示多个图的注释文本。下面的代码完美运行,但它一次显示一个图的一个注释,但我的图有多个图。 plot1_list、plot2_list、plot3_list 是我的图表中的图表列表。注释文本在我的图中采用“标签”的形式。 如何同时显示每个列表中每个图的注释文本?
import mplcursors
cursor1 = mplcursors.cursor(plot1_list, hover=False, bindings={"toggle_visible": "h", "toggle_enabled": "e"})
@cursor1.connect("add")
def on_add(sel):
sel.annotation.set_text(sel.artist.get_label())
cursor2 = mplcursors.cursor(plot2_list, hover=False, bindings={"toggle_visible": "h", "toggle_enabled": "e"})
@cursor2.connect("add")
def on_add(sel):
sel.annotation.set_text(sel.artist.get_label())
cursor3 = mplcursors.cursor(plot3_list, hover=False, bindings={"toggle_visible": "h", "toggle_enabled": "e"})
@cursor3.connect("add")
def on_add(sel):
sel.annotation.set_text(sel.artist.get_label())
以上代码中的情节列表是使用以下代码创建的:
label1 = str("MAXIMUM HEAD=" + str(m_head))
label2 = str("MAXIMUM POWER=" + str(m_power))
label3 = str("MAXIMUM EFFICIENCY=" + str(m_efficiency))
plot1, = host.plot(y0, y, linestyle=styles[0], color=colorstouse[count], label=label1)
plot2, = par1.plot(y0, y1, linestyle=styles[1], color=colorstouse[count], label=label2)
plot3, = par2.plot(y0, y2, linestyle=styles[2], color=colorstouse[count], label=label3)
【问题讨论】:
-
host = host_subplot(111, axes_class=AA.Axes) plt.subplots_adjust(right=0.75) par1 = host.twinx() par2 = host.twinx() new_fixed_axis = par1.get_grid_helper()。 new_fixed_axis par1.axis["right"] = new_fixed_axis(loc="right", axes=par1, offset=(0, 0)) par1.axis["right"].toggle(all=True) new_fixed_axis = par2.get_grid_helper ().new_fixed_axis par2.axis["right"] = new_fixed_axis(loc="right", axes=par2, offset=(60, 0)) par2.axis["right"].toggle(all=True)跨度>
-
x 轴相同,y 轴不同。
-
我添加了一个情节图像。如果您会注意到其中一个图在单击时有注释,但是一旦我单击另一个图,注释就会出现在另一个图上,而当前的图就会消失。我想在它们的蓝色和红色图上注释。
-
感谢您的帮助。我确实有最新版本。在我的情况下,所有三个注释都显示为最大头、最大功率和最大效率。但问题是它们只出现在每种颜色的情节中。我的意思是让我们在上图中说,我总共需要 6 个注释,但它不起作用。它只显示三个。
-
其实我的情节是动态的。这取决于用户的选择。它可以是 6 也可以是 90。在这种情况下我应该如何处理?谢谢
标签: python-3.x matplotlib plot annotations mplcursors