简体   繁体   English

鼠标悬停事件发生时如何更改饼图切片的颜色

[英]How to change color of pie slice when mouse hover event take place

I have created a pie chart in matplotlib . 我在matplotlib创建了一个饼图。 I want to achieve this result in python ie whenever the mouse is hovered on any slice its color is changed .I have searched a lot and came up with the use of bind method but that was not effective though and therefore was not able to come up with the positive result. 我想在python中实现这个结果, 即每当鼠标悬停在任何切片上时它的颜色都会改变 。我已经搜索了很多并且想出了使用bind方法但是这样做并没有效果因此无法出现有积极的结果。 I will have no problem if this can be done through any other library(say tkinter , plotly ,etc but I need to come up with the solution with matplotlib so I would appreciate that more).Please have a look through my question and any suggestion is warmly welcomed... Here is my code: 我不会有任何问题,如果这可以通过任何其他库来完成(比如tkinterplotly等,但我需要拿出与解决方案matplotlib ,所以我将不胜感激,更多)。请有过我的问题,任何建议看看热烈欢迎...这是我的代码:

import matplotlib.pyplot as plt

labels = 'A', 'B', 'C', 'D'
sizes = [10, 35, 50, 5]
explode = (0, 0, 0.1, 0)  # only "explode" the 3rd slice (i.e. 'C')

fig1, ax1 = plt.subplots()
ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
    shadow=True, startangle=90)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

plt.show()

Regards... 问候...

You would need a matplotlib event handler for a motion_notify_event . 您需要一个用于motion_notify_eventmatplotlib事件处理程序 This can be connected to a function which checks if the mouse is inside one of the pie chart's wedges. 这可以连接到一个函数,该函数检查鼠标是否在饼图的一个楔形内。 This is done via contains_point . 这是通过contains_point完成的。 In that case colorize the wedge differently, else set its color to its original color. 在这种情况下,不同地着色楔形,否则将其颜色设置为其原始颜色。

import matplotlib.pyplot as plt

labels = 'A', 'B', 'C', 'D'
sizes = [10, 35, 50, 5]
explode = (0, 0, 0.1, 0)  # only "explode" the 3rd slice (i.e. 'C')

fig1, ax1 = plt.subplots()
wedges, _, __ = ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
    shadow=True, startangle=90)
ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

ocols= [w.get_facecolor() for w in wedges]
ncols= ["gold", "indigo", "purple", "salmon"]

def update(event):
    if event.inaxes == ax1:
        for i, w in enumerate(wedges):
            if w.contains_point([event.x, event.y]):
                w.set_facecolor(ncols[i])
            else:
                w.set_facecolor(ocols[i])
        fig1.canvas.draw_idle()


fig1.canvas.mpl_connect("motion_notify_event", update)

plt.show()

First off, what you are looking for is the documentation on Event handling in matplotlib . 首先,您要查找的是有关matplotlib中事件处理的文档 In particular, the motion_notify_event will be fired every time the mouse moves. 特别是,每次鼠标移动时都会触发motion_notify_event However, I can't think of an easy way to identify which wedge the mouse is over right now. 但是,我想不出一个简单的方法来确定鼠标现在在哪个楔形物上。

If clicking is acceptable, then the problem is much easier: 如果点击是可以接受的,那么问题就容易多了:

labels = 'A', 'B', 'C', 'D'
sizes = [10, 35, 50, 5]
explode = (0, 0, 0.1, 0)  # only "explode" the 3rd slice (i.e. 'C')
click_color = [0.2, 0.2, 0.2]

fig1, ax1 = plt.subplots()
patches, texts, autotexts = ax1.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%',
    shadow=True, startangle=90)

# store original color inside patch object
# THIS IS VERY HACKY.
# We use the Artist's 'gid' which seems to be unused as far as I can tell
# to be able to recall the original color
for p in patches:
    p.set_gid(p.get_facecolor())
    # enable picking
    p.set_picker(True)

ax1.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.


def on_pick(event):
    #restore all facescolor to erase previous changes
    for p in patches:
        p.set_facecolor(p.get_gid())
    a = event.artist
    # print('on pick:', a, a.get_gid())
    a.set_facecolor(click_color)
    plt.draw()

fig1.canvas.mpl_connect('pick_event', on_pick)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM