0

On Stackoverflow I found the following two bits of code:

import matplotlib.pyplot as plt
import numpy as np; np.random.seed(1)

x = np.random.rand(15)
y = np.random.rand(15)
names = np.array(list("ABCDEFGHIJKLMNO"))
c = np.random.randint(1,5,size=15)

norm = plt.Normalize(1,4)
cmap = plt.cm.RdYlGn

fig,ax = plt.subplots()
sc = plt.scatter(x,y,c=c, s=100, cmap=cmap, norm=norm)

annot = ax.annotate("", xy=(0,0), xytext=(20,20),textcoords="offset points",
                bbox=dict(boxstyle="round", fc="w"),
                arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)

def update_annot(ind):

    pos = sc.get_offsets()[ind["ind"][0]]
    annot.xy = pos
    text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))),
                       " ".join([names[n] for n in ind["ind"]]))
    annot.set_text(text)
    annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))
    annot.get_bbox_patch().set_alpha(0.4)


def picker(event):
    vis = annot.get_visible()
    if event.inaxes == ax:
        cont, ind = sc.contains(event)
        if cont:
            update_annot(ind)
            annot.set_visible(True)
            fig.canvas.draw_idle()
        else:
            if vis:
                annot.set_visible(False)
                fig.canvas.draw_idle()

fig.canvas.mpl_connect("motion_notify_event", picker)

plt.show()

from Possible to make labels appear when hovering over a point in matplotlib? and

import matplotlib.pyplot as plt

def main():
    # Make an example pie plot
    fig = plt.figure()
    ax = fig.add_subplot(111)

    labels = ['Apple', 'Mango', 'Orange']
    wedges, plt_labels = ax.pie([20, 40, 60], labels=labels)
    ax.axis('equal')

    make_picker(fig, wedges)
    plt.show()

def make_picker(fig, wedges):

    def onclick(event):
        wedge = event.artist
        label = wedge.get_label()
        print(label)

# Make wedges selectable
    for wedge in wedges:
        wedge.set_picker(True)

    fig.canvas.mpl_connect('pick_event', onclick)

if __name__ == '__main__':
    main()

from matplotlib mouseclick event in pie chart.

In the first block of code, a scatter plot is beeing created and there is a mouse hover effect, when hovering the mouse over a data point.

In the second code, a pie chart is created an when the user clicks on a wedge of the chart, the label will be printed in the console.

What I want to have is the same hovering effect, when hovering over a wedge of the pie chart.

Is there a way to achieve this?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Fabian
  • 1
  • 2
  • Do you understand those codes? Did you try to solve your problem? At which point do you need help? – ImportanceOfBeingErnest Jun 07 '18 at 11:58
  • What you need here is rather a solution similar to a [bar graph](https://stackoverflow.com/questions/50560525/i-want-to-display-the-values-of-x-and-y-while-hover-mouse-over-the-bar-graph). – ImportanceOfBeingErnest Jun 07 '18 at 12:00
  • Yes, I understand these codes, but I couldn't find a way to solve my problem. I tried to change the second code to react on 'motion_notify_event' instead of 'pick_event', but then I get AttributeError: 'MouseEvent' object has no attribute 'artist' – Fabian Jun 07 '18 at 12:02
  • Ok, apparently you don't understand them. Start with the linked bar graph code and see how far you get with it. Provide a [mcve] of the issue you're having. – ImportanceOfBeingErnest Jun 07 '18 at 12:03

0 Answers0