0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

エクセルファイル(.xlsx)からヒストグラムを作成

Posted at

#環境
macOS Cataina
10.15.7

python 3.6.2

#ソースコード

2xy_hist.py
import matplotlib.pyplot as plt
import xlrd
import numpy
import matplotlib.ticker as tick # 目盛り操作に必要なライブラリを読み込み 
import sys

plt.rcParams["font.size"] = 14

if __name__ == '__main__':
    book = xlrd.open_workbook('2xy.xlsx')  # ブック名
    sheet1 = book.sheet_by_name('Sheet1') 


    X = []
    Y = []
    Z = []

##################シート読み込み#############                                               
    # セルを cell(行番号, 列番号) で指定(0から数える)                                         \
    for row in range(0, sheet1.nrows):
        valuex = sheet1.cell(row, 0).value
        if  valuex  != '':
            X.append(valuex)
    X = numpy.array(X)

    for row in range(0, sheet1.nrows):
        valuey = sheet1.cell(row, 1).value
        if  valuey  != '':
            Y.append(valuey)
    Y = numpy.array(Y)


#3講座目がある場合
#    for row in range(0, sheet1.nrows):
#        valuez = sheet1.cell(row, 2).value
#        if  valuez  != '':
#            Z.append(valuez)
#    Z = numpy.array(Z)

fig2, axes = plt.subplots(2, 2)

print(X)
print(Y)
print(Z)

#sys.exit()
#################xのヒストグラム############
#plt.hist(X, bins=20,color="0.3", range=(0,100))  
axes[0,0].hist(X, bins=20,color="0.3", range=(0,100))
axes[0,0].grid(linestyle="--",which="minor")
axes[0,0].grid(which='major',color='black',linestyle='--')
#axes[0,0].set_title('2x')
axes[0,0].set_xlabel('score')
axes[0,0].set_ylabel('frequency')  
axes[0,0].set_xlim(0,100)
axes[0,0].set_ylim(0,10)
axes[0,0].xaxis.set_minor_locator(tick.MultipleLocator(5)) 
axes[0,0].yaxis.set_minor_locator(tick.MultipleLocator(1))
muX = numpy.mean(X)
meX = numpy.median(X)
sigmaX = numpy.std(X) 
varianceX = numpy.var(X)
# 平均値の線
axes[0,0].axvline(muX, linewidth=2, color='k',label="average",linestyle='solid')
#中央値                                                                                                                    
axes[0,0].axvline(meX, linewidth=2, color='k',label="median",linestyle='dashdot')                                        
axes[0,0].legend(loc='upper right')
axes[0,0].text(0,10.1, r'''
2x,μ=%.2f, me=%.2f, σ=%.2f''' %(muX, meX, sigmaX))   


#plt.show()

#################yのヒストグラム############
#plt.subplot(2,2,3)
#plt.hist(Y, bins=20,color="0.3", range=(0,100))
axes[0,1].hist(Y, bins=20,color="0.3", range=(0,100))
axes[0,1].grid(linestyle="--",which="minor")
axes[0,1].grid(which='major',color='black',linestyle='--')
#axes[0,1].set_title('2y')
#axes[0,1].set_xlabel('score')
#axes[0,1].set_ylabel('frequency')
axes[0,1].set_xlim(0,100)
axes[0,1].set_ylim(0,10)
axes[0,1].xaxis.set_minor_locator(tick.MultipleLocator(5))
axes[0,1].yaxis.set_minor_locator(tick.MultipleLocator(1))
muY = numpy.mean(Y)
meY = numpy.median(Y)
sigmaY = numpy.std(Y)
varianceY = numpy.var(Y)
# 平均値の線  
axes[0,1].axvline(muY, linewidth=2, color='k',label="average",linestyle='solid')
#中央値
axes[0,1].axvline(meY, linewidth=2, color='k',label="median",linestyle='dashdot')
axes[0,1].legend(loc='upper right')
axes[0,1].text(0,10.1, r'''
2y,μ=%.2f, me=%.2f, σ=%.2f''' %(muY, meY, sigmaY))


#3講座目がある場合
##################zのヒストグラム############
#axes[1,0].hist(Z, bins=20,color="0.3", range=(0,100))
##plt.hist(Z, bins=20,color="0.3", range=(0,100))                                           
##sys.exit()                                                                                 
#axes[1,0].grid(linestyle="--",which="minor")
#axes[1,0].grid(which='major',color='black',linestyle='--')
##axes[1,0].set_title('2z')
#axes[1,0].set_xlabel('score')
#axes[1,0].set_ylabel('frequency')
#axes[1,0].set_xlim(0,100)
#axes[1,0].set_ylim(0,10)
#axes[1,0].xaxis.set_minor_locator(tick.MultipleLocator(5))
#axes[1,0].yaxis.set_minor_locator(tick.MultipleLocator(1))
#muZ = numpy.mean(Z)
#meZ = numpy.median(Z)
#sigmaZ = numpy.std(Z)
#varianceZ = numpy.var(Z)
# 平均値の線                                                                                                                                                               
#axes[1,0].axvline(muZ, linewidth=2, color='k',label="average",linestyle='solid')
#中央値                                                                                                                                                                    
#axes[1,0].axvline(meZ, linewidth=2, color='k',label="median",linestyle='dashdot')
#axes[1,0].legend(loc='upper right')
#axes[1,0].text(0,10.1, r'''                                                                                                                                                
#2z,μ=%.2f, me=%.2f, σ=%.2f''' %(muZ, meZ, sigmaZ))

axes[1,0].axis('off')

#################xyzのヒストグラム############                                                                                                          
Z = numpy.append(Z, X)
Z = numpy.append(Z, Y)
print(Z) # [1, 2, 3, 4, 5] 
axes[1,1].hist(Z, bins=20,color="0.3", range=(0,100))
axes[1,1].grid(linestyle="--",which="minor")
axes[1,1].grid(which='major',color='black',linestyle='--')
#axes[1,1].set_title('2xy')
axes[1,1].set_xlabel('score')
axes[1,1].set_ylabel('frequency')
axes[1,1].set_xlim(0,100)
#axes[1,1].xticks(range(5, 10))
axes[1,1].set_ylim(0,20)
axes[1,1].xaxis.set_minor_locator(tick.MultipleLocator(5))
axes[1,1].yaxis.set_minor_locator(tick.MultipleLocator(1))
muZ = numpy.mean(Z)
meZ = numpy.median(Z)
sigmaZ = numpy.std(Z)
varianceZ = numpy.var(Z)
# 平均値の線
axes[1,1].axvline(muZ, linewidth=2, color='k',label="average",linestyle='solid')
#中央値
axes[1,1].axvline(meZ, linewidth=2, color='k',label="median",linestyle='dashdot')
axes[1,1].legend(loc='upper right')
axes[1,1].text(0,20.1, r'''
2xy,μ=%.2f, me=%.2f, σ=%.2f''' %(muZ, meZ, sigmaZ))
plt.show()

完成しました
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?