添加工具栏按钮图标matplotlib [英] Add toolbar button icon matplotlib

查看:108
本文介绍了添加工具栏按钮图标matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向 matplotlib 图形工具栏中的自定义按钮添加一个图标.我怎样才能做到这一点?到目前为止,我有以下代码:

I want to add an icon to a custom button in a matplotlib figure toolbar. How can I do that? So far, I have the following code:

import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolToggleBase

class NewTool(ToolToggleBase):
    ...[tool code]

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], label="legend")
ax.legend()
fig.canvas.manager.toolmanager.add_tool("newtool", NewTool)
fig.canvas.manager.toolbar.add_tool(toolmanager.get_tool("newtool"), "toolgroup")
fig.show()

目前,它唯一能做的就是添加一个新按钮(按我的意愿去做),但图标只是工具的名称,即:newtool".如何为自定义图标(如png图像)更改此设置?

For now, the only thing that it does is adding a new button (which do what I want) but the icon is only the tool's name i.e.: "newtool". How can I change this for a custom icon like a png image?

推荐答案

该工具可以有一个属性 image,表示 png 图像的路径.

The tool can have an attribute image, which denotes the path to a png image.

import matplotlib
matplotlib.rcParams["toolbar"] = "toolmanager"
import matplotlib.pyplot as plt
from matplotlib.backend_tools import ToolBase

class NewTool(ToolBase):
    image = r"C:\path\to\hiker.png"

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1, 2, 3], label="legend")
ax.legend()
tm = fig.canvas.manager.toolmanager
tm.add_tool("newtool", NewTool)
fig.canvas.manager.toolbar.add_tool(tm.get_tool("newtool"), "toolgroup")
plt.show()

这篇关于添加工具栏按钮图标matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