Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- plt.style.use('ggplot')
- #dataをplt.hist()にわたすだけでヒストグラムを作成
- data = np.random.randn(10000)
- plt.hist(data);
- #plt.savefig("hist.png", bbox_inches = 'tight', pad_inches = 0)
- #いろいろと設定を変えることで見た目を変えることができる。
- plt.hist(data, bins=50, density=True, alpha=0.5,
- histtype='stepfilled', color='lightgreen',
- edgecolor='m');
- #plt.savefig("hist_kai.png", bbox_inches = 'tight', pad_inches = 0)
- #2つのヒストグラムを重ねて表示することもできる
- x1 = np.random.normal(1, .8, 1000)
- x2 = np.random.normal(-1, .8, 1000)
- kwargs = dict(histtype='stepfilled', alpha=0.5, density=True, bins=50)
- plt.hist(x1, **kwargs)
- plt.hist(x2, **kwargs);
- #plt.savefig("hist_2ko.png", bbox_inches = 'tight', pad_inches = 0)
- #,棒の数。(デフォルト:10)
- n1,b1,p1 = plt.hist(data,bins=np.arange(-5,6,1),label='bins=np.arange(-5,6,1)');
- n1,b2,p2 = plt.hist(data,bins=10,label='bins=10');
- plt.legend()
- #plt.savefig("hist_bins.png", bbox_inches = 'tight', pad_inches = 0)
- b1
- array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])
- b2
- array([-3.53911134, -2.77787418, -2.01663702, -1.25539985, -0.49416269,
- 0.26707448, 1.02831164, 1.7895488 , 2.55078597, 3.31202313,
- 4.0732603 ])
- #range
- n3,b3,p3 = plt.hist(data, range=(-5,5),label='range=(-5,5)');
- n4,b4,p4 = plt.hist(data,label='range=(np.min(data),np.max(data))');
- plt.legend()
- #plt.savefig("hist_range.png", bbox_inches = 'tight', pad_inches = 0)
- b4
- array([-3.53911134, -2.77787418, -2.01663702, -1.25539985, -0.49416269,
- 0.26707448, 1.02831164, 1.7895488 , 2.55078597, 3.31202313,
- 4.0732603 ])
- #正規化
- plt.hist(data, density=True, label='density=True')
- plt.legend()
- #plt.savefig("hist_densuty.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x8342240>
- #累積ヒストグラム cumulative
- plt.hist(data, density=True,cumulative=True, label='cumulative=True')
- plt.legend()
- #plt.savefig("hist_cumulative.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x83c5240>
- #底上げ bottom
- n5,b5,p5 = plt.hist(data,bottom=3000,label='bottom=3000');
- n6,b6,p6 = plt.hist(data,label='bottom=None');
- plt.legend()
- #plt.savefig("hist_bottom.png", bbox_inches = 'tight', pad_inches = 0)
- n5==n6
- array([ True, True, True, True, True, True, True, True, True,
- True])
- data2 = np.random.randn(10000,2)
- #histtype 'bar', 'barstacked', 'step', 'stepfilled'
- plt.hist(data2,histtype='bar',label='bar')
- plt.legend()
- #plt.savefig("bar.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x82f0e10>
- plt.hist(data2,histtype='barstacked',label='barstacked')
- plt.legend()
- #plt.savefig("barstacked.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x85af860>
- plt.hist(data2,histtype='step',label='step')
- plt.legend()
- #plt.savefig("step.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x85afa20>
- plt.hist(data2,histtype='stepfilled',label='stepfilled')
- plt.legend()
- #plt.savefig("stepfilled.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x85950b8>
- #stacked
- plt.hist(data2,stacked=True,label='stacked=True')
- plt.legend()
- #plt.savefig("stacked.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x8716358>
- #align : {'left', 'mid', 'right'}, 棒が生える場所
- """
- •'left': bars are centered on the left bin edges.
- •'mid': bars are centered between the bin edges.
- •'right': bars are centered on the right bin edges.
- """
- plt.hist(data,align='left',range=(-5,5),label='align=left')
- plt.legend()
- plt.xlim(-5,5)
- #plt.savefig("align_left.png", bbox_inches = 'tight', pad_inches = 0)
- (-5, 5)
- plt.hist(data,align='mid',range=(-5,5),label='align=mid')
- plt.legend()
- plt.xlim(-5,5)
- #plt.savefig("align_mid.png", bbox_inches = 'tight', pad_inches = 0)
- (-5, 5)
- plt.hist(data,align='right',range=(-5,5),label='align=right')
- plt.legend()
- plt.xlim(-5,5)
- #plt.savefig("align_right.png", bbox_inches = 'tight', pad_inches = 0)
- (-5, 5)
- #orientation 'horizontal', 'vertical'
- plt.hist(data,orientation='horizontal',label='orientation="horizontal"')
- plt.legend()
- #plt.savefig("horizontal.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x97f5b38>
- #rwidth
- #The relative width of the bars as a fraction of the bin width
- plt.hist(data,rwidth=1,label='rwidth=1')
- plt.hist(data,rwidth=.5,label='rwidth=.5')
- plt.hist(data,rwidth=.1,label='rwidth=.1')
- plt.legend()
- #plt.savefig("rwidth.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x87e75f8>
- #log logscale y軸
- plt.hist(data,log=True,label='log=True')
- plt.legend()
- #plt.savefig("log.png", bbox_inches = 'tight', pad_inches = 0)
- <matplotlib.legend.Legend at 0x98f1940>
Add Comment
Please, Sign In to add comment