在Flask中显示Matplotlib图时出错

im9ewurl  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(69)

我从一个flask端点获取两个输入。第一个值是string类型,另一个也是字符串,我将其转换为int类型。
当我将这些值从flask脚本传递到另一个使用matplotlib.pyplot生成饼图的脚本时,我得到一个错误:

analyzer.py:114: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
  patches, texts = plt.pie(sizes, colors=colors, startangle=90)
2020-11-29 20:02:23.430 Python[48923:2857853] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'

字符串
但是,当我直接运行piechart生成脚本(analyzer.py)时,我能够看到预期的输出,并且在指定的目录中也生成了图形。
以下是导致问题的函数:

def plotPieChart(self, positive, wpositive, spositive, negative,
            wnegative, snegative, neutral, searchTerm, noOfSearchTerms):
    
    labels = ['positive [' + str(positive) + '%]', 'Weakly positive ['
              + str(wpositive) + '%]','Strongly positive [' + str(spositive)
              + '%]', 'Neutral [' + str(neutral) + '%]', 'Negative ['
              + str(negative) + '%]', 'Weakly Negative [' + str(wnegative)
              + '%]', 'Strongly Negative [' + str(snegative) + '%]']
              
    sizes = [positive, wpositive, spositive, neutral, negative, wnegative, snegative]
    
    colors = ['yellowgreen','lightgreen','darkgreen', 'gold', 'red','lightsalmon','darkred']
    
    patches, texts = plt.pie(sizes, colors=colors, startangle=90)
    
    plt.legend(patches, labels, loc="best")
    
    plt.title('How people are reacting on ' + searchTerm + ' by analyzing '
        + str(noOfSearchTerms) + ' Tweets.')
    
    plt.axis('equal')
    
    plt.tight_layout()
    
    # plt.show()
    plt.savefig('app/static/images/piechart.png')


完整的stdout堆栈跟踪:

analyzer.py:114: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
  patches, texts = plt.pie(sizes, colors=colors, startangle=90)
