How to plot x-axis in both Celsius and Kelvin?
Show older comments
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
Rik
on 4 May 2019
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.
Star Strider
on 4 May 2019
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.
Martin Privara
on 5 May 2019
Star Strider
on 5 May 2019
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)];
Martin Privara
on 5 May 2019
Star Strider
on 5 May 2019
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.
Answers (0)
Categories
Find more on Fuzzy Logic in Simulink 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!