I am trying to use plotyy to specify the axes on a graph but I am struggling to figure out how to use it

1 vue (au cours des 30 derniers jours)
I am plotting a graph that uses semilog and plot and I want to specify the left and right axes as they use different units.
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 5);
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "Z1:AD202";
% Specify column names and types
opts.VariableNames = ["PressureMpa", "PoreDiameternm", "CumulativePoreVolumemLg", "SmoothedDVdlogDPoreVolumemLg", "OfTotalIntrusionVolume"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
% Import the data
mean = readtable("mean.xlsx", opts, "UseExcel", false);
%% Clear temporary variables
clear opts
figure
% Create axes
axes1 = axes('Parent',figure);
hold(axes1,'on');
semilogx(mean.PoreDiameternm(1:92),mean.SmoothedDVdlogDPoreVolumemLg(1:92),'DisplayName','Pore Size Distribution')
semilogx(mean.PoreDiameternm(1:92),mean.OfTotalIntrusionVolume(1:92),'LineStyle','-','Color','r','DisplayName','% Of Total Intrusion Volume');
% Activate the right side of the axes
yyaxis(axes1,'right');
set(axes1,'YColor',[0 0 0]);
% Uncomment the following line to preserve the Y-limits of the axes
% ylim(axes1,[0 0.2]);
ylabel('Smoothed dV/dlogD Pore Volume (mL/g)','Interpreter','latex')
yyaxis(axes1,'left');
% Uncomment the following line to preserve the Y-limits of the axes
% ylim(axes1,[0 0.2]);
ylabel('% Of Total Intrusion Volume','Interpreter','latex')
xlabel('Pore Diameter nm','Interpreter','latex')
hold(axes1,'off');
% Set the remaining axes properties
set(axes1,'XDir','reverse','XMinorTick','on','XScale','log');
Warning: Error in state of SceneNode.
String scalar or character vector must have valid interpreter syntax:
Any help would be greatly aprreciated
edit, made it % of total volume as opposed to cumulative volume so my problem makes a little more sense
and I reuploaded my data
  4 commentaires
A Poyser
A Poyser le 28 Fév 2025
I am a very rookie user of MatLab so I often use the GUI to generate code and then edit it. In this case I chpped out a piece of code I have been using that has been making 6 figures but I am only intereted in one of them (figure6) but I forgot to remove that line in the question.
Les Beckham
Les Beckham le 28 Fév 2025
Modifié(e) : Les Beckham le 28 Fév 2025
Unless you provide valid data (non-NaN), you are going to have a hard time getting help with plotting it. Did you read all of my comments? Your updated code has not addressed all of those issues.

Connectez-vous pour commenter.

Réponse acceptée

A Poyser
A Poyser le 3 Mar 2025
Here is how I solved it
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 5);
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "Z1:AD202";
% Specify column names and types
opts.VariableNames = ["PressureMpa", "PoreDiameternm", "CumulativePoreVolumemLg", "SmoothedDVdlogDPoreVolumemLg", "OfTotalIntrusionVolume"];
opts.VariableTypes = ["double", "double", "double", "double", "double"];
% Import the data
mean = readtable("mean.xlsx", opts, "UseExcel", false);
% Clear temporary variables
clear opts
%% Create figure
figure;
% Activate left y-axis
yyaxis left;
semilogx(mean.PoreDiameternm(1:92), mean.SmoothedDVdlogDPoreVolumemLg(1:92), '-b', 'DisplayName', 'Pore Size Distribution');
ylabel('Smoothed dV/dlogD Pore Volume (mL/g)','Interpreter','latex');
% Activate right y-axis
yyaxis right;
semilogx(mean.PoreDiameternm(1:92), mean.OfTotalIntrusionVolume(1:92), '-r', 'DisplayName', '\% Of Total Intrusion Volume');
ylabel('\% Of Total Intrusion Volume','Interpreter','latex');
% Ensure both y-axes are black
yyaxis left;
set(gca, 'YColor', 'k');
yyaxis right;
set(gca, 'YColor', 'k');
% General plot settings
xlabel('Pore Diameter (nm)','Interpreter','latex');
set(gca, 'XDir', 'reverse', 'XMinorTick', 'on', 'XScale', 'log', 'YColor', 'k');
legend('Location','northwest','Interpreter','latex');
hold off;
with the help of chat gpt

Plus de réponses (0)

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by