How to add right ylabel with complex values (attached data and code)

조회 수: 1 (최근 30일)
Ismaeel
Ismaeel 2019년 3월 5일
댓글: Star Strider 2019년 3월 6일
I have a matrix attached called "PF". I read the matrix and plot it using pcolor. This matrix has x-label named "x_state" and left y-axis named "DR"; both attached. I want to add ylabel to the righ axis giving the complex array called "EV" attached.
Any idea?
Thanks
clear all; clc;
PF = xlsread('PF.xlsx'); % Main data to be plotted
DR = xlsread('DR.xlsx'); % used to label left y-axis
[~, TEXT, ~] = xlsread('States.xlsx'); x_state=TEXT; % used to label x-axis
[~, TEXT, ~] = xlsread('EV.xlsx'); EV=TEXT; % I want to add these complex values to the right y-axis
pcolor(PF)
xticks(1:176);yticks(1:176);
xticklabels(x_state)
xtickangle(90)
yticklabels(num2str(DR*100))
ytickangle(0)
ylabel('DR (%)')
xlabel('States')
axis([65 96 65 96])

답변 (3개)

Alex Mcaulley
Alex Mcaulley 2019년 3월 6일
  댓글 수: 3
Alex Mcaulley
Alex Mcaulley 2019년 3월 6일
Is this that you want?
clear all; clc;
PF = xlsread('PF.xlsx'); % Main data to be plotted
DR = xlsread('DR.xlsx'); % used to label left y-axis
[~, TEXT, ~] = xlsread('States.xlsx'); x_state=TEXT; % used to label x-axis
[~, TEXT, ~] = xlsread('EV.xlsx'); EV=TEXT; % I want to add these complex values to the right y-axis
pcolor(PF)
xticks(1:176);yticks(1:176);
xticklabels(x_state)
xtickangle(90)
yticklabels(num2str(DR*100))
ytickangle(0)
ylabel('DR (%)')
yyaxis 'right'
ax = gca;
ax.YAxis(2).TickLabels = cellfun(@num2str,EV,'UniformOutput',false);
ax.YAxis(2).TickValues=1:numel(EV);
ylabel('EV (%)')
xlabel('States')
axis([65 96 65 96])
Ismaeel
Ismaeel 2019년 3월 6일
Thanks Alex for your efforts. Yes, but when I zoom in or out, the right y-axis labels do not change accordingly; they are consant unlike the left y-axis labels. Is there a way to solve this? Thanks once again.

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


Star Strider
Star Strider 2019년 3월 6일
Try this:
x1 = sort(rand(1,10)); % Create Data
y1 = rand(1,10); % Create Data
x2 = sort(rand(1, 15)); % Create Data
y2 = rand(1, 15)+0.5; % Create Data
figure
ax = gca;
yyaxis left
plot(x1, y1)
yyaxis right
plot(x2, y2)
yt2 = ax.YAxis(2).TickValues; % Get Current Yick Values
ytv = sort(exp(1j*rand(1,7))); % Define Complex YAxis(2) Tick Values
yt2new = linspace(min(yt2), max(yt2), numel(ytv)); % Create Tick Positions Corresponding To ‘ytv’
y2lbls = sprintfc('%.3f%+.3fj', ytv); % Create Complex YAxis(2) Tick Labels Cell Array: ‘sprintfc’
y2lbls = compose('%.3f%+.3fj', ytv); % Create Complex YAxis(2) Tick Labels Cell Array: ‘compose’
ax.YAxis(2).TickValues = yt2new; % Set New Tick Values
ax.YAxis(2).TickLabels = y2lbls; % Set New Tick Labels
You already have a vector for ‘ytv’, so substitute it for my vector. (Keeping the names the same with a different vector will simplify your using my code.)
Experiment to get the result you want.
  댓글 수: 6
Ismaeel
Ismaeel 2019년 3월 6일
Thanks Star Stider. I gave it another try but with no progress.

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


Ismaeel
Ismaeel 2019년 3월 6일
Plotting is a basic problem in many fileds. I wonder how Matlab after years of feedbacks did not solve such a problem. The prolem is simple, one wants to plot data with additional right y-axis given by complex values. There should be no hard coding to plot such data.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by