0

I've got a little problem with matplotlib.

I made an input function which allows me to decide whether I want to print my plot, save it or do both. The problem is with setting variables in my code:

I use input command:

Save_Print_SavPrin = input('Press s = Save, p = Print, b = Both: ')

But for that I need those variables s, p and b. The problem is that I cannot define them before I start defining my plots.

If I do something like this:

p = plt.show()
b = savefig1, savefig2, plt.show()
s = savefig1, savefig2
Save_Print_SavPrin = input('Press s = Save, p = Print, b = Both: ')

I'll get an obivous error that python doesn't know what is savefig1 etc. and it will also prompt plt.show() too soon.

If I take this part of code to the last lines of my code and use this loop, so it will look like this:

Here goes code for plotting things, I have two figures so after fig1 I have savefig1, and after fig2 is this code, almost same, but different output file name.

savefig2 = plt.savefig('S:\Data\Python\Results\PlotB2.png', format='png', dpi=600)


p = plt.show()
b = savefig1, savefig2, plt.show()
s = savefig1, savefig2
Save_Print_SavPrin = input('Press s = Save, p = Print, b = Both: ')


if Save_Print_SavPrin == p:
    plt.show()
elif Save_Print_SavPrin == s:
    savefig1, savefig2
elif Save_Print_SavPrin == b:
    savefig1, savefig2, plt.show()

The problem here that when I put it in the last place in my code, every function will execute, then I will be asked by input what I want and I do those things again, which is pointless. Do you have any idea?

2
  • Why not just move every call to plt.savefig() and plt.show() inside your if statements? Commented Mar 7, 2018 at 11:55
  • What do You mean? Commented Mar 7, 2018 at 12:00

1 Answer 1

1

You should not define a variable to plt.show() or plt.savefig() as those functions will be called at that point (and they return None). Instead, put those calls inside your if statements:

import matplotlib.pyplot as plt

fig1 = plt.figure()
# plot something here

fig2 = plt.figure()
# plot something else here

Save_Print_SavPrin = input('Press s = Save, p = Print, b = Both: ')
# If using Python 2.x, use raw_input()

if Save_Print_SavPrin == "p":
    print("Show figure")
    plt.show()

elif Save_Print_SavPrin == "s":
    print("Saving")
    fig1.savefig("Test1.png")
    fig2.savefig("Test2.png")

elif Save_Print_SavPrin == "b":
    print("Both")
    fig1.savefig("Test1.png")
    fig2.savefig("Test2.png")
    plt.show()
Sign up to request clarification or add additional context in comments.

4 Comments

It looks like a great idea, but when I press any key p, s or b i got an error that they are not defined.
Ah, you are using python2, then yes use raw_input. I will update my answer
Perfect everything works great exept one, as I have mentioned before, I have two figures, how to make in a loop so it will save figure 1 and figure 2?
@Hiddenguy I've updated my answer for saving multiple figures

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.