How can I display celcius up the left hand axis and farenheit on the right hand axis pof a temperature graph?

1 view (last 30 days)
I have a temperature sensor, and at the moment I am displaying 2 graphs, on in C and the other in F. This takes up too much real estate, so I'd like to have a graph with two vertical axes, one displaying C and the other F. Is that possible?
Thanks
Rob

Answers (1)

dpb
dpb on 18 Jul 2016
Edited: dpb on 19 Jul 2016
Yeah, but you can't make 'em line up in general...try the following for the idea--
plot(-40:10:212) % just a line to get an axis over a wide range of F temperatures
hAx=gca; % save first axes handle
hAx(2)=axes('position',get(hAx,'position'),'YAxisLocation','right','color','none'); % make 2nd axes
yt=get(hAx(1),'ytick'); % get the LH axes tick locations
ylim(hAx(2),([yt(1) yt(end)]-32)/1.8) % set corresponding limits for RH axes in C
set(hAx(1),'box','off') % turn off box so no RH ticks matching LH axis
set(hAx(2),'xtick',[],'YAxisLocation','top') % will draw axis on top to bound area
ylabel(hAx(1),'F'); ylabel(hAx(2),'C')
The autoscale will put an even-numbered set of ticks labelled on RH axis but they won't quite match so there'll be a set of double ticks. On HG classic, that's only solvable by the above manipulations; don't know if you can manage to turn off the RH axis ticks alone w/ HG2 or not???
ADDENDUM
Of course, you can line up ticks if you can stand 18F as a spacing...
>> C=-40:10:100; F=1.8*C+32];
>> disp([C;F)
-40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90 100
-40 -22 -4 14 32 50 68 86 104 122 140 158 176 194 212
>>
close
plot(C)
hAx=gca;
hAx(2)=axes('position',get(hAx,'position'),'YAxisLocation','right','color','none');
ylim(hAx(2),[F(1) F(end)])
set(hAx(2),'ytick',F)
set(hAx(2),'XAxisLocation','top','xtick',[])
grid on
arrayfun(@ylabel,hAx.',['C';'F'])
Above rearranges to place F on RH axes instead...
You can match ticks by adding
set(hAx(1),'ytick',C)
or, it could be less cluttered if used C(1:2:end) and similar for RH to use every other entry...as always, "salt to suit".

Categories

Find more on 2-D and 3-D 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!