how to do a scatter plot with second x axes

Hello everyone,
I am trying to plot a scatter plot with a secondary x-axis. On the x1-axis, the graph should have the value 1/temperature(variable tempK) where the temperature is in Kelvin, and on the y-axis(variable(lnkeq), the natural logarithm of keq. On the secondary x-axis(varable tempC)
, I need to have the temperature plotted in degrees Celsius. i need to correct figure 1.
something like that
clc
clear all
close all
%%
%cicle for obtain the sample data content directly from the .xlsx file
for i=1:1
table = readtable('P_T_Ni.xlsx');
temperature(:,i) = table.temperature;
pressure(:,i) = table.pressure;
Ni_grt(:,i) = table.Ni_Grt;
Ni_ol(:,i) = table.Ni_Ol;
Cr_grt(:,i) = table.Wt_Cr;
Ca_grt(:,i) = table.Wt_Ca;
end
%% calculation
%Keq
Ni_grt = Ni_grt(~isnan(Ni_grt), :);
Keq= Ni_grt ./2900;
%temperature from °C to K
temperature = temperature +273.15 ;
%Ni olivine mean
%Ni_ol = Ni_ol(~isnan(Ni_ol), :);
%mean_Ni_ol = mean(Ni_ol);
%% ignore the NaN value
temperature = temperature(~isnan(temperature), :);
pressure = pressure(~isnan(pressure), :);
Ca_grt = Ca_grt(~isnan(Ca_grt), :);
Cr_grt = Cr_grt(~isnan(Cr_grt), :);
%% Calculate the parameters (ΔH, ΔV, ΔS)
pressure_keq=[pressure Keq];
% function of T
T = @(x,pressure_Keq) (x(1) + pressure_Keq(:,1) * x(2)) ./ (x(3) - log( pressure_Keq(:,2)));
% fixed start parameters (ΔH, ΔV, ΔS)
% The chosen values must be of the same order of magnitude as the expected values
x0 = [1000, 10, 1];
% nonlinear regression with lsqcurvefit
x_fit = lsqcurvefit(T, x0, pressure_keq, temperature);
% Extract the fitted parameters
DeltaH = x_fit(1);
DeltaV = x_fit(2);
DeltaS = x_fit(3);
% the new value od temperature from Ni_grt of database in Kelvin
predicted_temperature = T(x_fit, pressure_keq);
% print the result
fprintf('Parametri adattati:\n');
fprintf('ΔH = %.2f\n', DeltaH);
fprintf('ΔV = %.2f\n', DeltaV);
fprintf('ΔS = %.2f\n', DeltaS);
%fprintf('Ni_ol = %.2f\n', mean_Ni_ol);
%% diff T(Ni-in-grt)-T(TA98)
deltaT = predicted_temperature-temperature;
mean_deltaT = mean(deltaT);
fprintf('mean_detaT = %.2f\n', mean_deltaT);
%% 1 sigma statistics
%calculate standard deviation
sigma_value=std(deltaT);
fprintf ('1σ of deltaT=%.2f\n', sigma_value);
%% R^2
R_sq= 1-var(temperature-predicted_temperature)/var(temperature);
fprintf('R^2=%.2f\n',R_sq);
%% graphs
%transform the pressure from Kbar to Gpa
pressure= pressure/10;
%trasform the Temperature from k to °C
temperature = temperature-273.15;
%1/T in mkelvin e lnkd
%on x bottom axes
tempK = (1 ./(temperature+273));
%on x top axes
tempC = temperature;
%on y left axes
lnKeq= log(Keq);
figure(1);
t= tiledlayout(1,1);
ax2= axes(t);
xlabel('1/T (K)');
xlabel(ax2,'Temperature °C');
ylabel('lnKd')
plot(ax2,tempK,lnKeq,'x')
ax1= axes(t);
ax1.XAxis.Exponent = 0;
plot(ax1,tempK,lnKeq,'*');
ax2.XAxisLocation = 'top';
ax2.XAxis.Exponent = 0;
ax1.Box = 'off';
ax2.Box = 'off';

 Accepted Answer

I have modified the code related to the plotting part.
The ytick labels look bold because there are two sets of them, one on top of the another. If you want to change them to look normal, you can set any of the ytick labels to [].
clc
clear all
close all
%%
%cicle for obtain the sample data content directly from the .xlsx file
for i=1:1
table = readtable('P_T_Ni.xlsx');
temperature(:,i) = table.temperature;
pressure(:,i) = table.pressure;
Ni_grt(:,i) = table.Ni_Grt;
Ni_ol(:,i) = table.Ni_Ol;
Cr_grt(:,i) = table.Wt_Cr;
Ca_grt(:,i) = table.Wt_Ca;
end
%% calculation
%Keq
Ni_grt = Ni_grt(~isnan(Ni_grt), :);
Keq= Ni_grt ./2900;
%temperature from °C to K
temperature = temperature +273.15 ;
%Ni olivine mean
%Ni_ol = Ni_ol(~isnan(Ni_ol), :);
%mean_Ni_ol = mean(Ni_ol);
%% ignore the NaN value
temperature = temperature(~isnan(temperature), :);
pressure = pressure(~isnan(pressure), :);
Ca_grt = Ca_grt(~isnan(Ca_grt), :);
Cr_grt = Cr_grt(~isnan(Cr_grt), :);
%% Calculate the parameters (ΔH, ΔV, ΔS)
pressure_keq=[pressure Keq];
% function of T
T = @(x,pressure_Keq) (x(1) + pressure_Keq(:,1) * x(2)) ./ (x(3) - log( pressure_Keq(:,2)));
% fixed start parameters (ΔH, ΔV, ΔS)
% The chosen values must be of the same order of magnitude as the expected values
x0 = [1000, 10, 1];
% nonlinear regression with lsqcurvefit
x_fit = lsqcurvefit(T, x0, pressure_keq, temperature);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% Extract the fitted parameters
DeltaH = x_fit(1);
DeltaV = x_fit(2);
DeltaS = x_fit(3);
% the new value od temperature from Ni_grt of database in Kelvin
predicted_temperature = T(x_fit, pressure_keq);
% print the result
fprintf('Parametri adattati:\n');
Parametri adattati:
fprintf('ΔH = %.2f\n', DeltaH);
ΔH = 7737.42
fprintf('ΔV = %.2f\n', DeltaV);
ΔV = 26.79
fprintf('ΔS = %.2f\n', DeltaS);
ΔS = 2.60
%fprintf('Ni_ol = %.2f\n', mean_Ni_ol);
%% diff T(Ni-in-grt)-T(TA98)
deltaT = predicted_temperature-temperature;
mean_deltaT = mean(deltaT);
fprintf('mean_detaT = %.2f\n', mean_deltaT);
mean_detaT = 0.26
%% 1 sigma statistics
%calculate standard deviation
sigma_value=std(deltaT);
fprintf ('1σ of deltaT=%.2f\n', sigma_value);
1σ of deltaT=47.81
%% R^2
R_sq= 1-var(temperature-predicted_temperature)/var(temperature);
fprintf('R^2=%.2f\n',R_sq);
R^2=0.91
%% graphs
%transform the pressure from Kbar to Gpa
pressure= pressure/10;
%trasform the Temperature from k to °C
temperature = temperature-273.15;
%1/T in mkelvin e lnkd
%on x bottom axes
tempK = (1 ./(temperature+273));
%on x top axes
tempC = temperature;
%on y left axes
lnKeq= log(Keq);
t = tiledlayout(1,1);
ax1 = axes(t);
%% Correction
% vvvvv
p1 = plot(ax1,tempC,lnKeq,'k*');
%% Correction
% vvv
xlabel(ax1,'Temperature °C');
ax1.XAxis.Exponent = 0;
ax1.Box = 'off';
ylabel('lnKd')
ax2 = axes(t);
p2 = plot(ax2,tempK,lnKeq,'rx');
xlabel(ax2, '1/T (K)');
ax2.XAxisLocation = 'top';
ax2.XAxis.Exponent = 0;
ax2.Box = 'off';
%% Addition
%Set the color of the axes object, which is on the top, to 'none' so that the underlying plot is visible
ax2.Color = 'none';
%Add the legends for both plots
%You can modify the legend labels as you like
legend([p1, p2], {'Data-set 1 (Celsius)', 'Data-set 2 (Kelvin)'}, 'Location', 'north')

8 Comments

Thank you very much, you are so colse at what i want to do.
however, I would need the black data series not to be plotted. Because the two x-axes must be correlated, meaning that for a value of 1/T on one axis, it must correspond to T on the other axis.
I hope I have explained myself clearly.
So, the values on the x-axis at the bottom should just be the Temperature values in Kelvin?
better in °C but you can plot in K and after i try to convert.
Bottom x-axis in degree Celsius
clc
clear all
close all
%%
%cicle for obtain the sample data content directly from the .xlsx file
for i=1:1
table = readtable('P_T_Ni.xlsx');
temperature(:,i) = table.temperature;
pressure(:,i) = table.pressure;
Ni_grt(:,i) = table.Ni_Grt;
Ni_ol(:,i) = table.Ni_Ol;
Cr_grt(:,i) = table.Wt_Cr;
Ca_grt(:,i) = table.Wt_Ca;
end
%% calculation
%Keq
Ni_grt = Ni_grt(~isnan(Ni_grt), :);
Keq= Ni_grt ./2900;
%temperature from °C to K
temperature = temperature +273.15 ;
%Ni olivine mean
%Ni_ol = Ni_ol(~isnan(Ni_ol), :);
%mean_Ni_ol = mean(Ni_ol);
%% ignore the NaN value
temperature = temperature(~isnan(temperature), :);
pressure = pressure(~isnan(pressure), :);
Ca_grt = Ca_grt(~isnan(Ca_grt), :);
Cr_grt = Cr_grt(~isnan(Cr_grt), :);
%% Calculate the parameters (ΔH, ΔV, ΔS)
pressure_keq=[pressure Keq];
% function of T
T = @(x,pressure_Keq) (x(1) + pressure_Keq(:,1) * x(2)) ./ (x(3) - log( pressure_Keq(:,2)));
% fixed start parameters (ΔH, ΔV, ΔS)
% The chosen values must be of the same order of magnitude as the expected values
x0 = [1000, 10, 1];
% nonlinear regression with lsqcurvefit
x_fit = lsqcurvefit(T, x0, pressure_keq, temperature);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% Extract the fitted parameters
DeltaH = x_fit(1);
DeltaV = x_fit(2);
DeltaS = x_fit(3);
% the new value od temperature from Ni_grt of database in Kelvin
predicted_temperature = T(x_fit, pressure_keq);
% print the result
fprintf('Parametri adattati:\n');
Parametri adattati:
fprintf('ΔH = %.2f\n', DeltaH);
ΔH = 7737.42
fprintf('ΔV = %.2f\n', DeltaV);
ΔV = 26.79
fprintf('ΔS = %.2f\n', DeltaS);
ΔS = 2.60
%fprintf('Ni_ol = %.2f\n', mean_Ni_ol);
%% diff T(Ni-in-grt)-T(TA98)
deltaT = predicted_temperature-temperature;
mean_deltaT = mean(deltaT);
fprintf('mean_detaT = %.2f\n', mean_deltaT);
mean_detaT = 0.26
%% 1 sigma statistics
%calculate standard deviation
sigma_value=std(deltaT);
fprintf ('1σ of deltaT=%.2f\n', sigma_value);
1σ of deltaT=47.81
%% R^2
R_sq= 1-var(temperature-predicted_temperature)/var(temperature);
fprintf('R^2=%.2f\n',R_sq);
R^2=0.91
%% graphs
%transform the pressure from Kbar to Gpa
pressure= pressure/10;
%trasform the Temperature from k to °C
temperature = temperature-273.15;
%1/T in mkelvin e lnkd
%on x bottom axes
tempK = (1 ./(temperature+273));
%on x top axes
tempC = temperature;
%on y left axes
lnKeq= log(Keq);
t = tiledlayout(1,1);
ax1 = axes(t);
%Change the color of the 1st plot to white
p1 = plot(ax1,tempC,lnKeq,'w');
xlabel(ax1,'Temperature °C');
ax1.XAxis.Exponent = 0;
ax1.Box = 'off';
ylabel('lnKd')
ax2 = axes(t);
p2 = plot(ax2,tempK,lnKeq,'rx');
xlabel(ax2, '1/T (K)');
ax2.XAxisLocation = 'top';
ax2.XAxis.Exponent = 0;
ax2.Box = 'off';
ax2.Color = 'none';
ok, last request. can you plot in reverse way the bottom x axes?
clc
clear all
close all
%%
%cicle for obtain the sample data content directly from the .xlsx file
for i=1:1
table = readtable('P_T_Ni.xlsx');
temperature(:,i) = table.temperature;
pressure(:,i) = table.pressure;
Ni_grt(:,i) = table.Ni_Grt;
Ni_ol(:,i) = table.Ni_Ol;
Cr_grt(:,i) = table.Wt_Cr;
Ca_grt(:,i) = table.Wt_Ca;
end
%% calculation
%Keq
Ni_grt = Ni_grt(~isnan(Ni_grt), :);
Keq= Ni_grt ./2900;
%temperature from °C to K
temperature = temperature +273.15 ;
%Ni olivine mean
%Ni_ol = Ni_ol(~isnan(Ni_ol), :);
%mean_Ni_ol = mean(Ni_ol);
%% ignore the NaN value
temperature = temperature(~isnan(temperature), :);
pressure = pressure(~isnan(pressure), :);
Ca_grt = Ca_grt(~isnan(Ca_grt), :);
Cr_grt = Cr_grt(~isnan(Cr_grt), :);
%% Calculate the parameters (ΔH, ΔV, ΔS)
pressure_keq=[pressure Keq];
% function of T
T = @(x,pressure_Keq) (x(1) + pressure_Keq(:,1) * x(2)) ./ (x(3) - log( pressure_Keq(:,2)));
% fixed start parameters (ΔH, ΔV, ΔS)
% The chosen values must be of the same order of magnitude as the expected values
x0 = [1000, 10, 1];
% nonlinear regression with lsqcurvefit
x_fit = lsqcurvefit(T, x0, pressure_keq, temperature);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% Extract the fitted parameters
DeltaH = x_fit(1);
DeltaV = x_fit(2);
DeltaS = x_fit(3);
% the new value od temperature from Ni_grt of database in Kelvin
predicted_temperature = T(x_fit, pressure_keq);
% print the result
fprintf('Parametri adattati:\n');
Parametri adattati:
fprintf('ΔH = %.2f\n', DeltaH);
ΔH = 7737.42
fprintf('ΔV = %.2f\n', DeltaV);
ΔV = 26.79
fprintf('ΔS = %.2f\n', DeltaS);
ΔS = 2.60
%fprintf('Ni_ol = %.2f\n', mean_Ni_ol);
%% diff T(Ni-in-grt)-T(TA98)
deltaT = predicted_temperature-temperature;
mean_deltaT = mean(deltaT);
fprintf('mean_detaT = %.2f\n', mean_deltaT);
mean_detaT = 0.26
%% 1 sigma statistics
%calculate standard deviation
sigma_value=std(deltaT);
fprintf ('1σ of deltaT=%.2f\n', sigma_value);
1σ of deltaT=47.81
%% R^2
R_sq= 1-var(temperature-predicted_temperature)/var(temperature);
fprintf('R^2=%.2f\n',R_sq);
R^2=0.91
%% graphs
%transform the pressure from Kbar to Gpa
pressure= pressure/10;
%trasform the Temperature from k to °C
temperature = temperature-273.15;
%1/T in mkelvin e lnkd
%on x bottom axes
tempK = (1 ./(temperature+273));
%on x top axes
tempC = temperature;
%on y left axes
lnKeq= log(Keq);
t = tiledlayout(1,1);
ax1 = axes(t);
%Change the color of the 1st plot to white
p1 = plot(ax1,tempC,lnKeq,'w');
xlabel(ax1,'Temperature °C');
ax1.XAxis.Exponent = 0;
ax1.Box = 'off';
ylabel('lnKd')
%Change the bottom x axis direction
ax1.XDir = 'reverse';
ax2 = axes(t);
p2 = plot(ax2,tempK,lnKeq,'rx');
xlabel(ax2, '1/T (K)');
ax2.XAxisLocation = 'top';
ax2.XAxis.Exponent = 0;
ax2.Box = 'off';
ax2.Color = 'none';
Great job!! Thank you very much for the help. :)
You are welcome!
If this answer solved your problem, please consider accepting the answer.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!