Introduction
Data visualization is an exploration tool for scientists, researchers, engineers, and analysts to represent data in a graphical or pictorial form. MATLAB, a programming language for data analysis and visualization, has its own visualization library called MATLAB Plot or simply MatPlot. MatPlot is a powerful and flexible library with numerous data visualization techniques for generating informative and clear visualizations. In this article, we will explore various data visualization techniques using MatPlot.
Line Plot
The line plot is a fundamental visualization technique that displays the value of a dependent variable on the y-axis against independent variable on the x-axis. The simplest way to create a line plot in MatPlot is by using the “plot” function. Let’s consider an example of plotting the graph of sine and cosine functions.
x = linspace(0, 10, 100);
y_sin = sin(x);
y_cos = cos(x);
plot(x, y_sin, '-r', x, y_cos, '-g');
xlabel('x');
ylabel('y');
title('Sine and Cosine Functions');
legend('Sine', 'Cosine');
The above code generates a line plot of sine and cosine functions and labels the x-axis as “x” and the y-axis as “y”. We also set the title of the plot as “Sine and Cosine Functions” and include a legend to differentiate between the two lines.
Scatter Plot
A scatter plot is a data visualization technique that shows the relationship between two continuous variables. In a scatter plot, the x-axis is for the independent variable, and the y-axis is for the dependent variable. Scatter plots are useful for identifying patterns or relationships between variables, such as correlation or clustering. In MatPlot, we can use the “scatter” function to generate a scatter plot. Let’s generate a scatter plot of random data.
x = randn(100,1);
y = randn(100,1);
scatter(x, y);
xlabel('x');
ylabel('y');
title('Random Data Scatter Plot');
The above code generates a scatter plot of randomly generated data and labels the x-axis as “x” and the y-axis as “y”. We also set the title of the plot as “Random Data Scatter Plot”.
Bar Graph
A bar graph is a data visualization technique that represents data in a categorical form. A bar graph has bars of equal width and whose height indicates the value of a dependent variable. In MatPlot, we can use the “bar” function to generate a bar graph. Let’s generate a bar graph of the top 10 oil producers.
x = [737, 511, 309, 259, 224, 163, 138, 128, 118, 112];
countries = ['United States', 'Saudi Arabia', 'Russia', 'Canada', 'China', 'Iraq', 'Iran', ...
'United Arab Emirates', 'Brazil', 'Kuwait'];
bar(x);
set(gca, 'XTick', 1:10, 'XTickLabel', countries, 'YTick', 0:200:800);
xlabel('Countries');
ylabel('Thousand Barrels per Day');
title('Top 10 Oil Producers');
The above code generates a bar graph of the top 10 oil producers and labels the x-axis as “Countries” and the y-axis as “Thousand Barrels per Day”. We also set the title of the plot as “Top 10 Oil Producers” and a customized set of tick marks on both the x and y-axes.
Contour Plot
A contour plot is a data visualization technique that displays three-dimensional (3D) data in a two-dimensional (2D) form. A contour plot represents the values of a dependent variable as contour lines of equal value in a 2D plane. Contour plots are useful for visualizing dense or unevenly spaced data. In MatPlot, we can use the “contour” function to generate a contour plot. Let’s generate a contour plot of a 3D Gaussian function.
[X Y] = meshgrid(-3:.25:3);
Z = peaks(X, Y);
contour(X, Y, Z);
xlabel('x');
ylabel('y');
title('Contour Plot of 3D Gaussian Function');
The above code generates a contour plot of a 3D Gaussian function and labels the x-axis as “x” and the y-axis as “y”. We also set the title of the plot as “Contour Plot of 3D Gaussian Function”.
Heat Map
A heat map is a data visualization technique that displays data using colors to represent values. Heat maps are useful for visualizing large datasets and identifying patterns or trends in data. In MatPlot, we can use the “heatmap” function to generate a heat map. Let’s generate a heat map of the correlation matrix of the iris dataset.
load('iris.mat');
C = corr(iris(:,1:4), 'Type', 'Spearman');
figure;
heatmap(C, 'Colormap', hot);
xlabel('Features');
ylabel('Features');
title('Heat Map of Correlation Matrix');
The above code generates a heat map of the correlation matrix of the iris dataset using the “heatmap” function. We set the colormap of the heat map to “hot” and label the x and y-axes with “Features”. We also set the title of the plot as “Heat Map of Correlation Matrix”.
Conclusion
MatPlot is a powerful and flexible data visualization library that provides numerous techniques for generating informative and clear visualizations. In this article, we explored various visualization techniques such as line plots, scatter plots, bar graphs, contour plots, and heat maps. These techniques provide useful visualizations to explore datasets and identify patterns or relationships between variables.