質問編集履歴
2
きれいに整形
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
fig = plt.figure()
|
|
21
21
|
axis = fig.add_subplot(1, 1, 1, xlim=tray_size[0], ylim=tray_size[1], aspect='equal')
|
|
22
22
|
for dish in ind:
|
|
23
|
-
axis.add_patch(dish.fig)
|
|
23
|
+
axis.add_patch(dish.fig)#<-----ここでエラーが発生
|
|
24
24
|
axis.axis("off")
|
|
25
25
|
plt.savefig('tmp.png')
|
|
26
26
|
fig.delaxes(axis)
|
1
きれいに整形
title
CHANGED
|
File without changes
|
body
CHANGED
|
@@ -2,7 +2,47 @@
|
|
|
2
2
|
pythonを使ってグラフをいっぱい書くプログラムを試しているのですが.
|
|
3
3
|
RuntimeError: Can not put single artist in more than one figure
|
|
4
4
|
というエラーが起きて困っています.
|
|
5
|
+
# 問題箇所
|
|
6
|
+
https://stackoverrun.com/ja/q/12300089
|
|
7
|
+
こちらのリンクでplt.figureを複数設定して解決されている話はあるのですが,
|
|
8
|
+
今のプログラムでうまくplt.figureを複数生成する方法が思いつきません.
|
|
9
|
+
```
|
|
10
|
+
def evalOneMax(ind):
|
|
11
|
+
"""Objective function."""
|
|
12
|
+
evaluation = 0
|
|
13
|
+
for dish in ind:
|
|
14
|
+
evaluation += dish.calorie
|
|
15
|
+
# TODO:グラフ領域を設定
|
|
16
|
+
|
|
17
|
+
#TODO:いい感じにトレーのサイズを外から引数として撮ってくるやり方がわからない直接書いた
|
|
18
|
+
|
|
19
|
+
tray_size = np.array([370, 260])
|
|
20
|
+
fig = plt.figure()
|
|
21
|
+
axis = fig.add_subplot(1, 1, 1, xlim=tray_size[0], ylim=tray_size[1], aspect='equal')
|
|
22
|
+
for dish in ind:
|
|
23
|
+
axis.add_patch(dish.fig)
|
|
24
|
+
axis.axis("off")
|
|
5
|
-
''
|
|
25
|
+
plt.savefig('tmp.png')
|
|
26
|
+
fig.delaxes(axis)
|
|
27
|
+
plt.clf()
|
|
28
|
+
plt.cla()
|
|
29
|
+
plt.close()
|
|
30
|
+
del axis
|
|
31
|
+
gray_img = cv2.imread('tmp.png')
|
|
32
|
+
ind.img = gray_img
|
|
33
|
+
ret, dishes_area_image = cv2.threshold(gray_img, 200, 255, cv2.THRESH_BINARY)#図形がある
|
|
34
|
+
dishes_area = cv2.countNonZero(cv2.bitwise_not(cv2.cvtColor(dishes_area_image, cv2.COLOR_RGB2GRAY)))
|
|
35
|
+
cv2.imwrite('dishes_area.png',dishes_area_image)
|
|
36
|
+
ret, dishes_overlap_area_image = cv2.threshold(gray_img, 100, 255, cv2.THRESH_BINARY)#重なってて黒い
|
|
37
|
+
cv2.imwrite('dishes_overlap_area.png',dishes_overlap_area_image)
|
|
38
|
+
dishes_overlap_area = cv2.countNonZero(cv2.bitwise_not(cv2.cvtColor(dishes_overlap_area_image, cv2.COLOR_RGB2GRAY)))
|
|
39
|
+
overlap_per = 1 - dishes_overlap_area/dishes_area
|
|
40
|
+
|
|
41
|
+
return evaluation * overlap_per
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
# プログラム全体
|
|
45
|
+
```
|
|
6
46
|
import random
|
|
7
47
|
import numpy as np
|
|
8
48
|
from operator import attrgetter
|
|
@@ -234,4 +274,5 @@
|
|
|
234
274
|
|
|
235
275
|
if __name__ == "__main__":
|
|
236
276
|
main()
|
|
277
|
+
|
|
237
|
-
|
|
278
|
+
```
|