2020-11-29 20:02:23.430 Python[48923:2857853] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff30458b57 __exceptionPreprocess + 250
    1   libobjc.A.dylib                     0x00007fff692a45bf objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff3048134c -[NSException raise] + 9
    3   AppKit                              0x00007fff2d67b5ec -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 310
    4   AppKit                              0x00007fff2d663052 -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1416
    5   AppKit                              0x00007fff2d662ac3 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 42
    6   _macosx.cpython-39-darwin.so        0x000000010b17d8a5 -[Window initWithContentRect:styleMask:backing:defer:withManager:] + 69
    7   _macosx.cpython-39-darwin.so        0x000000010b1813fd FigureManager_init + 269
    8   Python                              0x00000001002ababa wrap_init + 12
    9   Python                              0x0000000100269732 wrapperdescr_call + 316
    10  Python                              0x0000000100262b47 _PyObject_MakeTpCall + 266
    11  Python                              0x000000010030abd7 call_function + 455
    12  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    13  Python                              0x00000001002631ec function_code_fastcall + 97
    14  Python                              0x0000000100262a0b _PyObject_FastCallDictTstate + 212
    15  Python                              0x0000000100263435 _PyObject_Call_Prepend + 139
    16  Python                              0x00000001002aba1e slot_tp_init + 87
    17  Python                              0x00000001002a521b type_call + 150
    18  Python                              0x0000000100262b47 _PyObject_MakeTpCall + 266
    19  Python                              0x000000010030abd7 call_function + 455
    20  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    21  Python                              0x00000001002631ec function_code_fastcall + 97
    22  Python                              0x0000000100264e4f method_vectorcall + 164
    23  Python                              0x000000010030aba3 call_function + 403
    24  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    25  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    26  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    27  Python                              0x0000000100264e4f method_vectorcall + 164
    28  Python                              0x0000000100262ec3 PyVectorcall_Call + 146
    29  Python                              0x0000000100308064 _PyEval_EvalFrameDefault + 28268
    30  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    31  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    32  Python                              0x0000000100262ec3 PyVectorcall_Call + 146
    33  Python                              0x0000000100308064 _PyEval_EvalFrameDefault + 28268
    34  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    35  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    36  Python                              0x000000010030aba3 call_function + 403
    37  Python                              0x0000000100307de0 _PyEval_EvalFrameDefault + 27624
    38  Python                              0x00000001002631ec function_code_fastcall + 97
    39  Python                              0x000000010030aba3 call_function + 403
    40  Python                              0x0000000100307de0 _PyEval_EvalFrameDefault + 27624
    41  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    42  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    43  Python                              0x000000010030aba3 call_function + 403
    44  Python                              0x0000000100307de0 _PyEval_EvalFrameDefault + 27624
    45  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    46  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    47  Python                              0x000000010030aba3 call_function + 403
    48  Python                              0x0000000100307e9e _PyEval_EvalFrameDefault + 27814
    49  Python                              0x00000001002631ec function_code_fastcall + 97
    50  Python                              0x000000010030aba3 call_function + 403
    51  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    52  Python                              0x00000001002631ec function_code_fastcall + 97
    53  Python                              0x000000010030aba3 call_function + 403
    54  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    55  Python                              0x00000001002631ec function_code_fastcall + 97
    56  Python                              0x0000000100308064 _PyEval_EvalFrameDefault + 28268
    57  Python                              0x00000001002631ec function_code_fastcall + 97
    58  Python                              0x000000010030aba3 call_function + 403
    59  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    60  Python                              0x00000001002631ec function_code_fastcall + 97
    61  Python                              0x000000010030aba3 call_function + 403
    62  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    63  Python                              0x00000001002631ec function_code_fastcall + 97
    64  Python                              0x000000010030aba3 call_function + 403
    65  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    66  Python                              0x00000001002631ec function_code_fastcall + 97
    67  Python                              0x0000000100262a0b _PyObject_FastCallDictTstate + 212
    68  Python                              0x0000000100263435 _PyObject_Call_Prepend + 139
    69  Python                              0x00000001002aae83 slot_tp_call + 87
    70  Python                              0x0000000100262b47 _PyObject_MakeTpCall + 266
    71  Python                              0x000000010030abd7 call_function + 455
    72  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    73  Python                              0x00000001002631ec function_code_fastcall + 97
    74  Python                              0x0000000100262a0b _PyObject_FastCallDictTstate + 212
    75  Python                              0x0000000100263435 _PyObject_Call_Prepend + 139
    76  Python                              0x00000001002aae83 slot_tp_call + 87
    77  Python                              0x0000000100262b47 _PyObject_MakeTpCall + 266
    78  Python                              0x000000010030abd7 call_function + 455
    79  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    80  Python                              0x00000001002710c5 gen_send_ex + 269
    81  Python                              0x0000000100307629 _PyEval_EvalFrameDefault + 25649
    82  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    83  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    84  Python                              0x000000010030aba3 call_function + 403
    85  Python                              0x0000000100307de0 _PyEval_EvalFrameDefault + 27624
    86  Python                              0x000000010030b6eb _PyEval_EvalCode + 1998
    87  Python                              0x000000010026317c _PyFunction_Vectorcall + 248
    88  Python                              0x000000010030aba3 call_function + 403
    89  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    90  Python                              0x00000001002631ec function_code_fastcall + 97
    91  Python                              0x000000010030aba3 call_function + 403
    92  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    93  Python                              0x00000001002631ec function_code_fastcall + 97
    94  Python                              0x000000010030aba3 call_function + 403
    95  Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    96  Python                              0x00000001002631ec function_code_fastcall + 97
    97  Python                              0x000000010030aba3 call_function + 403
    98  Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    99  Python                              0x00000001002631ec function_code_fastcall + 97
    100 Python                              0x0000000100262a0b _PyObject_FastCallDictTstate + 212
    101 Python                              0x0000000100263435 _PyObject_Call_Prepend + 139
    102 Python                              0x00000001002aba1e slot_tp_init + 87
    103 Python                              0x00000001002a521b type_call + 150
    104 Python                              0x0000000100262b47 _PyObject_MakeTpCall + 266
    105 Python                              0x000000010030abd7 call_function + 455
    106 Python                              0x0000000100307d34 _PyEval_EvalFrameDefault + 27452
    107 Python                              0x00000001002631ec function_code_fastcall + 97
    108 Python                              0x000000010030aba3 call_function + 403
    109 Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    110 Python                              0x00000001002631ec function_code_fastcall + 97
    111 Python                              0x0000000100264f52 method_vectorcall + 423
    112 Python                              0x0000000100308064 _PyEval_EvalFrameDefault + 28268
    113 Python                              0x00000001002631ec function_code_fastcall + 97
    114 Python                              0x000000010030aba3 call_function + 403
    115 Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    116 Python                              0x00000001002631ec function_code_fastcall + 97
    117 Python                              0x000000010030aba3 call_function + 403
    118 Python                              0x0000000100307d18 _PyEval_EvalFrameDefault + 27424
    119 Python                              0x00000001002631ec function_code_fastcall + 97
    120 Python                              0x0000000100264edb method_vectorcall + 304
    121 Python                              0x000000010038514f t_bootstrap + 70
    122 Python                              0x0000000100344ff1 pythread_wrapper + 25
    123 libsystem_pthread.dylib             0x00007fff6a651109 _pthread_start + 148
    124 libsystem_pthread.dylib             0x00007fff6a64cb8b thread_start + 15
)
libc++abi.dylib: terminating with uncaught exception of type NSException


我遵循this answer在flask中使用matplotlib的输出。
请让我知道如何解决这个问题?

p8h8hvxi

p8h8hvxi1#

我通过使用Agg后端解决了这个问题,它是非GUI的:

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

字符串

相关问题