【问题标题】:Change icon in multiple Matplotlib figure windows在多个 Matplotlib 图形窗口中更改图标
【发布时间】:2018-03-01 20:36:05
【问题描述】:

我在 matplotlib 中有 2 个图形,一个是标准图形,另一个是我绘制纬度/经度的底图。我试图为两个窗口设置一个标志,但我只能将自定义标志放到 Figure1 上,Figure2 仍然显示 Tkinter 标志。

我正在关注这里Change icon in a Matplotlib figure window的答案之一

from Tkinter import PhotoImage
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

thismanager = plt.get_current_fig_manager()
img = PhotoImage(file='elcc_logo.ppm')

我有 2 个地块:

plt.figure(num=1, figsize=(16, 12))
thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img)
plt.plot(date, energyplot, 'bo')

plt.figure(num=2, figsize=(10, 8))
thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img)
earth = Basemap(projection='mill')
earth.drawcoastlines(color='0.50', linewidth=0.25)
earth.fillcontinents(color='0.95', zorder=0)
x, y = earth(longitudes, latitudes)
earth.scatter(x, y, color='g', marker='o', linewidths=0.5)
for i, txt in enumerate(datalogger_id):
        plt.annotate(txt, xy=(x[i], y[i]), xycoords='data', xytext=(8, -4), textcoords='offset points', clip_on=True)
plt.show()

我可以交换数字号码,无论哪个有num=1的人都可以得到标志,我只是不能同时得到它。

【问题讨论】:

  • 您使用的代码不清楚。与添加图片的部分相比,plt.figure 坐在哪一点?在任何情况下,您当然都需要为两个人物都提供徽标,而不仅仅是其中一个。
  • 我尝试了很多安排,我把 thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img) 放在每个 plt.figure 之前,之后,在脚本的顶部,紧接在 plt.show() 之前。我可以把它们都列出来,或者有人可以告诉我如何正确地做到这一点
  • 您需要thismanagerthismanager2,每个数字一个。如果您不提供至少一个问题的minimal reproducible example,我无法向您展示如何正确执行此操作。
  • 我不知道这个问题的完整性是什么,我之前因为给出不相关的代码而被否决。当你不知道时,很难知道。我进行了编辑以提供我尝试过的众多变体之一。
  • 检查您是否提供了minimal reproducible example,非常简单。您将问题中的代码复制到 python 文件并运行它。如果它重现了问题,它已经是一个完整且可验证的示例。如果您能够从代码中删除更多行或使其不那么复杂,那么它还不是最小的。当我尝试运行您的代码时,我得到RuntimeError: Too early to create image

标签: matplotlib tkinter matplotlib-basemap


【解决方案1】:

我会尝试以下方法(不幸的是,我无法对其进行测试,因为我没有任何可用的图标文件),您可以在其中为每个人物创建一个经理。

import matplotlib; matplotlib.use("TkAgg")
from Tkinter import PhotoImage
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np

plt.figure(num=1, figsize=(16, 12))
thismanager = plt.get_current_fig_manager()
img = PhotoImage(name="icon.ppm")
thismanager.window.tk.call('wm', 'iconphoto', thismanager.window._w, img)

plt.figure(num=2, figsize=(10, 8))
thismanager2 = plt.get_current_fig_manager()
img2 = PhotoImage(name="icon.ppm")
thismanager2.window.tk.call('wm', 'iconphoto', thismanager2.window._w, img2)

plt.show()

【讨论】:

  • 我现在得到这个错误:thismanager2.window.tk.call('wm', 'iconphoto', thismanager2.window._w, img2) _tkinter.TclError: can't use "pyimage20" as iconphoto:不是照片图像
  • 我尝试制作 2 个不同的 .ppm 文件,例如 icon1.ppm icon2.ppm,但没有任何区别
  • 那很不幸。但另一方面,这将是 minimal reproducible example,您可以通过编辑问题或提出新问题来提出问题。
猜你喜欢
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 2011-06-08
  • 1970-01-01
  • 1970-01-01
  • 2015-01-14
  • 1970-01-01
  • 2012-06-24
相关资源
最近更新 更多