# 如何在 Matplotlib 中繪製資料列表的直方圖

## `plt.hist()` 方法的語法

``````matplotlib.pyplot.hist(x,
bins=None,
range=None,
density=False,
weights=None,
cumulative=False,
bottom=None,
histtype='bar',
align='mid',
orientation='vertical',
rwidth=None,
log=False,
color=None,
label=None,
stacked=False, *,
data=None,
**kwargs)
``````

### 示例：從資料列表中使用 Matplotlib 繪製直方圖

``````import matplotlib.pyplot as plt

data = [3,4,2,3,4,5,4,7,8,5,4,6,2,1,0,9,7,6,6,5,4]

n, bins, patches=plt.hist(data)
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.title("Histogram")
plt.show()
``````

``````import matplotlib.pyplot as plt

data = [3,4,2,3,4,5,4,7,8,5,4,6,2,1,0,9,7,6,6,5,4]
n, bins, patches=plt.hist(data,bins=20)
plt.xlabel("Values")
plt.ylabel("Frequency")
plt.title("Histogram with 20 bins")
plt.show()
``````

``````import matplotlib.pyplot as plt

data = [3,4,2,3,4,5,4,7,8,5,4,6,2,1,0,9,7,6,6,5,4]
n, bins, patches=plt.hist(data,bins=20,density=True)
plt.xlabel("Weight")
plt.ylabel("Probability")
plt.title("Histogram with Probability Plot")
plt.show()
``````

``````import matplotlib.pyplot as plt

data = [3,4,2,3,4,5,4,7,8,5,4,6,2,1,0,9,7,6,6,5,4]
n, bins, patches=plt.hist(data,bins=20,density=True,color='red')
plt.xlabel("Weight")
plt.ylabel("Probability")
plt.title("Red Histogram Plot")
plt.show()
``````

## 相關文章 - Matplotlib Visualizations

• Matplotlib 中的疊加條形圖