How to plot y-axis limits and y-limitation line

조회 수: 52 (최근 30일)
Mahnoor
Mahnoor 2023년 7월 7일
댓글: Star Strider 2023년 7월 11일
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

답변 (3개)

Steven Lord
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
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
Mahnoor
Mahnoor 2023년 7월 11일
편집: Mahnoor 2023년 7월 11일
Thankyou @Dyuman Joshi for your response. A quick question, when I put in the code for yticks, do I need to identify the interval between the first and final limits as well? Can this be automatically done in a way as I still cannot seem to get my desired y-axis limits on the plot without constantly adjusting the spacing interval (220 in the code of y ticks).
Dyuman Joshi
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)'
x = 8×1
-220 -20 180 380 580 780 980 1180
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)'
x = 8×1
-200 0 200 400 600 800 1000 1200

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


Star Strider
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
Mahnoor
Mahnoor 2023년 7월 11일
Thankyou @Star Strider for your response. I still cannot seem to get my desired limits using yticks.
Star Strider
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
Y_Tick_Vals = 1×8
-220 -20 180 380 580 780 980 1180
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.
consider using linspace instead —
ytix = linspace(-220, 1200, 11)
ytix = 1×11
-220 -78 64 206 348 490 632 774 916 1058 1200
ytix(1:8)
ans = 1×8
-220 -78 64 206 348 490 632 774
ytix(9:end)
ans = 1×3
916 1058 1200
ytix = linspace(-220, 1200, 6)
ytix = 1×6
-220 64 348 632 916 1200
or a different colon step increment or start and stop limits.
.

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

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by