Basic Plotting Functions
2D Plotting
Comparison of Basic Plotting Functions:
% 1. plot function (line plot)
x = linspace(0, 2*pi, 100);
y = sin(x);
figure;
plot(x, y, 'b-', 'LineWidth', 2); % Blue solid line
title('Sine Curve');
% 2. scatter function (scatter plot)
x = randn(100,1);
y = randn(100,1);
figure;
scatter(x, y, 50, y, 'filled'); % Point size 50, color mapped to y values
colormap jet;
colorbar;
% 3. bar function (bar chart)
categories = {'A','B','C','D'};
values = [10 20 15 25];
figure;
bar(categories, values);
xtickangle(45); % Rotate x-axis labels
% 4. histogram function (histogram)
data = randn(1000,1);
figure;
histogram(data, 20); % 20 bins
xlabel('Value');
ylabel('Frequency');
Graphics Property Settings
Comprehensive Graphics Property Control:
% Create basic plot
x = 0:0.1:2*pi;
y = sin(x);
figure;
h = plot(x, y);
% 1. Color and line style settings
h.Color = [0.2 0.4 0.8]; % RGB color
h.LineStyle = '--'; % Dashed line
h.LineWidth = 2; % Line width
% 2. Marker settings
h.Marker = 'o'; % Circle marker
h.MarkerSize = 8; % Marker size
h.MarkerFaceColor = 'r'; % Filled red
h.MarkerEdgeColor = 'k'; % Black edge
% 3. Text annotations
title('Sine Function Curve', 'FontSize', 14, 'FontWeight', 'bold');
xlabel('X-axis (radians)', 'FontSize', 12);
ylabel('Y-axis (sin(x))', 'FontSize', 12);
legend('sin(x)', 'Location', 'northwest');
% 4. Axis control
xlim([0 2*pi]); % X-axis range
ylim([-1.2 1.2]); % Y-axis range
xticks(0:pi/2:2*pi); % X-axis ticks
xticklabels({'0','π/2','π','3π/2','2π'}); % Custom tick labels
grid on; % Display grid
Interactive Graphics
Implementation of Data Interaction Features:
% 1. ginput function (manual point selection)
figure;
plot(peaks);
title('Click 5 points');
points = ginput(5); % Get coordinates of 5 points
disp('Selected point coordinates:');
disp(points);
% Mark selected points on the plot
hold on;
plot(points(:,1), points(:,2), 'ro', 'MarkerSize', 10);
% 2. datacursormode (data cursor)
figure;
plot(x, y);
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @customDataTip); % Custom tooltip content
% Custom data tip function
function txt = customDataTip(~, event_obj)
pos = event_obj.Position;
txt = {['X: ', num2str(pos(1))], ['Y: ', num2str(pos(2))]};
end
Multiple Subplots
Complex Subplot Layout Example:
% Create 2x2 subplot layout
figure;
t = 0:0.01:2*pi;
% Subplot 1: Sine curve
subplot(2,2,1);
plot(t, sin(t));
title('Sine Function');
% Subplot 2: Cosine curve
subplot(2,2,2);
plot(t, cos(t));
title('Cosine Function');
% Subplot 3: Dual Y-axis plot
subplot(2,2,3);
yyaxis left;
plot(t, sin(t));
ylabel('sin(t)');
yyaxis right;
plot(t, cos(t), 'r');
ylabel('cos(t)');
title('Dual Y-Axis Plot');
% Subplot 4: Polar plot
subplot(2,2,4, 'polarplot');
polarplot(t, sin(2*t));
title('Polar Plot');
% Adjust subplot spacing
sgtitle('Multiple Subplots Example'); % Add overall title
3D Plotting
Basic 3D Graphics Plotting:
% 1. plot3 function (3D curve)
t = 0:pi/50:10*pi;
figure;
plot3(sin(t), cos(t), t, 'LineWidth', 2);
xlabel('X'); ylabel('Y'); zlabel('Z');
title('3D Helix');
% 2. surf function (surface plot)
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-X.^2-Y.^2);
figure;
surf(X,Y,Z);
shading interp; % Interpolated shading
colormap jet;
colorbar;
title('Gaussian Surface');
% 3. mesh function (mesh plot)
figure;
mesh(X,Y,Z);
xlabel('X'); ylabel('Y'); zlabel('Z');
title('Gaussian Mesh');
% 4. contour function (contour plot)
figure;
contour(X,Y,Z,20);
xlabel('X'); ylabel('Y');
title('Gaussian Contours');
Figure Window Management
Figure Window Control Techniques:
% 1. Create new figure window
figure('Name', 'Custom Window', 'NumberTitle', 'off', 'Position', [100 100 800 600]);
% 2. Get current figure window
gcf_handle = gcf; % Get current figure handle
set(gcf_handle, 'Color', 'w'); % Set background color to white
% 3. Get current axes
ax = gca; % Get current axes handle
ax.FontSize = 12; % Set axes font size
% 4. Multi-window management
figure(1); plot(x, y); % Activate window 1
figure(2); plot(x, z); % Activate window 2
close(1); % Close window 1
Advanced Graphics and Visualization
Special Graphics Plotting
Professional Field Graphics Examples:
% 1. pie function (pie chart)
data = [30 25 20 25];
labels = {'A','B','C','D'};
figure;
pie(data, [0 0 1 0]); % Explode the 3rd section
colormap summer;
title('Category Proportions');
% 2. area function (area plot)
x = 1:10;
y = rand(1,10);
figure;
area(x, y);
xlabel('Time'); ylabel('Value');
title('Cumulative Area Plot');
% 3. quiver function (quiver plot)
[x,y] = meshgrid(-2:0.2:2);
u = -y;
v = x;
figure;
quiver(x,y,u,v,2); % Scale factor 2
xlabel('X'); ylabel('Y');
title('Vector Field');
% 4. streamline function (streamline plot)
figure;
startx = -3:0.2:3;
starty = zeros(size(startx));
streamline(x,y,u,v,startx,starty);
title('Streamlines');
Graphics Object Manipulation
Low-Level Graphics Object Control:
% 1. Create line object
figure;
h_line = line([0 1 2], [0 1 0], 'Color', 'r', 'LineWidth', 2);
h_line.Marker = 'o'; % Add markers
% 2. Create text object
h_text = text(1, 0.5, 'Key Point', ...
'FontSize', 12, 'Color', 'b', 'Rotation', 45);
% 3. Create axes object
h_axes = axes('Position', [0.1 0.1 0.8 0.8], 'XLim', [0 2], 'YLim', [0 1]);
plot(h_axes, [0 1 2], [0 1 0]);
% 4. Batch modify object properties
all_lines = findobj(gcf, 'Type', 'line');
set(all_lines, 'LineWidth', 1.5);
% 5. Object hierarchy operations
h_parent = uipanel('Title', 'Control Panel', 'Position', [0.1 0.1 0.8 0.3]);
h_child = uicontrol(h_parent, 'Style', 'slider', 'Position', [10 10 200 20]);
Dynamic Graphics and Animation
Real-Time Data Visualization Techniques:
% 1. animatedline dynamic curve
figure;
h = animatedline('Color', 'b', 'LineWidth', 2);
axis([0 10 -1 1]);
% Simulate real-time data
for t = 0:0.1:10
y = sin(t);
addpoints(h, t, y);
drawnow limitrate; % Control refresh rate
end
% 2. Colormap animation
figure;
[X,Y] = meshgrid(-2:0.1:2);
Z = X.*exp(-X.^2-Y.^2);
h_surf = surf(X,Y,Z);
shading interp;
colormap(jet);
colorbar;
% Dynamically change viewpoint
for az = 0:5:360
view(az, 30);
drawnow;
end
% 3. Data update animation
figure;
h_bar = bar(rand(1,5));
for k = 1:20
new_data = rand(1,5);
set(h_bar, 'YData', new_data);
drawnow;
pause(0.1);
end
Graphics Export and Saving
High-Quality Graphics Output:
% 1. Basic saving method
figure;
plot(x, y);
saveas(gcf, 'basic_plot.png'); % Save as PNG
% 2. High-quality export
exportgraphics(gcf, 'high_quality.pdf', 'Resolution', 300); % PDF vector graphics
% 3. Export to different formats
formats = {'png', 'jpg', 'pdf', 'eps', 'svg'};
for i = 1:length(formats)
exportgraphics(gcf, sprintf('plot.%s', formats{i}), ...
'ContentType', 'vector'); % Vector format
end
% 4. Batch export subplots
figure;
for k = 1:4
subplot(2,2,k);
plot(rand(10,1));
end
exportgraphics(gcf, 'subplots.pdf', 'Resolution', 300);
Graphical User Interface Basics
Simple GUI Development Example:
% 1. Create basic GUI window
fig = uifigure('Name', 'Data Plotting Tool', 'Position', [100 100 500 400]);
% 2. Add controls
ax = uiaxes(fig, 'Position', [50 100 400 250]);
btn_plot = uibutton(fig, 'Text', 'Plot Curve', ...
'Position', [50 50 100 30], 'ButtonPushedFcn', @plotData);
btn_clear = uibutton(fig, 'Text', 'Clear Plot', ...
'Position', [200 50 100 30], 'ButtonPushedFcn', @clearPlot);
% 3. Callback function definitions
function plotData(src, event)
x = linspace(0, 2*pi, 100);
y = sin(x);
plot(ax, x, y);
title(ax, 'Sine Curve');
end
function clearPlot(src, event)
cla(ax);
title(ax, '');
end
% 4. Add data input controls
edit_points = uieditfield(fig, 'numeric', ...
'Position', [50 150 100 22], 'Value', 100);
lbl_points = uilabel(fig, 'Text', 'Number of Points:', ...
'Position', [50 180 100 22]);
Custom Color Schemes
Professional Color Scheme Implementation Methods:
% 1. Use predefined colormap
figure;
surf(peaks);
colormap(jet); % Classic rainbow colors
colorbar;
% 2. Create custom colormap
myColors = [linspace(0,1,256)', linspace(0,0,256)', linspace(1,0,256)']; % Red to blue
colormap(myColors);
% 3. Discrete color scheme
discreteColors = [
0.85 0.33 0.10; % Orange
0.49 0.49 0.49; % Gray
0.47 0.67 0.19; % Green
0.30 0.75 0.93 % Cyan
];
colormap(discreteColors);
% 4. Use ColorBrewer colors
brewerColors = cbrewer('qual', 'Set2', 4); % 4 qualitative colors
colormap(brewerColors);
% 5. Apply colorbar
colorbar('Ticks', [0 0.33 0.67 1], ...
'TickLabels', {'Low', 'Medium-Low', 'Medium-High', 'High'});
% 6. Gradient background
figure;
ax = axes('Position', [0 0 1 1], 'Visible', 'off');
gradient = linspace(0,1,256)';
image(repmat(gradient, 1, 1), 'CDataMapping', 'scaled');
colormap(parula);
By mastering these graphics visualization techniques, you will be able to:
- Create professional-level scientific charts
- Develop interactive data exploration tools
- Build complex visualization applications
- Implement dynamic data monitoring systems
- Design publication-quality academic graphics
- Produce engaging presentation materials
- Develop custom GUI data analysis tools



