【发布时间】: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() 之前。我可以把它们都列出来,或者有人可以告诉我如何正确地做到这一点
-
您需要
thismanager和thismanager2,每个数字一个。如果您不提供至少一个问题的minimal reproducible example,我无法向您展示如何正确执行此操作。 -
我不知道这个问题的完整性是什么,我之前因为给出不相关的代码而被否决。当你不知道时,很难知道。我进行了编辑以提供我尝试过的众多变体之一。
-
检查您是否提供了minimal reproducible example,非常简单。您将问题中的代码复制到 python 文件并运行它。如果它重现了问题,它已经是一个完整且可验证的示例。如果您能够从代码中删除更多行或使其不那么复杂,那么它还不是最小的。当我尝试运行您的代码时,我得到
RuntimeError: Too early to create image
标签: matplotlib tkinter matplotlib-basemap