I'm using matplotlib to show a picture but I want to hide the window frame.
I tried the code frameon=False in plt.figure() but the window frame is still there. Just the background color turns to grey.
Here is the code and running result. The picture was showing with the window even I add the "frameon=False" in the code.
frameon suppresses the figure frame. What you want to do is show the figure canvas in a frameless window, which cannot be managed from within matplotlib, because the window is an element of the GUI that shows the canvas. Whether it is possible to suppress the frame and how to do that will depend on the operating system and the matplotlib backend in use.
Let's consider the tk backend.
import matplotlib
# make sure Tk backend is used
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# turn navigation toolbar off
plt.rcParams['toolbar'] = 'None'
# create a figure and subplot
fig, ax = plt.subplots(figsize=(2,2))
#remove margins
fig.subplots_adjust(0,0,1,1)
# turn axes off
ax.axis("off")
# show image
im = plt.imread("https://upload.wikimedia.org/wikipedia/commons/8/87/QRCode.png")
ax.imshow(im)
# remove window frame
fig.canvas.manager.window.overrideredirect(1)
plt.show()
Related
I am trying to create a function that will accept a figure handle from a closed matplotlib figure and use that handle to reshow the figure. The below code will do this however the navigation toolbar is still linked to the old (destroyed) figure so plot interactivity is lost. Is there a way of linking the navigation toolbar to the new window so the plot is interactive?
For reference, I have consulted similar questions:
Matplotlib: how to show a figure that has been closed
Re-opening closed figure matplotlib
Matplotlib: re-open a closed figure?
The solution (if it exists) should not require use of a bespoke backend. I am hoping this is possible with whatever backend is default (which changes with different OS). I'm also looking to do this without relying on iPython.
My partially complete solution (which lacks navigation bar interactivity in the reshown figure) is:
import matplotlib.pyplot as plt
def reshow_figure(handle):
figsize = handle.get_size_inches() # get the size of the old figure
fig_new = plt.figure() # make a new figure
new_manager = fig_new.canvas.manager # get the figure manager from the new figure
new_manager.canvas.figure = handle # assign the old figure to the new figure manager
handle.set_canvas(new_manager.canvas) # assign the new canvas to the old figure
handle.set_size_inches(figsize) # restore the figsize
plt.show() # show the resurrected figure
plt.plot([1,2,3,4,5],[1,5,3,4,2])
fig = plt.gcf() # get the figure handle to resurrect the figure later
plt.title('My Figure') # just to check the title copies across
plt.gcf().set_size_inches((10,5)) # set a custom size to test recovery of the figsize
plt.show()
# manually close the figure window
reshow_figure(fig)
I am facing couple of issues. First, I wanted all the plots in a separate window. For this, I successfully changed the settings and I got the separate window. The problem is, I got all the plots in same figures, which is bad. Second issue is, how do I inscribe window pan to the Ipconsole? I donot want a separate window. I want this window inside the console?
For the first issue, you can have your plots in different figures by using figure this way:
import matplotlib.pyplot as plt
plt.figure()
# Plot your first graph(s)
plt.figure()
# Plot your other graph(s)
plt.show()
Each time you call figure, a new window is created. For more information on figure, you can check the doc
I've created a 3d plot via matplotlib and am having issues importing it into a tkinter canvas GUI. The screenshots below show the plot as I'd like to see it (in the standalone program) and how it's appearing within my canvas (low resolution, incorrect spacing, scales, etc).
Running the following code at the end of my standalone program gives me 1280x960, DPI 200.
DPI = fig.get_dpi()
bbox = fig.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
width, height = bbox.width*fig.dpi, bbox.height*fig.dpi
In my tkinter canvas, I'm setting the figure (as below), and keeping all other attributes the same.
fig = Figure(figsize=(6.4,4.8), dpi=200)
Standalone:
Tkinter Canvas:
Any help would be greatly appreciated.
I'm using matplotlib to show a picture but I want to hide the window frame.
I tried the code frameon=False in plt.figure() but the window frame is still there. Just the background color turns to grey.
Here is the code and running result. The picture was showing with the window even I add the "frameon=False" in the code.
frameon suppresses the figure frame. What you want to do is show the figure canvas in a frameless window, which cannot be managed from within matplotlib, because the window is an element of the GUI that shows the canvas. Whether it is possible to suppress the frame and how to do that will depend on the operating system and the matplotlib backend in use.
Let's consider the tk backend.
import matplotlib
# make sure Tk backend is used
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# turn navigation toolbar off
plt.rcParams['toolbar'] = 'None'
# create a figure and subplot
fig, ax = plt.subplots(figsize=(2,2))
#remove margins
fig.subplots_adjust(0,0,1,1)
# turn axes off
ax.axis("off")
# show image
im = plt.imread("https://upload.wikimedia.org/wikipedia/commons/8/87/QRCode.png")
ax.imshow(im)
# remove window frame
fig.canvas.manager.window.overrideredirect(1)
plt.show()
I am using matplotlib with interactive mode on and am performing a computation, say an optimization with many steps where I plot the intermediate results at each step for debugging purposes. These plots often fill the screen and overlap to a large extent.
My problem is that during the calculation, figures that are partially or fully occluded don't refresh when I click on them. They are just a blank grey.
I would like to force a redraw if necessary when I click on a figure, otherwise it is not useful to display it. Currently, I insert pdb.set_trace()'s in the code so I can stop and click on all the figures to see what is going on
Is there a way to force matplotlib to redraw a figure whenever it gains mouse focus or is resized, even while it is busy doing something else?
Something like this might work for you:
import matplotlib.pyplot as plt
import numpy as np
plt.ion() # or leave this out and run with ipython --pylab
# draw sample data
fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot(np.random.rand(10))
class Refresher:
# look for mouse clicks
def __init__(self, fig):
self.canvas = fig.canvas
self.cid = fig.canvas.mpl_connect('button_press_event', self.onclick)
# when there is a mouse click, redraw the graph
def onclick(self, event):
self.canvas.draw()
# remove sample data from graph and plot new data. Graph will still display original trace
line.remove()
ax.plot([1,10],[1,10])
# connect the figure of interest to the event handler
refresher = Refresher(fig)
plt.show()
This will redraw the figure whenever you click on the graph.
You can also experiment with other event handling like
ResizeEvent - figure canvas is resized
LocationEvent - mouse enters a new figure
check more out here:
Have you tried to call plt.figure(fig.number) before plotting on figure fig and plt.show() after plotting a figure? It should update all the figures.