Bar Graph in MATLAB

MATLAB Tutorial

📊 Bar Graph in MATLAB

A bar graph in MATLAB is used to compare values across categories using rectangular bars.

It’s widely used in statistics, business analysis, engineering results, and reports.
MATLAB is developed by MathWorks.


🔹 What Is a Bar Graph?

  • Displays discrete data

  • Height (or length) of each bar represents a value

  • Created using the bar() function


1️⃣ Basic Bar Graph

🔸 Syntax

bar(y)

🔸 Example


Output:
A simple bar chart with 4 bars.


2️⃣ Bar Graph with X and Y Values



3️⃣ Bar Graph with Category Labels


📌 gca → get current axes.


4️⃣ Grouped Bar Graph

Used to compare multiple datasets side by side.



5️⃣ Stacked Bar Graph

Shows cumulative contribution of each group.



6️⃣ Horizontal Bar Graph



7️⃣ Bar Width Control



⚠️ Important Notes

  • bar() → vertical bars

  • barh() → horizontal bars

  • Grouped bars → matrix input

  • Use labels & title for clarity


🎯 Interview Questions: Bar Graph in MATLAB

🔹 Q1. Which function is used to draw a bar graph in MATLAB?

Answer: bar()


🔹 Q2. How do you create a horizontal bar graph?

Answer: Using barh().


🔹 Q3. How do you create grouped bar graphs?

Answer: By passing a matrix to bar().


🔹 Q4. What does a stacked bar graph show?

Answer: The cumulative contribution of each dataset.


🔹 Q5. How do you label the axes in a bar graph?

Answer: Using xlabel() and ylabel().


🔹 Q6. How do you add a legend to a bar graph?

Answer: Using legend().


Summary

  • Bar graphs compare discrete values

  • Created using bar() or barh()

  • Supports grouped & stacked formats

  • Essential for categorical data visualization

You may also like...