下面是一些使用MATLAB进行数据可视化的常见案例,包括简单的2D图、3D图、以及带有注释和格式化的图表。每个案例都会提供详细的MATLAB代码。
1. 简单的2D图
案例:绘制正弦和余弦函数
matlab 复制代码%
生成数据
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
%
创建图形
figure;
plot(x, y1, '-r', 'LineWidth', 1.5); %
绘制正弦函数,红色实线
hold on;
plot(x, y2, '--b', 'LineWidth', 1.5); %
绘制余弦函数,蓝色虚线
hold off;
%
添加标题和标签
title('Sine and Cosine Functions');
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)');
%
设置网格
grid on;
2. 带有注释的2D图
案例:绘制抛物线并添加注释
matlab 复制代码%
生成数据
x = linspace(-10, 10, 100);
y = x.^2;
%
创建图形
figure;
plot(x, y, '-g', 'LineWidth', 2); %
绘制抛物线,绿色实线
%
添加标题和标签
title('Parabola');
xlabel('x');
ylabel('y');
legend('y = x^2');
%
添加注释
text(0, 0, 'Vertex (0, 0)', 'HorizontalAlignment', 'right');
%
设置网格
grid on;
3. 3D图
案例:绘制三维曲面图
matlab 复制代码%
生成数据
[X, Y] = meshgrid(-3:0.1:3, -3:0.1:3);
Z = peaks(X, Y);
%
创建图形
figure;
surf(X, Y, Z);
%
添加标题和标签
title('3D Surface Plot');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
%
设置视角
view(45, 30);
%
添加颜色条
colorbar;
4. 多子图
案例:绘制子图
matlab 复制代码%
生成数据
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
%
创建图形
figure;
%
子图
1
subplot(3, 1, 1);
plot(x, y1, '-r', 'LineWidth', 1.5);
title('Sine Function');
xlabel('x');
ylabel('sin(x)');
grid on;
%
子图
2
subplot(3, 1, 2);
plot(x, y2, '-b', 'LineWidth', 1.5);
title('Cosine Function');
xlabel('x');
ylabel('cos(x)');
grid on;
%
子图
3
subplot(3, 1, 3);
plot(x, y3, '-g', 'LineWidth', 1.5);
title('Tangent Function');
xlabel('x');
ylabel('tan(x)');
grid on;
5. 极坐标图
案例:绘制极坐标图
matlab 复制代码%
生成数据
theta = linspace(0, 2*pi, 100);
r = abs(sin(2*theta) .* cos(2*theta));
%
创建图形
figure;
polarplot(theta, r, '-m', 'LineWidth', 1.5);
%
添加标题
title('Polar Plot of r = |sin(2\theta) cos(2\theta)|');
6. 柱状图
案例:绘制柱状图
matlab 复制代码%
生成数据
categories = {'A', 'B', 'C', 'D'};
values = [5, 3, 9, 7];
%
创建图形
figure;
bar(values, 'FaceColor', [0.2, 0.6, 0.8]);
%
添加标题和标签
title('Bar Chart Example');
xlabel('Category');
ylabel('Value');
set(gca, 'XTickLabel', categories);
%
设置网格
grid on;
7. 散点图
案例:绘制散点图
matlab 复制代码%
生成数据
x = randn(100, 1);
y = randn(100, 1);
sizes = 100 * rand(100, 1); %
点的大小
colors = rand(100, 1); %
点的颜色
%
创建图形
figure;
scatter(x, y, sizes, colors, 'filled');
%
添加标题和标签
title('Scatter Plot Example');
xlabel('X-axis');
ylabel('Y-axis');
%
添加颜色条
colorbar;
README.md
markdown 复制代码# MATLAB Plotting Examples
This repository contains examples of using MATLAB for data visualization, including 2D plots, 3D plots, and specialized plots.
## Requirements
- MATLAB
## Usage
1. Clone the repository or download the files.
2. Open MATLAB.
3. Navigate to the directory containing the examples.
4. Run the desired script, e.g., `simple_2d_plot.m`.
## Examples
### Simple 2D Plot
```matlab
% simple_2d_plot.m
% Generates a 2D plot of sine and cosine functions.
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
figure;
plot(x, y1, '-r', 'LineWidth', 1.5);
hold on;
plot(x, y2, '--b', 'LineWidth', 1.5);
hold off;
title('Sine and Cosine Functions');
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)');
grid on;
Annotated 2D Plot
matlab 复制代码% annotated_2d_plot.m
% Generates a 2D plot of a parabola with annotations.
x = linspace(-10, 10, 100);
y = x.^2;
figure;
plot(x, y, '-g', 'LineWidth', 2);
title('Parabola');
xlabel('x');
ylabel('y');
legend('y = x^2');
text(0, 0, 'Vertex (0, 0)', 'HorizontalAlignment', 'right');
grid on;
3D Surface Plot
matlab 复制代码% 3d_surface_plot.m
% Generates a 3D surface plot.
[X, Y] = meshgrid(-3:0.1:3, -3:0.1:3);
Z = peaks(X, Y);
figure;
surf(X, Y, Z);
title('3D Surface Plot');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
view(45, 30);
colorbar;
Subplots
matlab 复制代码% subplots_example.m
% Generates multiple subplots in one figure.
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = tan(x);
figure;
subplot(3, 1, 1);
plot(x, y1, '-r', 'LineWidth', 1.5);
title('Sine Function');
xlabel('x');
ylabel('sin(x)');
grid on;
subplot(3, 1, 2);
plot(x, y2, '-b', 'LineWidth', 1.5);
title('Cosine Function');
xlabel('x');
ylabel('cos(x)');
grid on;
subplot(3, 1, 3);
plot(x, y3, '-g', 'LineWidth', 1.5);
title('Tangent Function');
xlabel('x');
ylabel('tan(x)');
grid on;
Polar Plot
matlab 复制代码% polar_plot.m
% Generates a polar plot.
theta = linspace(0, 2*pi, 100);
r = abs(sin(2*theta) .* cos(2*theta));
figure;
polarplot(theta, r, '-m', 'LineWidth', 1.5);
title('Polar Plot of r = |sin(2\theta) cos(2\theta)|');
Bar Chart
matlab 复制代码% bar_chart_example.m
% Generates a bar chart.
categories = {'A', 'B', 'C', 'D'};
values = [5, 3, 9, 7];
figure;
bar(values, 'FaceColor', [0.2, 0.6, 0.8]);
title('Bar Chart Example');
xlabel('Category');
ylabel('Value');
set(gca, 'XTickLabel', categories);
grid on;
Scatter Plot
matlab 复制代码% scatter_plot_example.m
% Generates a scatter plot.
x = randn(100, 1);
y = randn(100, 1);
sizes = 100 * rand(100, 1);
colors = rand(100, 1);
figure;
scatter(x, y, sizes, colors, 'filled');
title('Scatter Plot Example');
xlabel('X-axis');
ylabel('Y-axis');
colorbar;
mathematica 复制代码以上是几个
MATLAB
画图的案例及其相关代码。通过这些案例,你可以学会如何使用
MATLAB
进行各种数据可视化,包