필터 지우기
필터 지우기

is there a way to make the 0 x and y axis bold?

조회 수: 57 (최근 30일)
Gihahn
Gihahn 2024년 4월 6일
답변: Steven Lord 2024년 4월 6일
% 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
Dyuman Joshi
Dyuman Joshi 2024년 4월 6일
편집: Dyuman Joshi 2024년 4월 6일
"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?
Gihahn
Gihahn 2024년 4월 6일
Yes like the origan axis for both the x and y axis on the graph self

댓글을 달려면 로그인하십시오.

답변 (4개)

Star Strider
Star Strider 2024년 4월 6일
To make only the tick labels bold, use the FontWeight property in the NumericRuler Properties
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
Star Strider
Star Strider 2024년 4월 6일
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
John D'Errico 2024년 4월 6일
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
John D'Errico 2024년 4월 6일
편집: John D'Errico 2024년 4월 6일
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.

Voss
Voss 2024년 4월 6일
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');

Steven Lord
Steven Lord 2024년 4월 6일
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)

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by