如何在不显示任何控件的情况下显示matplotlib交互式窗口? [英] How can I display a matplotlib interactive window without any of the controls visible?

查看:313
本文介绍了如何在不显示任何控件的情况下显示matplotlib交互式窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to display a PNG on a users Xfce4 desktop. So far I'm using a python script that shows the PNG in an interactive matplotlib window:

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

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

虽然那没有吸引力.有没有一种方法可以删除所有交互式控件,所有放置在图像周围的边框空间(轴?),并在启动时将窗口调整为特定的宽度/高度?

That is very unattractive though. Is there a way to remove all the interactive controls, all the border space (axis?) that gets put around the image, and resize the window to a particular width/height on startup?

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

Alternatively, is there a better option to provide a lightweight and static display of an image on a desktop? Doesn't have to be python/matplotlib of course.

推荐答案

可以,但是到那时,您可以考虑使用裸" gui工具包.

Sure, but at that point, you might consider using a "bare" gui toolkit instead.

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

At any rate, here's the matplotlib way:

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画布,2)使用后端特定的方法隐藏工具栏.

This does everything except for hiding the toolbar. To hide the toolbar, you'll need to be be backend-specific. You have two options: 1) Handle creating the window manually and embed the matplotlib canvas inside it, 2) hide the toolbar using backend-specific methods.

作为隐藏工具栏的示例,您将使用基于qt的后端:

As an example of hiding the toolbar, with the qt-based backends, you'd do:

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后端,您可以这样做:

And for the Tk-backend, you'd do:

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,则可以执行以下操作:

By contrast, if you wanted to skip matplotlib together and just use Tkinter, you'd do something like:

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()

这会在屏幕上以一个像素到一个像素显示图像,并且不允许调整大小.但是,它尽可能地少.

This displays the image at one-pixel-to-one-pixel on the screen and doesn't allow for resizing. However, it's about as minimal as you can get.

这篇关于如何在不显示任何控件的情况下显示matplotlib交互式窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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