📜  MATLAB 2D Bar()(1)

📅  最后修改于: 2023-12-03 15:17:33.594000             🧑  作者: Mango

MATLAB 2D Bar()

The bar() function in MATLAB is used to create bar graphs for 2D data representation. It is widely used for visualizing data in various fields like economics, finance, social sciences, and many more. The bar graph is a type of chart that represents data with rectangular bars of lengths proportional to the values they represent.

Syntax

bar(y) creates a bar graph of the vector y. The x-axis values are automatically generated, and the width of each bar is set to be 0.8.

bar(x,y) creates a bar graph of the vector y versus x. The length of the vectors x and y must be the same.

bar(___,width) sets the width of the bars to the specified value.

bar(___,Name,Value) sets additional properties of the bars using one or more Name-Value pair arguments.

Example
x = [1 2 3 4 5];
y = [10 20 30 40 50];
figure;
bar(x, y, 'r', 'LineWidth', 2, 'EdgeColor', 'b');
xlabel('X-Axis');
ylabel('Y-Axis');
title('Bar Graph Demo');

In the above example, we have created a bar graph of y versus x. The width of each bar was set to 0.8 by default, and we have changed it to 1. The color of the bars was set to red ('r'), and their edges were set to blue ('b'). We have also added labels for the x-axis and y-axis and a title to the graph.

Conclusion

The bar() function in MATLAB is a useful tool for creating bar graphs for 2D data visualization. By setting various properties of the bars, we can customize the graph as per our requirements.