BenQuick
BenQuick

Reputation: 11

showing figure in matplotlib

I have a question regarding show() method from matplotlib. I have a for loop (for peptide) and within this loop I have another loop (for TFE). In the for TFE loop I plot on 2 different figures and after exiting TFE loop I want to plot on the third figure. The problem is, if I write plt.show() after plotting on second figure (instead of fig1.show() and fig2. show() like in the code below), it also shows me the empty third figure (which was created before the loops, but is plotted after them) after the first for peptide step. Moreover, each for peptide step plots only one plot on figure 3 as it seems show() clears previous work done on the figure(3). I want that figure(3) is plotted in each for peptide step but in a way that it retains plotting in previous steps (and is not desrupted by plt.show()) and is shown only at the very end.

I wanted to use fig1.show() and fig2.show() but when I close one of the 2 figures in the first TFE loop, figure(3) appears corretly, only that the others figures of the for TFE loop are not presented and the following shows up in the terminal: invalid command name "139620086899400idle_draw" while executing "139620086899400idle_draw" ("after" script)

Can you please give me some insight to the mentioned invalid command name and what should I do to overcome the problem? I didn't paste the whole code as it is quite long and messy but I think it is enough for the understanding of a problem.

EDIT: Ok, I created minimal code and my question is: fig1 and fig2 show figures with plots for every peptide step (while executing code the plots are added on figures with each step). I want that for fig3, which returns as expected, but for fig1 and fig2 I actually want 2 figures for each peptide step, so that I have peptide number of fig1 and fig2 figures and 1 fig3 figure.

import matplotlib.pyplot as plt
from scipy import stats
fig3 = plt.figure(3)
dictionary = {"peptide1": {5: {"deltaG": -15.5, "Fhteor": [0.9, 0.88], "T": [40, 45]}, 10: {"deltaG": -14.5, "Fhteor": [0.85, 0.83], "T": [40, 45]}},
"peptide666":{5: {"deltaG": -5.5, "Fhteor": [0.6, 0.57], "T": [40, 45]}, 10: {"deltaG": 1, "Fhteor": [0.4, 0.33], "T": [40, 45]}}}
for peptide in dictionary:
    TFE_list = []
    dG_list = []
    fig1 = plt.figure(1)
    fig2 = plt.figure(2)
    for TFE in dictionary[peptide]:
        plt.figure(1)
        plt.plot(TFE, dictionary[peptide][TFE]["deltaG"], marker = "o") #plotting on fig1
        plt.figure(2)
        plt.plot(dictionary[peptide][TFE]["T"], dictionary[peptide][TFE]["Fhteor"], marker = "v") #plotting on fig2
        if TFE <= 15:
            TFE_list.append(TFE)
            dG_list.append(dictionary[peptide][TFE]["deltaG"])

    plt.figure(1)
    lr = stats.linregress(TFE_list, dG_list)
    y_list = [lr.intercept + i*lr.slope for i in TFE_list]
    plt.plot(TFE_list, y_list) #plotting linear regression on plot 1
    fig1.show()
    fig2.show()
    plt.figure(3)
    plt.plot(len(peptide), len(peptide)*3, marker ="o") #plotting some characteristics derived from "for TFE loop" on fig3
plt.show()

Upvotes: 1

Views: 861

Answers (2)

BenQuick
BenQuick

Reputation: 11

The problem was initiating of fig(1) and fig(2). They were initiated in the first step but in the next step they were just called and plotting was superimposed. The answer is in proper initiating like for example:

for peptide in dictionary: fig1 = plt.figure(peptide + str(1)) fig2 = plt.figure(peptide + str(2))

str(1) and str(2) are meant to distinguish between the two figures. Stil don't know what the message: "139620086899400idle_draw" while executing "139620086899400idle_draw" ("after" script) means, but at that point it is not important anymore.

Upvotes: 0

nat5142
nat5142

Reputation: 495

What happens when you add fig1.close() immediately after fig1.show()?

Also, I'm fairly certain that placing plt.figure(2) inside of a for loop will cause you issues, let alone the fact that you've declared a variable fig2 = plt.figure(2) outside of the for loop, then called plt.figure(2) within the loop as well. It's been a while since I've worked with matplotlib but I don't think that you should ever put a plt.figure() inside of a for loop unless you're looping over multiple figures, i.e.:

for i in range(len(all_figures_to_be_plotted)):
    plt.figure(i)

Upvotes: 1

Related Questions