## Matplot of Python -- 01. Drawing stack bar graph

lovejobs 2020-11-15 18:19:57
matplot python drawing stack graph

1.Matplotlib yes python A drawing library of , You can easily draw various icons , It's a powerful tool for data visualization .

2. In this paper, I will introduce the drawing of bar chart or bar chart

3. The code is as follows ：

《1》 Import module first

`1 import matplotlib.pyplot as plt`

《2》 Prepare the data

```labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]```

《3》 Set width

`width = 0.35 # The width of the bar chart `

《4》 Get subgraph object

`fig,ax = plt.subplots()`

《5》 Draw the first level histogram and the second level histogram

```ax.bar(labels, men_means,width,yerr=men_std,label='Men')
ax.bar(labels,women_means,width,yerr=women_std,bottom=men_means,label='Women')```

《6》 Set the title and label

```ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')```

《7》 Legend and display

```ax.legend()
plt.show()```

4. All codes are as follows

```import matplotlib.pyplot as plt
# Drawing a bar chart
labels = ['G1', 'G2', 'G3', 'G4', 'G5']
men_means = [20, 35, 30, 35, 27]
women_means = [25, 32, 34, 20, 25]
men_std = [2, 3, 4, 1, 2]
women_std = [3, 5, 2, 3, 3]
width = 0.35 # The width of the bar chart
fig,ax = plt.subplots()
ax.bar(labels, men_means,width,yerr=men_std,label='Men')
ax.bar(labels,women_means,width,yerr=women_std,bottom=men_means,label='Women')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.legend()
plt.show()```

5. Effect display 