How to plot x-axis in both Celsius and Kelvin?

Do you know a simple way how to plot y where axis at bottom is in Kelvin and axis at top is on Celsius?
y = [1 2 3 4 5 6 7 8 9 10]; % variable dependent on temperature
t = [10 20 30 40 50 60 70 80 90 100]; % temperature in degC
T = t + 273.15; % temperature in Kelvin
figure(1)
plot(t,y)
hold on
plot(T,y) % doesnt work so well
xlabel('Temperature, K')
xlabel('Temperature, degC')
I found this link but the example is a bit too compicated..
Thank you for help!

6 Comments

Unfortunately, that is the only 'real' way. You have an alternative: use multi-line tick labels, but I don't think that is much less complicated.
I agree with Rik — it's definitely not simple.
If you want to experiment with that code, this version (adapted to your data) works:
y = [1 2 3 4 5 6 7 8 9 10]; % variable dependent on temperature
t = [10 20 30 40 50 60 70 80 90 100]; % temperature in degC
T = t + 273.15; % temperature in Kelvin
figure(1)
ax1 = gca; % current axes
ax1_pos = ax1.Position; % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'Color','none');
line(t,y, 'Parent',ax1, 'Color','b')
line(T,y, 'Parent',ax2, 'Color','r')
xlabel(ax2,'Temperature, K')
xlabel(ax1,'Temperature, degC')
fpos = get(gcf, 'Position');
set(gcf, 'Position',fpos+[0 -150 0 150]) % Create Room For The Top X-Axis Label
The lines are not collinear because the top and bottom x-ticks are rounded to integers, so the Kelvin (red) line plots from 280 to 380, rather than 283.15 to 373.15.
Thank you Star. I've tried to use your code but I get two lines instead of one. Anyways I don't think I will be able to use it in my code. I have much more coplicated plots. Matlab has a lot to learn from Excel with respect to miltiple x-axis :)
figure1.jpg
My pleasure.
I mentioned that and what I believe is the reason for the two lines not being collinear.
They do line up (are collinear) if you add this line somewhere near the end of that code:
ax2.XLim = [min(T) max(T)];
It works now! Let's see how it behaves with more lines..
Great!
The XLim call must be after the last line call. The arguments must be the minimum and maximum of all the independent variable vectors in all your line calls.

Sign in to comment.

Answers (0)

Categories

Asked:

on 4 May 2019

Commented:

on 5 May 2019

Community Treasure Hunt

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

Start Hunting!