👀
Crash Visualization
  • Welcome
  • Preface
    • Who the book is written for
    • How the book is organized
  • 1. Introduction of Data Visualization
    • 1.1 What is data visualization?
    • 1.2 Why does visualization matter?
  • 2. Tricks in Visualization
    • 2.1 Choose Appropriate Chart
    • 2.2 Features of Charts
      • 2.2.1 Table
      • 2.2.2 Column Chart
      • 2.2.3 Line Chart
      • 2.2.4 Pie Chart
      • 2.2.5 Scatter Chart
      • 2.2.6 Map Chart
    • 2.3 Misused Graph
    • 2.4 Tips in Visualization
  • 3. Matplotlib
    • 3.1 Basic Concepts
    • 3.2 Line Chart
    • 3.3 Area Chart
    • 3.4 Column Chart
    • 3.5 Histogram Chart
    • 3.6 Scatter Chart
    • 3.7 Lollipop Chart
    • 3.8 Pie Chart
    • 3.9 Venn Chart
    • 3.10 Waffle Chart
    • 3.11 Animation
  • 4. Seaborn
    • 4.1 Trends
    • 4.2 Ranking
      • 4.2.1 Barplot
      • 4.2.2 Boxplot
    • 4.3 Composition
      • 4.3.1 Stacked Chart
    • 4.4 Correlation
      • 4.4.1 Scatter Plot
      • 4.4.2 Linear Relationship
      • 4.4.3 Heatmap
      • 4.4.4 Pairplot
    • 4.5 Distribution
      • 4.5.1 Boxplot
      • 4.5.2 Violin plot
      • 4.5.3 Histogram plot
      • 4.5.4 Density plot
      • 4.5.5 Joint plot
  • 5. Bokeh
    • 5.1 Basic Plotting
    • 5.2 Data Sources
    • 5.3 Annotations
    • 5.4 Categorical Data
    • 5.5 Presentation and Layouts
    • 5.6 Linking and Interactions
    • 5.7 Network Graph
    • 5.8 Widgets
  • 6. Plotly
    • 6.1 Fundamental Concepts
      • 6.1.1 Plotly Express
      • 6.1.2 Plotly Graph Objects
    • 6.2 Advanced Charts
      • 6.2.1 Advanced Scatter Chart
      • 6.2.2 Advanced Bar Chart
      • 6.2.3 Advanced Pie Chart
      • 6.2.4 Advanced Heatmap
      • 6.2.5 Sankey Chart
      • 6.2.6 Tables
    • 6.3 Statistical Charts
      • 6.3.1 Common Statistical Charts
      • 6.3.2 Dendrograms
      • 6.3.3 Radar Chart
      • 6.3.4 Polar Chart
      • 6.3.5 Streamline Chart
    • 6.4 Financial Charts
      • 6.4.1 Funnel Chart
      • 6.4.2 Candlestick Chart
      • 6.4.3 Waterfall Chart
  • Support
    • Donation
Powered by GitBook
On this page
  • 1. Column Chart
  • 2. Horizontal Column Chart
  • 3. Stacked Column Chart
  • 4. Grouped Column Chart

Was this helpful?

  1. 3. Matplotlib

3.4 Column Chart

Previous3.3 Area ChartNext3.5 Histogram Chart

Last updated 4 years ago

Was this helpful?

1. Column Chart

What to learn:

  • Set figure size

  • Plot with numerical variables and categorical variables

  • Change column color

  • Create a horizontal bar chart

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()

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

3. Stacked Column Chart

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

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()

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.

Column Chart
Horizontal Column Chart
Stacked Column Chart
Grouped Column Chart