Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在matplotlib中同时显示打印的注释文本_Python 3.x_Matplotlib_Plot_Annotations_Label - Fatal编程技术网

Python 3.x 在matplotlib中同时显示打印的注释文本

Python 3.x 在matplotlib中同时显示打印的注释文本,python-3.x,matplotlib,plot,annotations,label,Python 3.x,Matplotlib,Plot,Annotations,Label,我试图同时显示同一图形(或图)中多个绘图的注释文本。下面的代码工作得很好,但它一次显示一个绘图的注释,但我的图形有多个绘图。plot1\u列表、plot2\u列表、plot3\u列表是图形中的打印列表。注释文本在我的图中以“标签”的形式出现。 如何同时显示每个列表中每个图的注释文本 import mplcursors cursor1 = mplcursors.cursor(plot1_list, hover=False, bindings={"toggle_visible": "h", "

我试图同时显示同一图形(或图)中多个绘图的注释文本。下面的代码工作得很好,但它一次显示一个绘图的注释,但我的图形有多个绘图。plot1\u列表、plot2\u列表、plot3\u列表是图形中的打印列表。注释文本在我的图中以“标签”的形式出现。 如何同时显示每个列表中每个图的注释文本

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)

在您的情况下,每条曲线都需要一个单独的光标。这样,它们中的每一个都可以单独打开或关闭

代码看起来有点复杂。这里是一个简化的尝试。该示例显示了使用3个轴的15条曲线。请注意,它可能会变得非常拥挤,因此90条曲线可能有点太多

from matplotlib import pyplot as plt
import numpy as np
import mplcursors

num_devices = 5
fig, host = plt.subplots(figsize=(12, 4))
fig.subplots_adjust(right=0.8)

par1 = host.twinx()
par2 = host.twinx()
par2.spines["right"].set_position(("axes", 1.1))

x = np.linspace(0, 10)
y = -x ** 2 + 5 * x + 10
y1 = 10 + 20 * np.cos(x)
y2 = 30 + 5 * np.sin(x)

ys = [y + np.random.uniform(10, 30) for i in range(num_devices)]
y1s = [y1 + np.random.uniform(10, 30) for i in range(num_devices)]
y2s = [y2 + np.random.uniform(10, 30) for i in range(num_devices)]

colors = plt.cm.tab10.colors
plots = []
for dev in range(num_devices):
    plot1 = host.plot(x, ys[dev], c=colors[dev], ls='-', label=f'plot 1 - dev {dev}')
    plot2, = par1.plot(x, y1s[dev], c=colors[dev], ls='--', label=f'plot 2 - dev {dev}')
    plot3, = par2.plot(x, y2s[dev], c=colors[dev], ls=':', label=f'plot 3 - dev {dev}')
    plots += [plot1, plot2, plot3]

print(plots)
for i, plot in enumerate(plots):
    cursor = mplcursors.cursor(plot, hover=False, bindings={"toggle_visible": "h", "toggle_enabled": "e"})
    cursor.connect("add", lambda sel: sel.annotation.set_text(sel.artist.get_label()))
plt.show()

host=host\u子地块(111,轴类=AA.axes)plt.subplot\u adjust(right=0.75)par1=host.twinx()par2=host.twinx()new\u fixed\u axis=par1.get\u grid\u helper().new\u fixed\u axis par1.axis[“right”]=new\u fixed\u axis\u axis(loc=“right”,axes=par1,offset=(0,0))par1.axis[“right”]切换(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。在这种情况下,我应该如何处理?谢谢