Trying to have a Matlab line plot with Xticks coresponding to numbers with unequal numerical gap but equal spacing.
1 次查看(过去 30 天)
显示 更早的评论
I want to have a Matlab Line plot between the following vectors:
x = [3 4 6 7 9 10 12 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]; and y1=[0.952 1.04 1.033 0.968 1.01 0.989 0.994 0.957 1.001 1.042 1.01 0.962 0.971 1.001 0.961 1.02 0.997 0.954 1.001 1.03 0.971 1.001 0.961 1.0281 ]; using the following code in Matlab:
x = [3 4 6 7 9 10 12 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]; % Create Data
y = [0.964 0.957 1.03954 0.979 0.958 1.001 0.97 1 1.021 0.981 1.01 1.047 1.029 1.033 1.045 1.033 0.951 0.98 0.964 1.045 1.023 1.031 0.954 0.998]; % Create Data
y1=[0.952 1.04 1.033 0.968 1.01 0.989 0.994 0.957 1.001 1.042 1.01 0.962 0.971 1.001 0.961 1.02 0.997 0.954 1.001 1.03 0.971 1.001 0.961 1.0281 ];
ub = 1.05*ones(1,24); % Create Data: Upper Bound
lb = 0.95*ones(1,24);
plot(y);
xticks(1:length(y));
xticklabels(string(x));
hold on
plot(y1,'g')
xticks(1:length(y1));
xticklabels(string(x));
hold on
plot(ub,'g')
xticks(1:length(ub));
xticklabels(string(x));
hold on
plot(lb,'g')
xticks(1:length(lb));
xticklabels(string(x));
But I'm getting the output as shown below-
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027860/image.png)
But, following is the graph, which I am trying to get.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1027865/image.png)
Kindly advice.
0 个评论
回答(2 个)
Voss
2022-6-10
It looks like your xticks and xticklabels are ok (except you don't need to set them more than once - you also don't need to do hold on more than once), so what remains to be done is to set up the grid, legend, axes limits, and the text labels for the upper and lower bound lines.
(You may want to adjust the line colors, markers, and DisplayNames, because it's not clear to me which of y and y1 in your code correspond to which of MOAGDE, OMNI, MOringPSOSCD in the image.)
x = [3 4 6 7 9 10 12 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30]; % Create Data
y = [0.964 0.957 1.03954 0.979 0.958 1.001 0.97 1 1.021 0.981 1.01 1.047 1.029 1.033 1.045 1.033 0.951 0.98 0.964 1.045 1.023 1.031 0.954 0.998]; % Create Data
y1=[0.952 1.04 1.033 0.968 1.01 0.989 0.994 0.957 1.001 1.042 1.01 0.962 0.971 1.001 0.961 1.02 0.997 0.954 1.001 1.03 0.971 1.001 0.961 1.0281 ];
nx = numel(x);
ub = 1.05;
lb = 0.95;
% note: I'm using the colors and markers as shown in
% your figure, as opposed to what's used in your code:
h = plot(y,'-md','DisplayName','y');
hold on
h1 = plot(y1,'-go','DisplayName','y1');
plot([1 nx],[ub ub],'--b','LineWidth',2)
plot([1 nx],[lb lb],'--b','LineWidth',2)
text(nx/2,ub,'Upper bound of PQ bus', ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom')
text(nx/2,lb,'Lower bound of PQ bus', ...
'HorizontalAlignment','center', ...
'VerticalAlignment','top')
grid on
xlim([1 numel(y)]);
ylim([0.9 1.08])
xticks(1:length(y));
xticklabels(string(x));
legend([h h1],'Location','South','Orientation','horizontal')
set(get(gca(),'XAxis'),'TickLabelRotation',0)
0 个评论
dpb
2022-6-10
编辑:dpb
2022-6-10
Good start; a little extra than needed in some places, not quite enough others... :)
x=[3:30].'; x(3:3:end)=[];
y = [0.964 0.957 1.03954 0.979 0.958 1.001 0.97 1 1.021 0.981 1.01 1.047 1.029 1.033 1.045 1.033 0.951 0.98 0.964 1.045 1.023 1.031 0.954 0.998;
0.952 1.04 1.033 0.968 1.01 0.989 0.994 0.957 1.001 1.042 1.01 0.962 0.971 1.001 0.961 1.02 0.997 0.954 1.001 1.03 0.971 1.001 0.961 1.0281 ].';
ub = 1.05;
lb = 0.95;
hL=plot(y);
xticks(1:length(y));
xticklabels(string(x));
xlim([1 length(y)])
grid on
hold on
ylim([0.9 1.1])
yline(ub,'b--','Upper bound of PQ bus','linewidth',2,'labelhorizontalalign','center');
yline(lb,'b--','Lower bound of PQ bus','linewidth',2,'labelhorizontalalign','center','labelverticalalign','bottom');
hAx=gca;
hAx.YAxis.TickLabelFormat='%0.2f';
legend('WhatIsY','AndY1?','Location','south',"Orientation","horizontal")
Above figure came from the interactive running which decided needed to rotate xticklabels with default font size and with the close tick spacing set. You can play and set those properties as suits...
0 个评论
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!