我寫了一個函數來繪製一維數組theta的直方圖。但是我不喜歡這個函數的一點是數據在代碼中。你能知道如何將數據保存在文件中,並使其功能從文件中讀取?由於數據通常要大得多。使用python繪製直方圖時從文件讀取數據的方法?
PS:該代碼是
#hist.py
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
theta=[77.438110,82.811340,59.701530,124.94859,82.991272,84.300468,24.639610,112.28130]
num_bins = 72
# the histogram of the data
n, bins, patches = plt.hist(theta, num_bins, range=[0,180], normed = True, histtype='bar',facecolor='green')
plt.xlabel(r'$\theta$($\degree$)')
plt.ylabel(r'$P(\theta)$')
plt.show()
從直方圖中讀取數據是什麼意思?你已經在變量'n'和'bin'中使用了... –
@jiadong,數據來自何處? –
@SaulloCastro如果數組theta包含更大數量的條目,例如10000,那麼用這種方式定義它'theta = [77.438110,...,112.28130]'是不好的。我將theta的元素保存在另一個文件中,然後我想找到如何從文件中讀取數據。 – jiadong