Scatter plots and lines

4 ビュー (過去 30 日間)
Harvey
Harvey 2025 年 4 月 24 日
コメント済み: Sam Chak 2025 年 4 月 25 日
I am wanting to add in a line like the graph below, but cannot seem to get the black line to stop going upwards. The gradient of the line is 4, please see my code and help!
clearvars;
close all;
clc;
% Extracted data
L = [0.2, 0.21, 0.22, 0.23, 0.24, 0.25]; % Length in µm
G = [0.002073593, 0.002034555, 0.00198774, 0.00194322, 0.00191650, 0.00188695]; % G in µm⁻¹
V = [33.4790, 35.0453, 35.5131, 35.7370, 36.2533, 36.3155]; % V in µm/s
% Plotting
figure;
hold on;
% Plot G vs V as a regular line plot
plot(V, G, 'o', 'LineWidth', 2, 'Color', 'b', 'MarkerFaceColor', 'b', 'DisplayName', 'Data');
% Add a black line with gradient = 4 starting from the origin
m = 4; % Gradient
V_line = linspace(0, max(V)+1, 100); % Start from 0
G_line = m * V_line;
plot(V_line, G_line, 'k-', 'LineWidth', 2, 'DisplayName', 'Gradient = 4 (from origin)');
% Formatting the plot
xlabel('Velocity V (µm/s)', 'FontSize', 30);
ylabel('Thermal Gradient G (µm⁻¹)', 'FontSize', 30);
title('G vs V for Varying Separation Distances', 'FontSize', 35);
legend('Location', 'northeast', 'FontSize', 20);
grid on;
xlim([min(0, min(V)-0.5), max(V)+0.5]);
ylim([min(0, min(G)-0.00001), max(G)+0.00001]);
  3 件のコメント
Walter Roberson
Walter Roberson 2025 年 4 月 24 日
Your question does not indicate where you want the black line to stop ?
Cris LaPierre
Cris LaPierre 2025 年 4 月 24 日
Your code does not recreate the image shown.
Note that you have defined the equation of the line to go from 0 to 37.3155 along X and 0 to 4*(36.3155 + 1), or 149.2620, along Y:
% Add a black line with gradient = 4 starting from the origin
m = 4; % Gradient
V_line = linspace(0, max(V)+1, 100); % Start from 0
G_line = m * V_line;
plot(V_line, G_line, 'k-', 'LineWidth', 2, 'DisplayName', 'Gradient = 4 (from origin)');

サインインしてコメントする。

回答 (1 件)

Sam Chak
Sam Chak 2025 年 4 月 24 日
Could you add a thick black line as indicated by the dotted lines? The dotted lines serve as a tracing task, helping you become familiar with forming lines, which is a crucial step in learning to plot in MATLAB.
clearvars;
close all;
clc;
% Extracted data
L = [0.2, 0.21, 0.22, 0.23, 0.24, 0.25]; % Length in µm
G = [0.002073593, 0.002034555, 0.00198774, 0.00194322, 0.00191650, 0.00188695]; % G in µm⁻¹
V = [33.4790, 35.0453, 35.5131, 35.7370, 36.2533, 36.3155]; % V in µm/s
% Plotting
figure;
hold on;
%% ------- Added or modified :: Start -------
lgd = legend('');
Warning: Ignoring extra legend entries.
title(lgd, 'L (cm)')
% Plot G vs V as a regular line plot
plot(V(1), G(1), 'o', 'LineWidth', 2, 'Color', 'r', 'MarkerFaceColor', 'r', 'DisplayName', '20');
plot(V(2), G(2), 'o', 'LineWidth', 2, 'Color', 'y', 'MarkerFaceColor', 'y', 'DisplayName', '21');
plot(V(3), G(3), 'o', 'LineWidth', 2, 'Color', 'g', 'MarkerFaceColor', 'g', 'DisplayName', '22');
plot(V(4), G(4), 'o', 'LineWidth', 2, 'Color', 'c', 'MarkerFaceColor', 'c', 'DisplayName', '23');
plot(V(5), G(5), 'o', 'LineWidth', 2, 'Color', 'b', 'MarkerFaceColor', 'b', 'DisplayName', '24');
plot(V(6), G(6), 'o', 'LineWidth', 2, 'Color', 'm', 'MarkerFaceColor', 'm', 'DisplayName', '25');
xLine = [25 45.8];
yLine = [1.4e-3 2.5e-3];
line(xLine, yLine, 'Color', 'k', 'LineStyle', '--')
lgd.String(7) = '';
lgd.Location = 'northeast';
text(26.3, 2.3e-3, 'Stable', 'FontSize', 12)
text(45.3, 1.3e-3, 'Unstable', 'FontSize', 12)
xlim([25 50])
ylim([1e-3 2.5e-3])
%% ------- Added or modified :: End -------
% Add a black line with gradient = 4 starting from the origin
m = 4; % Gradient
V_line = linspace(0, max(V)+1, 100); % Start from 0
G_line = m * V_line;
% plot(V_line, G_line, 'k-', 'LineWidth', 2, 'DisplayName', 'Gradient = 4 (from origin)');
% Formatting the plot
xlabel('Velocity V (µm/s)');
ylabel('Thermal Gradient G (µm⁻¹)');
title('G vs V for Varying Separation Distances');
% legend('Location', 'northeast', 'FontSize', 20);
grid on;
% xlim([min(0, min(V)-0.5), max(V)+0.5]);
% ylim([min(0, min(G)-0.00001), max(G)+0.00001]);
  2 件のコメント
Cris LaPierre
Cris LaPierre 2025 年 4 月 25 日
Same code, just leveraging some of the capabilities of MATLAB to consolidate.
% Extracted data
L = [0.2, 0.21, 0.22, 0.23, 0.24, 0.25]; % Length in µm
G = [0.002073593, 0.002034555, 0.00198774, 0.00194322, 0.00191650, 0.00188695]; % G in µm⁻¹
V = [33.4790, 35.0453, 35.5131, 35.7370, 36.2533, 36.3155]; % V in µm/s
% Plotting
% Plot G vs V as a regular line plot
p=gscatter(V, G, L*100,'rygcbm');
hold on
plot([25 45.8], [1.4e-3 2.5e-3], 'k--')
hold off
% Formatting the plot
xlabel('Velocity V (µm/s)');
ylabel('Thermal Gradient G (µm⁻¹)');
title('G vs V for Varying Separation Distances');
lgd = legend(p,'Location', 'northeast');
title(lgd,"L (cm)")
grid on;
text(26.3, 2.3e-3, 'Stable', 'FontSize', 12)
text(45.3, 1.3e-3, 'Unstable', 'FontSize', 12)
xlim([25 50]);
ylim([1 2.5]*1e-3);
Sam Chak
Sam Chak 2025 年 4 月 25 日
Thank you, @Cris LaPierre, I learned new stuff today.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScatter Plots についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by