How to plot y-axis limits and y-limitation line
조회 수: 52 (최근 30일)
이전 댓글 표시
Hello All,
Using the code below, I have defined y-axis limits of -220 to 220 but when I run the plot the max and min values I can see are -200 and 200 and then the graph further extends to my desired limits. Is there any additional piece of code I can use to display the actual limits of -220 & 220?
Also, whenever I add a y axis limitation line (2 y-axis lines in red), 'data 1' and 'data 2' appears in the legend. Is there any way of completely getting rid of these words from the legend box or replacing 'data1' with 'Torque limit of motor' and 'data2' with 'Torque limit of motor' in the legend box?
Any help would be greatly appreciated
댓글 수: 0
답변 (3개)
Steven Lord
2023년 7월 7일
If you want to ensure that MATLAB creates ticks and tick labels at particular values, I would use a combination of the xticks or yticks functions along with potentially the xticklabels or yticklabels functions.
figure
x = 1:10;
plot(x, x.^2, 'o-')
grid on
Note that the tick locations were automatically selected by MATLAB and the tick labels automatically generated. The grid lines do not necessarily correspond with the y coordinates of the markers. To specify locations on the Y axis, call yticks.
figure
x = 1:10;
plot(x, x.^2, 'o-')
yticks(x.^2)
grid on
Here the grid lines from the y axis do correspond to the markers.
If you want to add tick locations, calling union to merge the existing tick locations with the specified ones may be useful.
figure
x = 1:10;
plot(x, x.^2, 'o-')
automaticLocations = yticks;
automaticPlusSpecified = union(automaticLocations, x.^2);
yticks(automaticPlusSpecified)
grid on
Though this is a bit messy since 0 and 1, 9 and 10, 49 and 50, and 80 and 81 are so close to one another. The multiples of 10 are from automaticLocations while the others come from x.^2.
As for your other question about yline, the label used to display the line is different from the display name used by legend. You can set both, though. In the example below 7^2 is the label displayed near the line while 'Seven squared' is the DisplayName property value that legend uses.
figure
plot(x, x.^2, 'o-')
yline(49, 'r:', '7^2', 'DisplayName', 'Seven squared')
legend show
If you want to control which objects appear in the legend, assign the handles returned by plot, yline, or other graphics commands into a variable or variables then pass a vector of those handles whose objects you want to appear in the legend into the legend function.
figure
hPlot = plot(x, x.^2, 'o-', 'DisplayName', 'x^2');
hYline = yline(49, 'r:', '7^2', 'DisplayName', 'Seven squared');
legend(hYline) % Show just the yline in the legend
Dyuman Joshi
2023년 7월 7일
편집: Dyuman Joshi
2023년 7월 7일
"Is there any additional piece of code I can use to display the actual limits of -220 & 220?"
Yes, Ticks, specifically yticks
%Random example
x = 0:0.1:10;
y = sin(x);
plot(x,y)
ylim([-2.5 3.5])
%red color
yline(-2, '--r', 'Random lower limit')
%magenta color
yline(3, '--m', 'Random upper limit')
%Modify ticks on y axis as required
%I have set the ticks from -2.5 to 3.5 at the interval of 0.5
yticks(-2.5:0.5:3.5)
"Is there any way of completely getting rid of these words from the legend box or replacing 'data1' with 'Torque limit of motor' and 'data2' with 'Torque limit of motor' in the legend box?"
Yes, define the input for the legend accordingly. Note that this method requires you to use the legend function after making all the plots.
%Defining string as input to legend()
str = {'Sine Curve', '', ''};
%Use empty strings for the plots you don't want to show the legend for
legend(str, 'Location', 'best')
댓글 수: 2
Dyuman Joshi
2023년 7월 11일
편집: Dyuman Joshi
2023년 7월 11일
You do not get your desired limit because of the input you have given.
x=(-220:200:1200)'
The vector you have defined does not contain 2000, and thus it the value is not displayed on the plot.
You will have to change the increment, or change the initial point, so that the vector includes the desired value.
For example > using -200 as the starting point is an option -
%E.g.
x=(-200:200:1200)'
Star Strider
2023년 7월 7일
I am not certain what you want. You set the xlim values and drew the yline annotations, so you obviously want those as they are.
The only improvement I can offer is to eliminate the other two legend entries for the yline calls. The eassiest way to do that is to use
hs = stairs(x, y, 'DisplayName','MEMotorR');
and
legend(hs, 'Location','best')
If you want to include more than one plot call in the legend call, return the handles just as I did here with the stairs call, and then concatenate them in square brackets, for example:
legend([hs hp], 'Location','best')
where ‘hs’ would be a stairs call and ‘hp’ would be a plot call. (Call them anything you want to, so that they make sense in the context of your code.)
All those together would be soething like this —
x = 0:245;
y = rand(size(x))*50;
figure
hs = stairs(x, y, 'DisplayName','MEMotorR');
yline([-200 200], '--r', 'Torque limit of motor')
xlabel('Time (s)')
ylabel('MEMotorR (Nm)')
title('Motor Torque vs. Time')
ylim([-220 220])
legend(hs, 'Location','best')
.
댓글 수: 2
Star Strider
2023년 7월 11일
O.K. I was not certain what result you wanted. The tick values are correct, as demonstrated here —
Y_Tick_Vals = -220 : 200 :1200 % Check Tick Values
x = 0:245;
y = rand(size(x))*1E3-20;
figure
hs = stairs(x, y, 'DisplayName','MEMotorR');
% yline([-200 200], '--r', 'Torque limit of motor')
xlabel('Time (s)')
ylabel('MEMotorR (Nm)')
title('Motor Torque vs. Time')
ylim([-220 1200])
legend(hs, 'Location','best')
yticks(-220:200:1200)
figure
hs = stairs(x, y, 'DisplayName','MEMotorR');
% yline([-200 200], '--r', 'Torque limit of motor')
xlabel('Time (s)')
ylabel('MEMotorR (Nm)')
title('Motor Torque vs. Time')
ylim([-220 1200])
legend(hs, 'Location','best')
ytix = linspace(-220, 1200, 6);
yticks(ytix)
The reason has to do with the increment of 200. The tick values begin from -220 and then add 200 each time, producing:
-220
-220 + 200 = -20
-20 + 200 = 180
180 + 200 =380
380 + 200 = 580
580 + 200 = 780
780 + 200 = 980
980 + 200 = 1180
1180 + 200 = 1380
However 1380 is beyond the upper set limit of the colon operator range here (1200) so it stops at 1180. That is simply how the colon, : operator works.
ytix = linspace(-220, 1200, 11)
ytix(1:8)
ytix(9:end)
ytix = linspace(-220, 1200, 6)
or a different colon step increment or start and stop limits.
.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!