4

I am having a weird problem in Pycharm 2017.3.3 Community Edition. I am making a cycle that is supposed to show different figures in each iteration. When I run the code, only the first figure shows and the program gets stuck. The same happens if I run the code in debug mode with no breakpoints. However, if I set a breakpoint, and keep resuming the program when it reaches the breakpoint, all the figures are shown and the program finishes the execution successfully. Also wanted to clarify that I have used the code before with less figures and it had worked out fine.

Here is the code:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from sklearn.externals import joblib

feat_file = 'C:\\Users\\DianaCarolina\\Google Drive\\Maestría\\datos\\indices_Diana\\all_type.xlsx'
sites = {"Bosque":['5256', '5260'], "Rastrojo": ['5253', '5255'], "Pastizal": ['5257', '7125']}
model=joblib.load("C:\\Users\\DianaCarolina\\Google Drive\\Maestría\\datos\\indices_Diana\\ecotyhmm13_3.pkl")
nclasses = 13
for comp in range(nclasses):
    histData = []
    types = []
    col = []
    for type in sites:
        df = pd.read_excel(feat_file, sheet_name=type, index_col=0)
        mdata=df.loc[df.iloc[:, 20] == comp, "Mes"]
        types.append(type)
        histData.append(mdata)

        if len(col) < 3:
            if type == "Bosque" and "green" not in col:
                col.append("green")
            elif type == "Rastrojo" and "orange" not in col:
                col.append("orange")
            elif type == "Pastizal" and "yellow" not in col:
                col.append("yellow")

    mclass = np.sum(np.array([model.gmms_[comp].weights_]).T * model.gmms_[comp].means_, 0)

    plt.figure()

    if not histData[0].empty or not histData[1].empty or not histData[2].empty:
        plt.subplot(2, 1, 1)
        n, bins, patches = plt.hist(histData, np.arange(0.5,13, 1), stacked=True, label= types, color = col, rwidth=0.8)
        plt.legend()
        plt.xticks(range(1,13), ("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", \
                                      "Septiembre", "Octubre", "Noviembre", "Diciembre"))
        plt.title("Clase "+str(comp+1)+" de "+str(nclasses))

        plt.subplot(2, 1, 2)

    plt.stem(mclass)
    plt.xticks(range(14), list(df.loc[:, "M":"Salida2_4"]))
    plt.title("Medias de características para la clase")
    plt.show()
12
  • To help you out we would need stackoverflow.com/help/mcve Commented Jan 29, 2018 at 15:24
  • How much times about your cycle statement? Commented Jan 29, 2018 at 15:26
  • @FrankAK it should show 13 figures. Commented Jan 29, 2018 at 15:28
  • Can you put your screenshots here? Commented Jan 29, 2018 at 15:29
  • 1
    If your code is very long, trim it down to a MCVE as this is the best way to get the good answers Commented Jan 29, 2018 at 16:07

1 Answer 1

3

plt.show() should usually only be called once at the end of your code. This will show all figures that have been created and it will block the execution of any code after it.

E.g.

import matplotlib.pyplot as plt

for i in range(2):
    plt.figure()
    plt.plot([1,2,3])

    plt.show()

This will only show the second figure after you close the first one.

The simplest solution would be to un-indent your plt.show() such that once all the figures have been created they will all be shown.

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.