0

I've redacted information where necessary. But this question captures my problem:

Problem Statement:

My objective is simple: given graph data, graph it, and save it as a FULLSCREEN IMAGE automatically. (this needs to scale to a large number of graphs). I looked at: Saving Matplotlib graphs to image as full screen

And came up with the following solution:

import matplotlib.pyplot as plt

#Prepare Data
some_x_array = [0, 1, 2]
other_x_array = [0, 1, 2]
some_y_array = [4, 5, 6]
other_y_array = [7, 8, 9] 

#Plot Data
plt.plot(some_x_array, some_y_array)
plt.plot(other_x_array, other_y_array)
plt.legend(['The X Axis', 'The Y Axis'])

#Maximize the Window

manager = plt.get_current_fig_manager()
manager.window.showMaximized()

#Acquire figure before the Image Loads
fig = plt.gcf()

#Show the Image (but don't block the flow)
plt.show(block=False)

#Save the image
fig.savefig('stackoverflowimage.png')

However anyone running this script will realize that the image it produces is not full-size. If you change the line plt.show(block=False) to plt.show(block=True), a graph appears, and once you close it, you will find a full size image.

I want to get a full size image, WITHOUT the graph appearing and demanding human interaction. What to do?

My Attempt at Solving the Problem:

It seems to me the command to make the screen full-size is executed lazily, i.e. I do already maximize the window in the code very early on, but until I show the thing, it never happens.

One Idea then is to try to do a show with a timer (ex: 0.001 seconds) but that feels like a very poor design choice.

1 Answer 1

4

The solution you found will only work if the figure is shown in a window, because manager.window.showMaximized() sets the size of that window. It is actually not really clear what "fullscreen" would mean outside of some window management system. E.g. what is "fullscreen" on a server?

So if you have some idea of what size "fullscreen" should correspond to, you can set the figure size to that number.

For example I have a 1920 x 1080 pixels screen. In that case I can set the figure size to

fig = plt.figure(figsize=(19.2,10.8), dpi=100)

For finding out the screen size programmatically, see How do I get monitor resolution in Python? which has solutions for various systems.

E.g. using tkinter

import Tkinter as tk # use tkinter for python 3
root = tk.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()

import matplotlib.pyplot as plt

#Prepare Data
x,y =   [0, 1, 2],[4, 5, 6]
x1,y1 = [0, 1, 2],[7, 8, 9] 

#Plot Data
fig = plt.figure(figsize=(width/100., height/100.), dpi=100)
plt.plot(x,y, label="label")
plt.legend()

#Save the image
fig.savefig('stackoverflowimage.png')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.