Python 3.x 如何加上「;复制id“;matplot图形窗口中的按钮

Python 3.x 如何加上「;复制id“;matplot图形窗口中的按钮,python-3.x,matplotlib,charts,Python 3.x,Matplotlib,Charts,我正在从事产品销售分析项目,为此我为不同的产品创建了一个甜甜圈图表。现在我的问题是,如何在甜甜圈图表的图例(或图形窗口中的任何位置)附近添加复制id按钮,以便用户可以直接从那里复制产品id 预期产出 代码 import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal")) products =

我正在从事产品销售分析项目,为此我为不同的产品创建了一个甜甜圈图表。现在我的问题是,如何在甜甜圈图表的图例(或图形窗口中的任何位置)附近添加
复制id
按钮,以便用户可以直接从那里复制产品id

预期产出

代码

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))

products = ["id 11111",
          "id 22222",
          "id 33333",
          "id 44444",
          "id 55555",
          "id 66666"]

data = [225, 90, 50, 60, 100, 5]

wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
          bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1)/2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(products[i], xy=(x, y), xytext=(1.35*np.sign(x), 1.4*y),
                horizontalalignment=horizontalalignment, **kw)

ax.set_title("Matplotlib Products Sell: A donut")

plt.show()

输出


以下代码允许您单击批注框并将内容复制到剪贴板

我使用
pandas.io.clipboard
执行此操作,如所示


嘿@Diziet Asahi谢谢你的回答,我尝试了你建议的任何方法,但出现了以下错误
raise PyperclipException(除了_MSG)pandas.io.clipboard.PyperclipException:Pyperclip无法为你的系统找到复制/粘贴机制。
我不知道你的环境,所以我不能说哪种解决方案有效,但是,除了我在该线程中链接的答案之外,还有很多解决方案,所以您可能想尝试一些,直到找到一个适合您的解决方案。我想您还没有收到我的问题。我想在
matplot
图形窗口上单击
onClick
它将复制
产品id
import numpy as np
import matplotlib.pyplot as plt
from pandas.io.clipboard import copy


def onclick(event):
    copy(event.artist.get_text())


fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal"))
cid = fig.canvas.mpl_connect('pick_event', onclick)

products = ["id 11111",
            "id 22222",
            "id 33333",
            "id 44444",
            "id 55555",
            "id 66666"]

annotations = []

data = [225, 90, 50, 60, 100, 5]

wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=-40)

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72)
kw = dict(arrowprops=dict(arrowstyle="-"),
          bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):
    ang = (p.theta2 - p.theta1) / 2. + p.theta1
    y = np.sin(np.deg2rad(ang))
    x = np.cos(np.deg2rad(ang))
    horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]
    connectionstyle = "angle,angleA=0,angleB={}".format(ang)
    kw["arrowprops"].update({"connectionstyle": connectionstyle})
    ax.annotate(products[i], xy=(x, y), xytext=(1.35 * np.sign(x), 1.4 * y),
                horizontalalignment=horizontalalignment, picker=True, **kw)

ax.set_title("Matplotlib Products Sell: A donut")

plt.show()