# Pandas DataFrame DataFrame.plot.hist() 函式

Minahil Noor 2023年1月30日 2020年11月7日

Python Pandas `DataFrame.plot.hist()` 函式繪製了一個 `DataFrame` 的列的單一直方圖。直方圖以圖形的形式表示資料。它可以建立範圍的條形圖。越高的條形圖表明有更多的資料落入這個條形圖的範圍。

## `pandas.DataFrame.plot.hist()` 語法

``````DataFrame.sample(by=None,
bins=10,
**kwargs)
``````

### 引數

`by` 它是一個字串或序列。它代表 `DataFrame` 中要分組的列。
`bins` 它是一個整數。它表示直方塊的數量。一個直方塊就像一個範圍，例如，0-5，6-10 等。
`**kwargs` 這些是自定義直方圖的附加關鍵字引數。你可以在這裡檢視更多資訊。

## 示例程式碼：`DataFrame.plot.hist()`

``````import pandas as pd
dataframe = pd.DataFrame({'Value':[100, 200, 300]})
print(dataframe)
``````

`````` Value
0  100
1  200
2  300
``````

``````import pandas as pd
from matplotlib import pyplot as plt

dataframe = pd.DataFrame({'Value':[100, 200, 300]})

histogram = dataframe.plot.hist()
print(histogram)
plt.show()
``````

``````AxesSubplot(0.125,0.125;0.775x0.755)
``````

## 示例程式碼：`DataFrame.plot.hist()` 繪製複雜的直方圖

``````import pandas as pd
import numpy as np

dataframe = pd.DataFrame(
np.random.randint(0,200,size=(200, 3)),
columns=list('ABC'))

print(dataframe)
``````

`````` A    B    C
0     15  163  163
1     29    7   54
2    195   40    6
3    183   92   57
4     72  167   40
..   ...  ...  ...
195   79   35    7
196  122   79  142
197  121   46  124
198  138  141  114
199  148   95  129

[200 rows x 3 columns]
``````

``````import pandas as pd
import numpy as np

from matplotlib import pyplot as plt

dataframe = pd.DataFrame(
np.random.randint(0,200,size=(200, 3)),
columns=list('ABC'))

histogram = dataframe.plot.hist()
print(histogram)
plt.show()
``````

``````AxesSubplot(0.125,0.125;0.775x0.755)
``````

## 示例程式碼： `DataFrame.plot.hist()` 改變 `bin` 數量

``````import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

dataframe = pd.DataFrame(
np.random.randint(0,200,size=(200, 3)),
columns=list('ABC'))

histogram = dataframe.plot.hist(bins= 2)
print(histogram)
plt.show()
``````

``````AxesSubplot(0.125,0.125;0.775x0.755)
``````

``````import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

dataframe = pd.DataFrame(
np.random.randint(0,200,size=(200, 3)),
columns=list('ABC'))

histogram = dataframe.plot.hist(bins= 50)
print(histogram)
plt.show()
``````

``````AxesSubplot(0.125,0.125;0.775x0.755)
``````