私はPythonを初めて使用しています。私はスタンドの販売をシミュレートするために次のコードを実行しようとしています。このコードでは、calculateTips、calculateProfit関数を使用して、発生する可能性に基づいて売上のヒントと利益を予測します。 summarizeData関数は、主に入力データのヒストグラムをプロットするために使用されます。 summariseDataはコード内で約5回使用されるので、5つの異なるプロットがあるはずです。python - 複数のプロットを描画する関数を使用しますか?
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import numpy.random as nr
def summariseData(distribution, name = 'distribution name'):
ser = pd.Series(distribution)
plt.figure()
plt.hist(ser, bins = 120)
plt.title('Frequency distribution of ' + name)
plt.ylabel('Frequency')
plt.show()
print('')
out = ser.describe()
##plt.show(block = True)
return out
def calculateProfit(num):
profit = nr.uniform(size = num)
out = [5 if x < 0.3 else (3.5 if x < 0.6 else 4) for x in profit]
return out
def calculateTips(num):
tips = nr.uniform(size = num)
out = [0 if x < 0.5 else (0.25 if x < 0.7
else (1 if x < 0.9 else 2)) for x in tips]
return out
def finalSimulation(num, mean = 600, std = 30):
arrival = nr.normal(loc = mean, size = num, scale = std)
profit = calculateProfit(num)
print(summariseData(profit, name = 'profit per arrival'))
totalProfit = arrival * profit
print(summariseData(totalProfit, name = 'total profit per day'))
tip = calculateTips(num)
print(summariseData(tip, name = 'tips per arrivals'))
totalTip = arrival * tip
print(summariseData(totalTip, name = 'total tips per day'))
totalGain = totalProfit + totalTip
return summariseData(totalGain, name = 'net gain per day')
if __name__ == "__main__" :
finalSimulation(100000)
Eclipseでコードを実行すると、最初のプロットが表示されます。しかし、次のプロットを見るためには、現在のプロット(画面に表示されている)を閉じる必要があります。
問題点:コードを実行すると、プロットがさまざまな図形で表示されるようになります(すべて一緒に表示したい)。そのうちの1人を閉じて次の人を見ることはできません。
ありがとうございます。最後に、各
plt.hist(normal args)
、次いで、その後
plt.figure(1)
とすべてのあなたの通常の属性、ylabelの、は、xlabel、など :
感謝。しかし、私はplt.show()をどこに置くべきか分からないのですか?私はコードの最後に置いてみましたが、何も変わりませんでした。今、私はfig_numをsummariseDataに追加しました。あなたの例に似ています。 – Soroush
私はそれを__main__に入れて、最後に実行します。それぞれの数字に新しい数字を教えていますか? –
うん、コードは今このようになります:def summariseData(distribution、figure_number、name = '配布名'):plt.figure(figure_number) そしてコードの残りの部分。 – Soroush