How to insert dash-line in a matlab figure?

12 ビュー (過去 30 日間)
Kamruzzaman Bhuiyan
Kamruzzaman Bhuiyan 2021 年 11 月 29 日
回答済み: Steven Lord 2024 年 6 月 3 日
How can i add such horizontal dash-line in a matlab figure?

回答 (2 件)

Ayush
Ayush 2024 年 6 月 3 日
Hi,
To add a horizontal dash line in a figure, you can use the "yline" function. This function will help you in drawing horizontal lines at the desired y-values. Refer to an example below for a better understanding:
% Sample data for plotting
x = linspace(0, 2*pi, 100);
y = sin(x);
% Plot the data with a thicker line
plot(x, y, 'LineWidth', 1); % Increase the line width to make it bold
hold on; % Keep the plot active to add more elements
% Add horizontal dash-lines at specified y-values with increased thickness
yValues = -1:0.25:1; % Example y-values where you want dash-lines
for i = 1:length(yValues)
yline(yValues(i), '-.', 'Color', [0 0 0], 'LineWidth', 1.5); % Adds a dashed line at each yValue with increased thickness
end
hold off; % Release the plot
% Customize the plot
xlabel('X-axis');
ylabel('Y-axis');
title('Example Plot');
In the above code, I used a loop to iterate over predefined y-values, at which the horizontal lines should be drawn. You can make changes to these values based on your requirements. The "yline" draws a dashed line ('-.') at each y-value in the specified colour. You can customize the line style and colour as needed.
For more information on the "yline" function, refer to the below documentation:

Steven Lord
Steven Lord 2024 年 6 月 3 日
You could use the yline function or you could set the axes properties YGrid and GridLineStyle.
x = 1:10;
y = x.^2;
ax = axes;
plot(ax, x, y)
ax.YGrid = 'on';
ax.GridLineStyle = '-.';
There are many other axes properties you can set as well.

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by