how to do a scatter plot with second x axes
조회 수: 11 (최근 30일)
이전 댓글 표시
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';
댓글 수: 0
채택된 답변
Dyuman Joshi
2023년 11월 23일
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);
% 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);
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
Dyuman Joshi
2023년 11월 23일
You are welcome!
If this answer solved your problem, please consider accepting the answer.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


