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

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])

Answers (3)

3 Comments

Thanks Alex for your answer. My data for y-axis are complex values. I tried using yyaxis but I could not get any result.
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])
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.

Sign in to comment.

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 Comments

Thanks Star Strider. The problem is that the labels of one axis is fixed and the other change. In your example, left y-axis is constat regardless zooming in or out. In my example using your code, the right y-axis is constant. In my work, both should change at the same time with the scale.
My pleasure.
With fixed ‘YAxis(2).TickValues’ designations, the ticks and tick values will remain fixed, since defining them as you do here over-rides any interpolation the plot functions would otherwise do. They will scale correctly when you expand the axes by zooming in on the plot, however they will not interpolate.
I also replaced your pcolor call with an imagesc call.
Try this:
PF = xlsread('PF.xlsx'); % Main data to be plotted
DR = xlsread('DR.xlsx'); % used to label left y-axis
[~, TEXTx, ~] = xlsread('States.xlsx');
x_state=TEXTx; % used to label x-axis
[~, TEXTy2, ~] = xlsread('EV.xlsx');
EV=TEXTy2; % I want to add these complex values to the right y-axis
figure
imagesc(PF)
ax = gca;
yyaxis left
ax.XAxis.TickValues = (1:176);
ax.YAxis(1).TickValues = (1:176);
ax.YAxis(1).FontSize = 6;
xticklabels(x_state)
xtickangle(90)
ax.XAxis.FontSize = 6;
yyaxis right
yt2 = ax.YAxis(2).TickValues; % Get Current Yick Values
yt2new = linspace(min(yt2), max(yt2), numel(EV)); % Create Tick Positions Corresponding To ‘ytv’
ax.YAxis(2).TickValues = yt2new; % Set New Tick Values
ax.YAxis(2).TickLabels = EV; % Set New Tick Labels
ax.YAxis(2).FontSize = 6;
Experiment to get the result you want.
I tried your last code, but still same result as shown in the figure attached. Please see how the left y-axis change when I zoomed it in, whereas the right y-axis stayed unchanged.
The right y-axis is defined not only by your fixing the tick positions and values, but also by defining them as a cell array of text values.
The left y-axis is defined as a continuous vector of integer values, however note that the left y-axis remains fixed with integer values. They will not change either (for example to decimal fractions) if you zoom them.
Thanks Star Stider. I gave it another try but with no progress.

Sign in to comment.

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.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 5 Mar 2019

Commented:

on 6 Mar 2019

Community Treasure Hunt

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

Start Hunting!