python - Pycharm 和 matplotlib 仅在 Debug模式下工作

标签 python debugging matplotlib pycharm

我在 Pycharm 2017.3.3 社区版中遇到了一个奇怪的问题。我正在制作一个循环,应该在每次迭代中显示不同的数字。当我运行代码时,只有第一个数字显示并且程序卡住了。如果我在没有断点的 Debug模式下运行代码,也会发生同样的情况。但是,如果我设置断点,并在到达断点时继续执行程序,则显示所有数字并且程序成功执行。还想澄清一下,我以前用较少的数字使用过代码并且效果很好。

代码如下:

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()

最佳答案

plt.show() 通常只应在代码末尾调用一次。这将显示已创建的所有图窗,并将阻止它之后的任何代码的执行。

例如

import matplotlib.pyplot as plt

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

    plt.show()

这只会在您关闭第一个数字后显示第二个数字。

最简单的解决方案是取消缩进您的 plt.show(),这样一旦创建了所有图形,它们就会全部显示出来。

关于python - Pycharm 和 matplotlib 仅在 Debug模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48504563/

相关文章:

c - 如何解释 linux 内核陷阱错误行?

c - WIndows系统编程使用标准C库

Python 导入 matplotlib.pyplot 不起作用

python - 如何使用自定义路径设置自定义字体到 matplotlib 全局字体?

Python:我可以修改元组吗?

Python 不能返回函数值?

jquery - jquery ui Accordion 头部的单选按钮导致它没有反应

python pandas 和 matplotlib 安装冲突

python - 使用groupdict将字符串解析为dict

python - 发射多颗子弹并通过按键改变它们的位置