Plot the same data once normalized values on the left y-axis and once with units on the right ; the same for bottom and top x-axis

4 ビュー (過去 30 日間)
I want to plot the same data once with normalized values on the left y-axis and once with units (GW) on the right ; the same for bottom and top x-axis. I tried the code below based on research in Matlab answers, but it does not work (I include the error I am getting below). Also, I want to have the same for x axis too, but I do not know how to implement the yyaxis for the x axis. Could you help me please? Thanks.
c=[2.88 1.27 100780000.00 44526000.00
2.88 1.27 100860000.00 44561000.00
2.89 1.28 100940000.00 44599000.00
2.93 1.16 102320000.00 40641000.00
2.93 1.16 102400000.00 40673000.00
2.87 1.40 100200000.00 48844000.00
2.87 1.40 100280000.00 48886000.00
2.87 1.40 100360000.00 48924000.00
2.87 1.40 100440000.00 48963000.00
2.88 1.40 100520000.00 49001000.00];
wn=c(:,1);
sn=c(:,2);
w=c(:,3);
s=c(:,4);
figure();
yyaxis left
plot(wn,sn,'-ok');
set(gca, 'YLim', [min(sn), max(sn)]) % #important
yyaxis right
set(gca,'YTick',s, 'YLim', [min(s), max(s)]) % #important
ylabel('PS GW');
% go back to left axes (main axes)
yyaxis left
grid on;
xlabel('Pw Normalized');
ylabel('PS Normalized');
I am getting this error below. However if I sort the order then it will not be the same vector.
Error using matlab.graphics.axis.Axes/set
Value must be a numeric vector whose values increase.
Error in TwoTwoNewRevApplast (line 27)
set(gca,'YTick',s, 'YLim', [min(s), max(s)]) % #important

回答 (1 件)

Hassaan
Hassaan 2024 年 3 月 21 日
編集済み: Hassaan 2024 年 3 月 21 日
% Sample Data
c = [2.88 1.27 100780000.00 44526000.00; 2.88 1.27 100860000.00 44561000.00; 2.89 1.28 100940000.00 44599000.00; 2.93 1.16 102320000.00 40641000.00; 2.93 1.16 102400000.00 40673000.00; 2.87 1.40 100200000.00 48844000.00; 2.87 1.40 100280000.00 48886000.00; 2.87 1.40 100360000.00 48924000.00; 2.87 1.40 100440000.00 48963000.00; 2.88 1.40 100520000.00 49001000.00];
wn = c(:,1);
sn = c(:,2);
w = c(:,3)/1e6; % Convert to GW for illustration
s = c(:,4)/1e6; % Convert to GW for illustration
% Normalizing data for illustration
wn_norm = (wn - min(wn)) / (max(wn) - min(wn));
sn_norm = (sn - min(sn)) / (max(sn) - min(sn));
% Assuming wn and w are your original x-data and the converted scale you wish to display.
figure;
yyaxis left
plot(wn, sn_norm, '-ok'); % Plot normalized data vs. original x-data
ylabel('Normalized PS');
yyaxis right
plot(wn, s, '-.xr'); % Plot original units data vs. original x-data
ylabel('PS (GW)');
xlabel('Pw');
grid on;
title('Dual Y-axis Example');
% Creating a top X-axis for a different scale or unit representation
ax1 = gca; % Get the current (left) axes
ax1_pos = ax1.Position; % Get the position of the original axes for alignment
% Create a new axes for the top X-axis
ax2 = axes('Position', ax1_pos, 'XAxisLocation', 'top', 'Color', 'none', 'YTick', [], 'YColor', 'none');
% Set the limits of the new axes to match the original
ax2.XLim = ax1.XLim;
% Assumming wn represents the x-data. For the top X-axis, we want to display
% One approach can be to use linspace to generate ticks over the range of wn
numTicks = 5; % Example: Number of ticks you want
newTicks = linspace(min(wn), max(wn), numTicks);
ax2.XTick = newTicks;
% Assuming 'w' contains the data in the transformed scale or units we want to display,
% calculate a representative set of labels based on the new tick positions
% A simplistic approximation and should be adjusted according to needs
newLabels = linspace(min(w), max(w), numTicks);
ax2.XTickLabel = arrayfun(@num2str, newLabels, 'UniformOutput', false);
xlabel(ax2, 'Transformed Scale or Units');
% Ensuring the left y-axis (yyaxis left) is active for further interactions
axes(ax1); % Makes the left y-axis active
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 件のコメント
Katerina F
Katerina F 2024 年 3 月 21 日
編集済み: Katerina F 2024 年 3 月 21 日
Thanks. I think I should clarify.
What I want to plot is already there in my code, no need for conversions because are already converted. I want to plot the vector sn versus wn in y and x axis in correspondance and the vector s versus w in the secondary y axis (right) and on the secondary x axis (top). But they are the same data, that is there should be just one line in the end not two.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeExploration and Visualization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by