第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

Python Matplotlib Multicursor:在圖例中顯示光標(biāo)下的值

Python Matplotlib Multicursor:在圖例中顯示光標(biāo)下的值

慕碼人2483693 2022-12-20 15:19:42
我正在使用 Multicursor 在每個(gè)圖形上獲取光標(biāo)。我想在將鼠標(biāo)懸停在圖表上時(shí)在圖例中顯示被光標(biāo)擊中的數(shù)據(jù)點(diǎn)的值,就像這樣實(shí)際上我一直認(rèn)為這是 matplotlib 和 Multicursor 的標(biāo)準(zhǔn)功能,但似乎不是。有人已經(jīng)這樣做了,還是我必須自己實(shí)施。我已經(jīng)在 cursor 下找到了這篇文章 matplotlib multiple values,但這可能只是我想要的實(shí)現(xiàn)的開(kāi)始。
查看完整描述

1 回答

?
米琪卡哇伊

TA貢獻(xiàn)1998條經(jīng)驗(yàn) 獲得超6個(gè)贊

我已經(jīng)開(kāi)發(fā)了一個(gè)解決方案。


import matplotlib.pyplot as plt

import numpy as np

from matplotlib.widgets import MultiCursor

from bisect import bisect_left



fig = plt.figure(figsize=(15, 8))


# create random graph with 60 datapoints, 0 till 59

x = list(range(0,60))


axes_list = [] 


def createRandomGraph(ax,x):

    y = np.random.randint(low=0, high=15, size=60)

    data.append(y)

    ax.plot(x,y, marker='.')



def take_closest(myList, myNumber):

    """

    Assumes myList is sorted. Returns closest value to myNumber.


    If two numbers are equally close, return the smallest number.

    """

    pos = bisect_left(myList, myNumber)

    if pos == 0:

        return myList[0]

    if pos == len(myList):

        return myList[-1]

    before = myList[pos - 1]

    after = myList[pos]

    if after - myNumber < myNumber - before:

       return after, pos

    else:

       return before, pos-1




def show_Legend(event): 

    #get mouse coordinates

    mouseXdata = event.xdata    


    # the value of the closest data point to the current mouse position shall be shown

    closestXValue, posClosestXvalue = take_closest(data[0], mouseXdata)


    i = 1

    for ax in axes_list:

        datalegend = ax.text(1.05, 0.5, data[i][posClosestXvalue],  fontsize=7,

                                      verticalalignment='top', bbox=props, transform=ax.transAxes)               

        ax.draw_artist(datalegend) 


        # this remove is required because otherwise after a resizing of the window there is 

        # an artifact of the last label, which lies behind the new one

        datalegend.remove()

        i +=1


    fig.canvas.update()



# store the x value of the graph in the first element of the list

data = [x]


# properties of the legend labels

props = dict(boxstyle='round', edgecolor='black', facecolor='wheat', alpha=1.5)


for i in range(5):

    if(i>0):        

        # all plots share the same x axes, thus during zooming and panning 

        # we will see always the same x section of each graph

        ax = plt.subplot(5, 1, i+1, sharex=ax)             

    else:

        ax = plt.subplot(5, 1, i+1)        


    axes_list.append(ax)    

    createRandomGraph(ax,x)  


multi = MultiCursor(fig.canvas, axes_list, color='r', lw=1)   


# function show_Legend is called while hovering over the graphs 

fig.canvas.mpl_connect('motion_notify_event', show_Legend)


plt.show()

輸出看起來(lái)像這樣

https://i.stack.imgur.com/ksxGW.gif

查看完整回答
反對(duì) 回復(fù) 2022-12-20
  • 1 回答
  • 0 關(guān)注
  • 420 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)