How to insert dash-line in a matlab figure?

10 views (last 30 days)
How can i add such horizontal dash-line in a matlab figure?

Answers (2)

Ayush
Ayush on 3 Jun 2024
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 on 3 Jun 2024
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.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!