python - 如何在没有任何控件可见的情况下显示 matplotlib 交互式窗口?

标签 python matplotlib xfce

我需要在用户 Xfce4 桌面上显示 PNG。到目前为止,我正在使用一个 python 脚本,在交互式 matplotlib 窗口中显示 PNG:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread("My.png")
plt.imshow(img)
plt.show()

但这非常没有吸引力。有没有办法删除所有交互式控件、图像周围的所有边框空间(轴?),并在启动时将窗口大小调整为特定的宽度/高度?

或者,是否有更好的选择来在桌面上提供轻量级的静态图像显示?当然不一定非得是 python/matplotlib。

最佳答案

当然,但到那时,您可能会考虑使用“裸”GUI 工具包。

无论如何,这是 matplotlib 的方法:

import matplotlib.pyplot as plt

# Note that the size is in inches at 80dpi. 
# To set a size in pixels, divide by 80.
fig = plt.figure(figsize=(4, 5))

# Now we'll add an Axes that takes up the full figure
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off') # Hide all ticks, labels, outlines, etc.

# Display the image so that it will stretch to fit the size
# of the figure (pixels won't be square)
ax.imshow(plt.imread('test.png'), aspect='auto')

plt.show()

除了隐藏工具栏之外,这可以完成所有操作。要隐藏工具栏,您需要特定于后端。您有两种选择:1) 手动创建窗口并在其中嵌入 matplotlib Canvas ,2) 使用后端特定方法隐藏工具栏。

作为隐藏工具栏的示例,使用基于 qt 的后端,您可以这样做:

import matplotlib
matplotlib.use('qt4agg')
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4, 5))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')

ax.imshow(plt.imread('test.png'), aspect='auto')

# qt specific!
fig.canvas.toolbar.setVisible(False)

plt.show()

对于 Tk 后端,您需要这样做:

import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4, 5))
ax = fig.add_axes([0, 0, 1, 1])
ax.axis('off')

ax.imshow(plt.imread('test.png'), aspect='auto')

# Tk specific!
fig.canvas.toolbar.pack_forget()

plt.show()

相比之下,如果您想一起跳过 matplotlib 并仅使用 Tkinter,您可以执行以下操作:

import Tkinter as tk
from PIL import ImageTk

root = tk.Tk()

im = ImageTk.PhotoImage(file='test.png')
panel = tk.Label(root, image=im)
panel.pack(fill=tk.BOTH, expand=True)

root.mainloop()

这会在屏幕上以一像素到一像素的方式显示图像,并且不允许调整大小。但是,它大约是您能得到的最小限度。

关于python - 如何在没有任何控件可见的情况下显示 matplotlib 交互式窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36212204/

相关文章:

Python 数据框分组

python - seaborn 置信区间计算正确吗?

python - 如何沿 x 轴移动图形?

gtk - 从 XFCE 面板中移除阴影

linux - Alt 推送时 Manjaro Xfce 中基于 Electron 的应用程序的焦点丢失

python - 如何用分数标记 x 轴?

python - 在 Python 中结合 Perlin 噪声和泊松圆盘采样

python-3.x - 如何定义一个包含3个点的平面,并以3D方式对其进行绘制?

编译 XFCE4 面板插件

python - 如何检测由较小轮廓组成的较大轮廓?