当绘制matplot图像时,Python突然停止了。 我想在index.html中显示matplot图像的表。现在我在views.py
中写道def past_result(request):
return render(request, 'index.html', {'chart': _view_plot(request)})
def _view_plot(request):
dates = [20150805,20160902,20170823]
heights = [5,6,3]
df = pd.DataFrame()
df['DATE'] = dates
df['SCORE'] = heights
col_width = 3.0
row_height = 0.625
font_size = 14
header_color = '#40466e'
row_colors = ['#f1f1f2', 'w']
edge_color = 'w'
bbox = [0, 0, 1, 1]
header_columns = 0
ax = None
if ax is None:
size = (np.array(df.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
ax.axis('off')
mpl_table = ax.table(cellText=df.values, bbox=bbox, colLabels=df.columns)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
for k, cell in six.iteritems(mpl_table._cells):
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0] % len(row_colors)])
plt.show()
return ax
index.html中的
<img src="data:image/png;base64,{{ chart }}" width="700px" height="500px" alt="RESULT"/>
当我阅读past_result方法时,Python突然停止了。我不知道为什么。为什么会发生这样的错误?我读这个网址作为参考 How to save the Pandas dataframe/series data as a figure?。我该如何解决这个问题?
Traceback is
** First throw call stack:
(
0 CoreFoundation 0x00007fffc96b737b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x00007fffde4b348d objc_exception_throw + 48
2 CoreFoundation 0x00007fffc96bc082 +[NSException raise:format:arguments:] + 98
3 Foundation 0x00007fffcb103ce0 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 Foundation 0x00007fffcb08e3b3 +[NSUndoManager(NSPrivate) _endTopLevelGroupings] + 170
5 AppKit 0x00007fffc711e8bd -[NSApplication run] + 1200
6 _macosx.so 0x000000010509c8d2 init_macosx + 32564
7 Python 0x00000001000c4fd5 PyEval_EvalFrameEx + 31605
8 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
9 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
10 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
11 Python 0x000000010003e1e0 function_call + 176
12 Python 0x000000010000c442 PyObject_Call + 98
13 Python 0x000000010001ec2d instancemethod_call + 365
14 Python 0x000000010000c442 PyObject_Call + 98
15 Python 0x000000010007a44a slot_tp_call + 74
16 Python 0x000000010000c442 PyObject_Call + 98
17 Python 0x00000001000c2198 PyEval_EvalFrameEx + 19768
18 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
19 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
20 Python 0x00000001000c540f PyEval_EvalFrameEx + 32687
21 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
22 Python 0x000000010003e1e0 function_call + 176
23 Python 0x000000010000c442 PyObject_Call + 98
24 Python 0x00000001000c2198 PyEval_EvalFrameEx + 19768
25 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
26 Python 0x000000010003e1e0 function_call + 176
27 Python 0x000000010000c442 PyObject_Call + 98
28 Python 0x00000001000c2198 PyEval_EvalFrameEx + 19768
29 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
30 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
31 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
32 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
33 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
34 Python 0x000000010003e1e0 function_call + 176
35 Python 0x000000010000c442 PyObject_Call + 98
36 Python 0x000000010001ec2d instancemethod_call + 365
37 Python 0x000000010000c442 PyObject_Call + 98
38 Python 0x000000010007a44a slot_tp_call + 74
39 Python 0x000000010000c442 PyObject_Call + 98
40 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
41 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
42 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
43 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
44 Python 0x000000010003e1e0 function_call + 176
45 Python 0x000000010000c442 PyObject_Call + 98
46 Python 0x000000010001ec2d instancemethod_call + 365
47 Python 0x000000010000c442 PyObject_Call + 98
48 Python 0x000000010007a44a slot_tp_call + 74
49 Python 0x000000010000c442 PyObject_Call + 98
50 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
51 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
52 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
53 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
54 Python 0x000000010003e1e0 function_call + 176
55 Python 0x000000010000c442 PyObject_Call + 98
56 Python 0x000000010001ec2d instancemethod_call + 365
57 Python 0x000000010000c442 PyObject_Call + 98
58 Python 0x000000010007a44a slot_tp_call + 74
59 Python 0x000000010000c442 PyObject_Call + 98
60 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
61 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
62 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
63 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
64 Python 0x000000010003e1e0 function_call + 176
65 Python 0x000000010000c442 PyObject_Call + 98
66 Python 0x000000010001ec2d instancemethod_call + 365
67 Python 0x000000010000c442 PyObject_Call + 98
68 Python 0x000000010007a44a slot_tp_call + 74
69 Python 0x000000010000c442 PyObject_Call + 98
70 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
71 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
72 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
73 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
74 Python 0x000000010003e1e0 function_call + 176
75 Python 0x000000010000c442 PyObject_Call + 98
76 Python 0x000000010001ec2d instancemethod_call + 365
77 Python 0x000000010000c442 PyObject_Call + 98
78 Python 0x000000010007a44a slot_tp_call + 74
79 Python 0x000000010000c442 PyObject_Call + 98
80 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
81 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
82 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
83 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
84 Python 0x000000010003e1e0 function_call + 176
85 Python 0x000000010000c442 PyObject_Call + 98
86 Python 0x000000010001ec2d instancemethod_call + 365
87 Python 0x000000010000c442 PyObject_Call + 98
88 Python 0x000000010007a44a slot_tp_call + 74
89 Python 0x000000010000c442 PyObject_Call + 98
90 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
91 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
92 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
93 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
94 Python 0x000000010003e1e0 function_call + 176
95 Python 0x000000010000c442 PyObject_Call + 98
96 Python 0x000000010001ec2d instancemethod_call + 365
97 Python 0x000000010000c442 PyObject_Call + 98
98 Python 0x000000010007a44a slot_tp_call + 74
99 Python 0x000000010000c442 PyObject_Call + 98
100 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
101 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
102 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
103 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
104 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
105 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
106 Python 0x000000010003e1e0 function_call + 176
107 Python 0x000000010000c442 PyObject_Call + 98
108 Python 0x000000010001ec2d instancemethod_call + 365
109 Python 0x000000010000c442 PyObject_Call + 98
110 Python 0x000000010007a44a slot_tp_call + 74
111 Python 0x000000010000c442 PyObject_Call + 98
112 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
113 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
114 Python 0x000000010003e1e0 function_call + 176
115 Python 0x000000010000c442 PyObject_Call + 98
116 Python 0x000000010001ec2d instancemethod_call + 365
117 Python 0x000000010000c442 PyObject_Call + 98
118 Python 0x000000010007a44a slot_tp_call + 74
119 Python 0x000000010000c442 PyObject_Call + 98
120 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
121 Python 0x00000001000c540f PyEval_EvalFrameEx + 32687
122 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
123 Python 0x00000001000c52cd PyEval_EvalFrameEx + 32365
124 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
125 Python 0x000000010003e1e0 function_call + 176
126 Python 0x000000010000c442 PyObject_Call + 98
127 Python 0x000000010001ec2d instancemethod_call + 365
128 Python 0x000000010000c442 PyObject_Call + 98
129 Python 0x000000010007a078 slot_tp_init + 88
130 Python 0x0000000100075ea5 type_call + 245
131 Python 0x000000010000c442 PyObject_Call + 98
132 Python 0x00000001000bfc73 PyEval_EvalFrameEx + 10259
133 Python 0x00000001000c540f PyEval_EvalFrameEx + 32687
134 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
135 Python 0x000000010003e1e0 function_call + 176
136 Python 0x000000010000c442 PyObject_Call + 98
137 Python 0x00000001000c2198 PyEval_EvalFrameEx + 19768
138 Python 0x00000001000c540f PyEval_EvalFrameEx + 32687
139 Python 0x00000001000c540f PyEval_EvalFrameEx + 32687
140 Python 0x00000001000c7ff3 PyEval_EvalCodeEx + 2131
141 Python 0x000000010003e1e0 function_call + 176
142 Python 0x000000010000c442 PyObject_Call + 98
143 Python 0x000000010001ec2d instancemethod_call + 365
144 Python 0x000000010000c442 PyObject_Call + 98
145 Python 0x00000001000bc1a7 PyEval_CallObjectWithKeywords + 87
146 Python 0x0000000100106897 t_bootstrap + 71
147 libsystem_pthread.dylib 0x00007fffdefb19af _pthread_body + 180
148 libsystem_pthread.dylib 0x00007fffdefb18fb _pthread_body + 0
149 libsystem_pthread.dylib 0x00007fffdefb1101 thread_start + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException