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

1 view (last 30 days)
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)

Alex Mcaulley
Alex Mcaulley on 6 Mar 2019
  3 Comments
Alex Mcaulley
Alex Mcaulley on 6 Mar 2019
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 on 6 Mar 2019
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.


Star Strider
Star Strider on 6 Mar 2019
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
Ismaeel
Ismaeel on 6 Mar 2019
Thanks Star Stider. I gave it another try but with no progress.

Sign in to comment.


Ismaeel
Ismaeel on 6 Mar 2019
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 Geographic Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!