简体   繁体   English

使用 matplotlib 绘制图形时出现间歇性错误“RuntimeError: main thread is not in main loop”

[英]Intermittent error while plotting graph using matplotlib “RuntimeError: main thread is not in main loop”

I am trying to pull data from different end points using my python code and feeding the same data to plot a graph using matplotlib.我正在尝试使用我的 python 代码从不同的端点提取数据并提供相同的数据以使用 matplotlib 绘制图形。 I dont have any problem in reading the data but when i invoke the methods to plot graph by feeding the data i see intermittent error caused by matplot lib.我在读取数据时没有任何问题,但是当我通过提供数据调用绘制图形的方法时,我看到由 matplot lib 引起的间歇性错误。 below are the error details.以下是错误详情。

Traceback (most recent call last):
  File "C:\Python35\lib\site-packages\slackbot\dispatcher.py", line 55, in _dispatch_msg_handler
    func(Message(self._client, msg), *args)
  File "C:\PycharmProjects\SlackBot\src\plugins\bot_response.py", line 248, in checkmarx
    draw_chart.riskscore_bar(top_riskscore, project_name, "output_files", "riskscore_bar.png")
  File "C:\PycharmProjects\SlackBot\src\drawchart.py", line 111, in riskscore_bar
    fig, ax = plt.subplots()
  File "C:\Python35\lib\site-packages\matplotlib\pyplot.py", line 1202, in subplots
    fig = figure(**fig_kw)
  File "C:\Python35\lib\site-packages\matplotlib\pyplot.py", line 535, in figure
    **kwargs)
  File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 98, in new_figure_manager_given_figure
    icon_img = Tk.PhotoImage(file=icon_fname)
  File "C:\Python35\lib\tkinter\__init__.py", line 3403, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Python35\lib\tkinter\__init__.py", line 3359, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
RuntimeError: main thread is not in main loop

I have tried looking into other cases from stackoverflow with same error messages, but it didnt help me fix this.我曾尝试从具有相同错误消息的 stackoverflow 中查看其他情况,但它没有帮助我解决此问题。 Below is my code snippet that invokes an error.下面是我调用错误的代码片段。

def riskscore_bar(self, top_riskscore, project_id, output_folder, output_filename): def riskscore_bar(self, top_riskscore, project_id, output_folder, output_filename):

logger.debug("Inside method plotgraph in drawchart.py.")

y_pos = np.arange(len(project_id))
width = .4

fig, ax = plt.subplots()
graph = ax.bar(y_pos+1, top_riskscore, width, color='#feb308')
ax.set_ylabel('Risk Score')
ax.set_title('Summary')
ax.set_xticks(y_pos + 1)
ax.set_xticklabels(project_id,fontsize=5, rotation=45 )

def autolabel(rects):
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width()/2., 1.001*height,
                '%d' % int(height),
                ha='center', va='bottom')

autolabel(graph)
pylab.savefig(os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', output_folder,output_filename))

The error seems to occur at "fig, ax = plt.subplots()".错误似乎发生在“fig, ax = plt.subplots()”。 any ideas on how this can be fixed?关于如何解决这个问题的任何想法?

this can be solved by adding plt.switch_backend('agg') below right after you import matplotlib.pyplot as plt.这可以通过在将 matplotlib.pyplot 作为 plt 导入后立即添加 plt.switch_backend('agg') 来解决。 As shown below如下所示

import matplotlib.pyplot as plt    
plt.switch_backend('agg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误:主线程不在主循环中使用带有 Django 的 Matplotlib - RuntimeError: main thread is not in main loop using Matplotlib with Django Matplotlib 和 :RuntimeError: 主线程不在主循环中: - Matplotlib and :RuntimeError: main thread is not in main loop: RuntimeError:使用tkinter.simpledialog时主线程不在主循环中 - RuntimeError: main thread is not in main loop while using tkinter.simpledialog RuntimeError:主线程不在主循环中-绘制两个图 - RuntimeError: main thread is not in main loop-Plotting in two figures RuntimeError:主线程不在主循环中 - RuntimeError: main thread is not in main loop 间歇性的Python线程错误,“主线程不在主循环中” - Intermittent Python thread error, “main thread is not in main loop” (Python tkinter):RuntimeError:主线程不在主循环中 - (Python tkinter): RuntimeError: main thread is not in main loop kochat in use RuntimeError: 主线程不在主循环中 - kochat in use RuntimeError: main thread is not in main loop PySimpleGUI。 运行时错误:主线程不在主循环中 - PySimpleGUI. RuntimeError: main thread is not in main loop 调试 Tkinter 代码给出“RuntimeError:主线程不在主循环中” - Debugging Tkinter code gives "RuntimeError: main thread is not in main loop"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM