is there a way to make the 0 x and y axis bold?
153 views (last 30 days)
Show older comments
% Given data points
x = [-0.1, 0, 0.2, 0.3];
y = [5.3, 2, 3.19, 1];
% Define the range of x values for the function plot
x1 = linspace(-0.1, 0.3, 100); % Adjust the range as needed
% Define the functions y1, y2, and y3
y3 = -556.6667 * x1.^3 + 185.5 * x1.^2 - 8.88333 * x1 + 2;
y1 = -33 * x1 + 2;
y2 = 129.83333 * x1.^2 - 20.016667 * x1 + 2;
% Fit a cubic polynomial to the data
coefficients = polyfit(x, y, 3); % 3 for cubic polynomial
% Generate points along the x-axis for plotting the polynomial
x_values = linspace(min(x), max(x), 100);
% Evaluate the cubic polynomial at these points
y_values = polyval(coefficients, x_values);
% Plot the data points, the cubic polynomial fit, and the functions
plot(x, y, 'o', 'MarkerFaceColor', 'b', 'MarkerSize', 8); % Data points
hold on;
plot(x_values, y_values, 'LineWidth', 2); % Cubic polynomial
plot(x1, y1, 'LineWidth', 2); % Function y1
plot(x1, y2, 'LineWidth', 2); % Function y2
plot(x1, y3, 'LineWidth', 2); % Function y3
hold off;
% Add labels and title
grid on;
xlabel('x-axis');
ylabel('y-axis');
% Add a legend
legend('Data Points Given', 'Cubic Polynomial Fit', 'Function P1(x)', 'Function P2(x)', 'Function P3(x)', 'Location', 'best');
% Make x and y axes bold
set(gca, 'LineWidth', 1.5);
set(gca, 'FontWeight', 'bold');
is there a way to make the 0 x and y axis bold?
2 Comments
Dyuman Joshi
on 6 Apr 2024
Edited: Dyuman Joshi
on 6 Apr 2024
"is there a way to make the 0 x and y axis bold?"
You have already done that - in the last line of your code.
Do you mean to bold just the axes and origin and not the x and y labels?
Answers (4)
Star Strider
on 6 Apr 2024
Ax = gca;
Ax.XAxis.FontWeight = 'bold';
Ax.YAxis.FontWeight = 'bold';
Try this —
% Given data points
x = [-0.1, 0, 0.2, 0.3];
y = [5.3, 2, 3.19, 1];
% Define the range of x values for the function plot
x1 = linspace(-0.1, 0.3, 100); % Adjust the range as needed
% Define the functions y1, y2, and y3
y3 = -556.6667 * x1.^3 + 185.5 * x1.^2 - 8.88333 * x1 + 2;
y1 = -33 * x1 + 2;
y2 = 129.83333 * x1.^2 - 20.016667 * x1 + 2;
% Fit a cubic polynomial to the data
coefficients = polyfit(x, y, 3); % 3 for cubic polynomial
% Generate points along the x-axis for plotting the polynomial
x_values = linspace(min(x), max(x), 100);
% Evaluate the cubic polynomial at these points
y_values = polyval(coefficients, x_values);
% Plot the data points, the cubic polynomial fit, and the functions
plot(x, y, 'o', 'MarkerFaceColor', 'b', 'MarkerSize', 8); % Data points
hold on;
plot(x_values, y_values, 'LineWidth', 2); % Cubic polynomial
plot(x1, y1, 'LineWidth', 2); % Function y1
plot(x1, y2, 'LineWidth', 2); % Function y2
plot(x1, y3, 'LineWidth', 2); % Function y3
hold off;
% Add labels and title
grid on;
xlabel('x-axis');
ylabel('y-axis');
% Add a legend
legend('Data Points Given', 'Cubic Polynomial Fit', 'Function P1(x)', 'Function P2(x)', 'Function P3(x)', 'Location', 'best');
% Make x and y axes bold
set(gca, 'LineWidth', 1.5);
% set(gca, 'FontWeight', 'bold');
Ax = gca;
Ax.XAxis.FontWeight = 'bold'; % <— ADDED
Ax.YAxis.FontWeight = 'bold'; % <— ADDED
.
3 Comments
Star Strider
on 6 Apr 2024
Then you didn’t see the difference in the legend.
In OP’s code, the legend is also bolded, in my code it’s not. (There doesn’t appear to be any way to only bold the tick labels and not the axis labels.)
Also, I’m taking my cue from this in OP’s code:
set(gca, 'FontWeight', 'bold');
So I assume OP wants the font is to be bold.
John D'Errico
on 6 Apr 2024
But from the comments, we see this question from @Dyuman Joshi
"Do you mean to bold just the axes and origin and not the x and y labels?"
And the answer from @Gihahn
"Yes like the origan axis for both the x and y axis on the graph self "
John D'Errico
on 6 Apr 2024
Edited: John D'Errico
on 6 Apr 2024
It seems simple enough to me. If you want to have ONLY the x and y axes emboldened...
% arbitrary example plot, and there is no need to use the large example
% you had, since this suffices to show how to solve the problem
plot(1:5,magic(5),'-')
xl = xlim;
yl = ylim;
H = line(xl([1 1 2]),yl([2 1 1]));
H.LineWidth = 3; H.Color = 'k';
Fairly bold, if I do say so myself.
0 Comments
Voss
on 6 Apr 2024
You can use xline and yline.
% Given data points
x = [-0.1, 0, 0.2, 0.3];
y = [5.3, 2, 3.19, 1];
% Define the range of x values for the function plot
x1 = linspace(-0.1, 0.3, 100); % Adjust the range as needed
% Define the functions y1, y2, and y3
y3 = -556.6667 * x1.^3 + 185.5 * x1.^2 - 8.88333 * x1 + 2;
y1 = -33 * x1 + 2;
y2 = 129.83333 * x1.^2 - 20.016667 * x1 + 2;
% Fit a cubic polynomial to the data
coefficients = polyfit(x, y, 3); % 3 for cubic polynomial
% Generate points along the x-axis for plotting the polynomial
x_values = linspace(min(x), max(x), 100);
% Evaluate the cubic polynomial at these points
y_values = polyval(coefficients, x_values);
box on
grid on
hold on
xline(0,'k','LineWidth',2,'HandleVisibility','off')
yline(0,'k','LineWidth',2,'HandleVisibility','off')
% Plot the data points, the cubic polynomial fit, and the functions
plot(x, y, 'o', 'MarkerFaceColor', 'b', 'MarkerSize', 8); % Data points
plot(x_values, y_values, 'LineWidth', 2); % Cubic polynomial
plot(x1, y1, 'LineWidth', 2); % Function y1
plot(x1, y2, 'LineWidth', 2); % Function y2
plot(x1, y3, 'LineWidth', 2); % Function y3
% Add labels and title
xlabel('x-axis');
ylabel('y-axis');
% Add a legend
legend('Data Points Given', 'Cubic Polynomial Fit', 'Function P1(x)', 'Function P2(x)', 'Function P3(x)', 'Location', 'best');
% Make x and y axes bold
set(gca, 'LineWidth', 1.5);
set(gca, 'FontWeight', 'bold');
0 Comments
Steven Lord
on 6 Apr 2024
If you wanted to highlight the X and Y axes by making them cross at the origin, set the XAxisLocation and YAxisLocation properties of the axes. It's not exactly what you asked, but it may satisfy the same purpose (if your purpose is to draw attention to the axes.)
x = -180:180;
ax = axes;
plot(ax, x, sind(x))
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
% Let's also set some ticks
xticks(-180:45:180)
yticks(-1:0.25:1)
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!