변수 이름을 통과 할 수있는 일종의 루프를 만들면이 코드를 수정하여 'data1이 None'이 아닌 부분을 적어 두지 않아야합니까? 함수에 대한 입력 수가 어떻게 든 변할 수있는 일종의 동적 코드를 만들 수있는 방법이 있는지 궁금합니다. 예를 들어 100 개의 다른 데이터 집합을 입력한다고 가정 해 봅시다. 그들 모두는 기능에 대한 정보를 제공 할 부분에서 무엇을해야합니까? 또한 어떻게 두 플롯에 제목을 둘 수 있습니까? 왜냐하면 plt.title()을 사용할 때 마지막 제목 만 표시하기 때문입니다.어떻게 파이썬에서 입력 변수를 통과하는 루프를 만들 수 있습니까?
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(4)
randomSet = np.random.randint(0, 2, (10, 20))
np.random.seed(3)
randomSet3 = np.random.randint(0, 2, (10, 20))
np.random.seed(2)
randomSet2 = np.random.randint(0, 2, (10, 20))
np.random.seed(1)
randomSet1 = np.random.randint(0, 2, (10, 20))
def showResult(data, data1 = None, data2 = None, data3 = None, data4 = None, data5 = None, nscan = 1):
#index = 0
total = np.zeros(data.shape[0]*data.shape[1])
dataList = [data.reshape(data.shape[0]*data.shape[1])]
if data1 is not None:
dataList.append(data1.reshape(data1.shape[0]*data1.shape[1]))
if data2 is not None:
dataList.append(data2.reshape(data2.shape[0]*data2.shape[1]))
if data3 is not None:
dataList.append(data3.reshape(data3.shape[0]*data3.shape[1]))
if data4 is not None:
dataList.append(data4.reshape(data4.shape[0]*data4.shape[1]))
if data5 is not None:
dataList.append(data5.reshape(data5.shape[0]*data5.shape[1]))
#total = copy.copy(data)
for i in range(nscan):
total += dataList[i]
fig = plt.figure(figsize = (8, 10))
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)
ax1.imshow(total.reshape(data.shape[0], data.shape[1]), cmap= 'gray', interpolation= 'nearest')
#plt.title('Image')
ax2.hist(total)
#plt.title('Histogram')
plt.show()
return total
showResult(randomSet, randomSet1, randomSet, randomSet3, randomSet, randomSet2, nscan= 6)
출력은 다음과 같아야합니다
array([ 1., 2., 5., 4., 4., 2., 4., 3., 2., 5., 0., 3., 5.,
6., 2., 5., 5., 5., 0., 0., 0., 2., 2., 1., 2., 0.,
4., 0., 5., 4., 4., 4., 1., 6., 2., 1., 3., 1., 4.,
1., 2., 4., 1., 3., 5., 3., 1., 5., 2., 4., 4., 1.,
1., 3., 1., 6., 3., 5., 5., 1., 3., 5., 4., 1., 4.,
3., 5., 5., 4., 5., 2., 1., 4., 1., 2., 1., 6., 3.,
2., 4., 5., 1., 1., 2., 5., 3., 2., 5., 3., 2., 3.,
3., 4., 1., 4., 2., 5., 2., 4., 5., 5., 5., 1., 4.,
5., 0., 4., 1., 5., 1., 5., 2., 2., 2., 1., 3., 1.,
1., 3., 1., 3., 3., 5., 5., 5., 2., 2., 1., 4., 5.,
2., 5., 2., 3., 2., 0., 0., 5., 5., 5., 2., 2., 1.,
1., 4., 4., 4., 2., 5., 2., 4., 5., 4., 2., 2., 1.,
4., 4., 2., 4., 4., 1., 4., 3., 5., 0., 1., 2., 3.,
0., 5., 3., 2., 2., 2., 4., 4., 2., 4., 0., 5., 5.,
2., 3., 0., 1., 1., 5., 3., 1., 3., 5., 1., 2., 3.,
5., 5., 2., 2., 5.])
Welcome to StackOverflow. 도움말 설명서의 게시 지침을 읽고 따르십시오. [최소한의 완전하고 검증 가능한 예제] (http://stackoverflow.com/help/mcve)가 여기에 적용됩니다. – Prune
주제별로 한 가지 질문을합니다. 어쨌든, 다양한'randomSet'은 언제나 같은 양의 데이터 포인트를 가지고 있습니까? 세트 사이에 RNPG를 뿌릴 이유가 있습니까? '스캔들 '의 핵심은 무엇입니까? – Reti43
하나 이상의 질문에 대해 사과드립니다. 모든 데이터 세트는 0과 1로 만들어지며 동일한 치수를 가지며 기본적으로 동일한 샘플에서 수집되지만 장치의 노이즈 또는 부정확성으로 인해 일부 데이터가 다릅니다. nscans는 기본적으로 데이터가 수집 된 횟수를 알기 때문에 같은 시간에 루프를 사용할 수 있습니다. –