Guest User

Untitled

a guest
Feb 15th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. plt.style.use('ggplot')
  4.  
  5. #dataをplt.hist()にわたすだけでヒストグラムを作成
  6. data = np.random.randn(10000)
  7. plt.hist(data);
  8. #plt.savefig("hist.png", bbox_inches = 'tight', pad_inches = 0)
  9.  
  10. #いろいろと設定を変えることで見た目を変えることができる。
  11. plt.hist(data, bins=50, density=True, alpha=0.5,
  12. histtype='stepfilled', color='lightgreen',
  13. edgecolor='m');
  14. #plt.savefig("hist_kai.png", bbox_inches = 'tight', pad_inches = 0)
  15.  
  16. #2つのヒストグラムを重ねて表示することもできる
  17. x1 = np.random.normal(1, .8, 1000)
  18. x2 = np.random.normal(-1, .8, 1000)
  19. kwargs = dict(histtype='stepfilled', alpha=0.5, density=True, bins=50)
  20.  
  21. plt.hist(x1, **kwargs)
  22. plt.hist(x2, **kwargs);
  23. #plt.savefig("hist_2ko.png", bbox_inches = 'tight', pad_inches = 0)
  24.  
  25. #,棒の数。(デフォルト:10)
  26. n1,b1,p1 = plt.hist(data,bins=np.arange(-5,6,1),label='bins=np.arange(-5,6,1)');
  27. n1,b2,p2 = plt.hist(data,bins=10,label='bins=10');
  28. plt.legend()
  29. #plt.savefig("hist_bins.png", bbox_inches = 'tight', pad_inches = 0)
  30. b1
  31. array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5])
  32. b2
  33. array([-3.53911134, -2.77787418, -2.01663702, -1.25539985, -0.49416269,
  34. 0.26707448, 1.02831164, 1.7895488 , 2.55078597, 3.31202313,
  35. 4.0732603 ])
  36.  
  37. #range
  38. n3,b3,p3 = plt.hist(data, range=(-5,5),label='range=(-5,5)');
  39. n4,b4,p4 = plt.hist(data,label='range=(np.min(data),np.max(data))');
  40. plt.legend()
  41. #plt.savefig("hist_range.png", bbox_inches = 'tight', pad_inches = 0)
  42. b4
  43. array([-3.53911134, -2.77787418, -2.01663702, -1.25539985, -0.49416269,
  44. 0.26707448, 1.02831164, 1.7895488 , 2.55078597, 3.31202313,
  45. 4.0732603 ])
  46.  
  47. #正規化
  48. plt.hist(data, density=True, label='density=True')
  49. plt.legend()
  50. #plt.savefig("hist_densuty.png", bbox_inches = 'tight', pad_inches = 0)
  51. <matplotlib.legend.Legend at 0x8342240>
  52.  
  53. #累積ヒストグラム cumulative
  54. plt.hist(data, density=True,cumulative=True, label='cumulative=True')
  55. plt.legend()
  56. #plt.savefig("hist_cumulative.png", bbox_inches = 'tight', pad_inches = 0)
  57. <matplotlib.legend.Legend at 0x83c5240>
  58.  
  59. #底上げ bottom
  60. n5,b5,p5 = plt.hist(data,bottom=3000,label='bottom=3000');
  61. n6,b6,p6 = plt.hist(data,label='bottom=None');
  62. plt.legend()
  63. #plt.savefig("hist_bottom.png", bbox_inches = 'tight', pad_inches = 0)
  64. n5==n6
  65. array([ True, True, True, True, True, True, True, True, True,
  66. True])
  67.  
  68. data2 = np.random.randn(10000,2)
  69. #histtype 'bar', 'barstacked', 'step', 'stepfilled'
  70. plt.hist(data2,histtype='bar',label='bar')
  71. plt.legend()
  72. #plt.savefig("bar.png", bbox_inches = 'tight', pad_inches = 0)
  73. <matplotlib.legend.Legend at 0x82f0e10>
  74.  
  75. plt.hist(data2,histtype='barstacked',label='barstacked')
  76. plt.legend()
  77. #plt.savefig("barstacked.png", bbox_inches = 'tight', pad_inches = 0)
  78. <matplotlib.legend.Legend at 0x85af860>
  79.  
  80. plt.hist(data2,histtype='step',label='step')
  81. plt.legend()
  82. #plt.savefig("step.png", bbox_inches = 'tight', pad_inches = 0)
  83. <matplotlib.legend.Legend at 0x85afa20>
  84.  
  85. plt.hist(data2,histtype='stepfilled',label='stepfilled')
  86. plt.legend()
  87. #plt.savefig("stepfilled.png", bbox_inches = 'tight', pad_inches = 0)
  88. <matplotlib.legend.Legend at 0x85950b8>
  89.  
  90. #stacked
  91. plt.hist(data2,stacked=True,label='stacked=True')
  92. plt.legend()
  93. #plt.savefig("stacked.png", bbox_inches = 'tight', pad_inches = 0)
  94. <matplotlib.legend.Legend at 0x8716358>
  95.  
  96. #align : {'left', 'mid', 'right'}, 棒が生える場所
  97. """
  98. •'left': bars are centered on the left bin edges.
  99. •'mid': bars are centered between the bin edges.
  100. •'right': bars are centered on the right bin edges.
  101. """
  102.  
  103. plt.hist(data,align='left',range=(-5,5),label='align=left')
  104. plt.legend()
  105. plt.xlim(-5,5)
  106. #plt.savefig("align_left.png", bbox_inches = 'tight', pad_inches = 0)
  107. (-5, 5)
  108.  
  109. plt.hist(data,align='mid',range=(-5,5),label='align=mid')
  110. plt.legend()
  111. plt.xlim(-5,5)
  112. #plt.savefig("align_mid.png", bbox_inches = 'tight', pad_inches = 0)
  113. (-5, 5)
  114.  
  115. plt.hist(data,align='right',range=(-5,5),label='align=right')
  116. plt.legend()
  117. plt.xlim(-5,5)
  118. #plt.savefig("align_right.png", bbox_inches = 'tight', pad_inches = 0)
  119. (-5, 5)
  120.  
  121. #orientation 'horizontal', 'vertical'
  122. plt.hist(data,orientation='horizontal',label='orientation="horizontal"')
  123. plt.legend()
  124. #plt.savefig("horizontal.png", bbox_inches = 'tight', pad_inches = 0)
  125. <matplotlib.legend.Legend at 0x97f5b38>
  126.  
  127. #rwidth
  128. #The relative width of the bars as a fraction of the bin width
  129. plt.hist(data,rwidth=1,label='rwidth=1')
  130. plt.hist(data,rwidth=.5,label='rwidth=.5')
  131. plt.hist(data,rwidth=.1,label='rwidth=.1')
  132. plt.legend()
  133. #plt.savefig("rwidth.png", bbox_inches = 'tight', pad_inches = 0)
  134. <matplotlib.legend.Legend at 0x87e75f8>
  135.  
  136. #log logscale y軸
  137. plt.hist(data,log=True,label='log=True')
  138. plt.legend()
  139. #plt.savefig("log.png", bbox_inches = 'tight', pad_inches = 0)
  140. <matplotlib.legend.Legend at 0x98f1940>
Add Comment
Please, Sign In to add comment