如何在 python 图中的悬停标签中添加新行?

How to add new line to the hover label in python plot?

我想在 plot.For 示例中将特定点悬停时添加带有新行的文本 如果悬停了 A 点,则悬停的标签将显示如下文本, 文本 1
文本 2
文本 3
下面是 draw_plot 函数片段:

   def draw_plot(ue_info,selected_parm):

            rp_x = [-1.0,0.0,1.0,-1.0,0.0,1.0,-1.0,0.0,1.0,-1.0,0.0,1.0]
            rp_y = [1.5,1.5,1.5,0.5,0.5,0.5,-0.5,-0.5,-0.5,-1.5,-1.5,-1.5]
            Cluster = np.array([1, 1, 1, 1, 1, 1, 1, 1, 1,1,1,1])

            fig = plt.figure()
            ax = fig.add_subplot(111)
            scatter = ax.scatter(rp_x,rp_y,c=Cluster,s=50)
            fig.patch.set_visible(False)
            ax.axis('off')
            ax.set_autoscale_on(False)

            ue_offset  = 0.2

            for ue in ue_info:
                    try:
                            i = rand.uniform(rp_x[int(ue_info[ue]["pr_rp_id"])]-ue_offset,rp_x[int(ue_info[ue]["pr_rp_id"])]+ue_offset)
                            j = rand.uniform(rp_y[int(ue_info[ue]["pr_rp_id"])]-ue_offset,rp_y[int(ue_info[ue]["pr_rp_id"])]+ue_offset)
                            if float(ue_info[ue][selected_parm]) < 20.0:
                                    ues = ax.scatter(i,j,s=50,c='green',marker='+')
                            if float(ue_info[ue][selected_parm]) < 21.0:
                                    ues = ax.scatter(i,j,s=50,c='orange',marker='+')
                            else:
                                    ues = ax.scatter(i,j,s=50,c='red',marker='+')
                            ueLabel = "UE:"+ue +"\n" +" pr_rp_id:"+ ue_info[ue]["pr_rp_id"]
                            tooltip = mpld3.plugins.PointLabelTooltip(ues,labels=[ueLabel])
                            mpld3.plugins.connect(fig, tooltip)
                    except:
                            print "One of the UE is removed while drawing the plot..."

            tooltip = mpld3.plugins.PointLabelTooltip(scatter)
            mpld3.plugins.connect(fig, tooltip)

            plt.text(-1.0, 1.7, "RP0", fontsize=8)
            plt.text(0.0, 1.7, "RP1", fontsize=8)
            plt.text(1.0, 1.7, "RP2", fontsize=8)
            plt.text(-1.0, 0.7, "RP3", fontsize=8)
            plt.text(0.0, 0.7, "RP4", fontsize=8)
            plt.text(1.0, 0.7, "RP5", fontsize=8)
            plt.text(-1.0,-0.3, "RP6", fontsize=8)
            plt.text(0.0, -0.3, "RP7", fontsize=8)
            plt.text(1.0, -0.3, "RP8", fontsize=8)
            plt.text(-1.0,-1.3, "RP9", fontsize=8)
            plt.text(0.0, -1.3, "RP10", fontsize=8)
            plt.text(1.0, -1.3, "RP11", fontsize=8)

            ax.set_ylim([-2,2])
            ax.set_xlim([-2,2])

            htmlGraph = mpld3.fig_to_html(fig)

            plt.close('all')
            return htmlGraph

悬停到特定点时的输出如下:

所以在上图中,当黄色“+”悬停时,它会以单个 line.But 显示悬停的文本,我想要的输出是:
UE:280
pr_rp_id:4

这是我用新行添加悬停文本的部分:

ueLabel = "UE:"+ue +"\n" +" pr_rp_id:"+ ue_info[ue]["pr_rp_id"]

请帮助我。

您可以使用 PointHTMLTooltip 而不是 PointLabelTooltip 生成新行:

import matplotlib.pyplot as plt
from mpld3 import fig_to_html, plugins, display
fig, ax = plt.subplots()
points = ax.plot(range(10), 'o')

texts = ['<p><h5>{title1}</h3><p><h5>#{title2}</h3>'.format(
         title1='hello', title2=i) for i in range(10
        )]

text = plugins.PointHTMLTooltip(points[0], texts)
plugins.connect(fig, text)

fig_to_html(fig)
display()