how to do a scatter plot with second x axes
Show older comments
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
More Answers (0)
Categories
Find more on Discrete Data 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!

