> For the complete documentation index, see [llms.txt](https://vecco-insight.gitbook.io/crash-visualization/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vecco-insight.gitbook.io/crash-visualization/matplotlib/1.3-column-chart-plot.md).

# 3.4 Column Chart

### 1. Column Chart

{% hint style="info" %}
**What to learn:**

* Set figure size&#x20;
* Plot with numerical  variables and categorical variables
* Change column color
* Create a horizontal bar chart
  {% endhint %}

```
plt.figure(figsize = (8,6))                     # set the figure size

performance = [70,95, 61, 88,82]                # y_axis values
student = ['Mavin','Lily','Tim','Ben','Julian'] # x_axis values
y_pos = np.arange(len(student))                 # calculate number of "student"

plt.bar(y_pos,performance,color = 'orange')  # create bars, set the orange color 
plt.xticks(y_pos,student)       #Create names on the x-axis
plt.show()
```

![Column Chart](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBLKZvCyEVHHjYNBlm4%2F-MBLXrNY62L9heIB1252%2Fdownload-2.png?alt=media\&token=811b2d39-5947-4125-a83e-7dfe0f930129)

### 2. Horizontal Column Chart

```
# Create horizontal bars
plt.barh(y_pos, height)     # just add  a 'h' (horizontal)
plt.yticks(y_pos,student)   # we need to set students'name on the y-axisStacked Column Chart
```

![Horizontal Column Chart](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBLKZvCyEVHHjYNBlm4%2F-MBLZ7Hr0PX-iJ_BIq_9%2Fdownload.png?alt=media\&token=7e235e4c-c96b-414f-b6bc-e5ca7d509ce9)

### 3. Stacked Column Chart

{% hint style="info" %}
**What to learn:**

* The difference of Stacked Column Chart and Grouped Column Chart
* Plot two groups of bars on one figure
* Choose  the proper chart&#x20;
  {% endhint %}

```
labels =['G1', 'G2', 'G3', 'G4']
male_avg = [1.80,1.75, 1.84,1.7]
female_avg =[1.75, 1.6, 1.70, 1.53]

width =0.4   # set the width of the bars

fig, ax = plt.subplots()
ax.bar(labels, male_avg, width,  label ='Male')
ax.bar(labels, female_avg, width,  bottom=  male_avg,
       label='Female')
       
ax.set_ylabel('Average Height')
ax.set_title('Height by group and gender')
ax.legend()

plt.show()
```

![Stacked Column Chart](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBLdArVr5Cg688V3Bz6%2F-MBLjUJld7-gcfh9xxJy%2Fdownload-2.png?alt=media\&token=d6628c2c-15e7-425a-99f9-3f2d1afbabf5)

**Obviously, this is a bad choice.** The stacked bar can't show the trends nor differences clearly. Meanwhile. the stack makes y\_axis value non-sense.  Nobody grows to 3.5 meters!

So we should alternate to the **grouped bar chart.**

### **4. Grouped Column Chart**

```
width =0.4
x = np.arange(len(labels))  # the label locations

fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, male_avg, width, label='Male')
rects2 = ax.bar(x + width/2, female_avg, width, label='Female')

ax.set_xticks(x)
ax.set_xticklabels(labels)  # set the categorical variable on x_axis
       
ax.set_ylabel('Average Height')
ax.set_title('Height by group and gender')
ax.legend()

plt.show()
```

Now, it looks much better and makes more sense.

![Grouped Column Chart](https://998709212-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB-ky7fVqjeXA6EcAbW%2F-MBLdArVr5Cg688V3Bz6%2F-MBLkakkVns_kFqhcFze%2Fdownload-3.png?alt=media\&token=b2510f49-a1bd-4387-95cf-10e7594ad177)
