使用matplotlib绘制图形时出现间歇性错误"RuntimeError:主线程不在主循环中" [英] Intermittent error while plotting graph using matplotlib "RuntimeError: main thread is not in main loop"
问题描述
我正在尝试使用我的 python 代码从不同的端点提取数据,并使用 matplotlib 提供相同的数据来绘制图形.我在读取数据时没有任何问题,但是当我通过提供数据调用绘制图形的方法时,我看到由 matplot lib 引起的间歇性错误.下面是错误的详细信息.
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. 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. 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
我曾尝试从具有相同错误消息的 stackoverflow 中查看其他情况,但它没有帮助我解决此问题.下面是调用错误的代码段.
I have tried looking into other cases from stackoverflow with same error messages, but it didnt help me fix this. Below is my code snippet that invokes an error.
def riskcore_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))
该错误似乎发生在"fig,ax = plt.subplots()"处.关于如何解决这个问题的任何想法?
The error seems to occur at "fig, ax = plt.subplots()". any ideas on how this can be fixed?
推荐答案
这可以通过在将 matplotlib.pyplot 导入为 plt 后立即在下方添加 plt.switch_backend('agg') 来解决.如下图
this can be solved by adding plt.switch_backend('agg') below right after you import matplotlib.pyplot as plt. As shown below
import matplotlib.pyplot as plt
plt.switch_backend('agg')
这篇关于使用matplotlib绘制图形时出现间歇性错误"RuntimeError:主线程不在主循环中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!