How can I select imagesc boundaries on the y-axis?

7 views (last 30 days)
Hello,
I would like to plot two imagesc bars displaying temperature over time to compair two years with each other. Since I want to plot some other graph on top of them, I need the "Temperature Bar" to be at a certain location on the y-axis in my plot:
% made up example data
W18_T = [1:10]';
W18_Y = [0 -0.5 -0.75 -0.3 0.1 0.2 0.3 0.4 0.4 0.3]';
W19_T = [11:20]'-10;
W19_Y = [1 0.5 0.25 0 -0.1 -0.2 -0.1 0.2 0.4 0.3]';
SP18_Y = [100 105 106 103 101 102 103 104 104 103]'; SP18_T = [1:10]';
SP19_Y = [120 125 126 123 121 122 123 124 124 123]'; SP19_T = [11:20]'-10;
% identify plot area on the y-axis
schnittpunkt = min([SP18_Y; SP19_Y])+((max([SP18_Y; SP19_Y])-min([SP18_Y; SP19_Y]))/2)
% plot
figure; hold on;
imagesc(W18_T',[min(SP18_Y) schnittpunkt],W18_Y');
imagesc(W19_T',[schnittpunkt max(SP19_Y)],W19_Y');
plot(SP18_T,SP18_Y,'LineWidth',2,'Color','k')
plot(SP19_T,SP19_Y,'LineWidth',2,'Color','k')
The problem is that even if I set " [min(SP18_Y) schnittpunkt] " as the "Bouderies" for my imagesc plot, it makes the Display much wider then I want.
I want the first bar to be plottet in the y-axis range of [min(SP18_Y) schnittpunkt] --> [100 113], but imagesc plots it at [87 126] ...
and the secound bar to be plottet in the y-axis range of [schnittpunkt max(SP19_Y)] --> [113 126], but imagesc plots it at [100 139]
How can I correct for it without "guessing" how wide to make the areas? I need to do this for a lot of different Variables at lot of different ranges, so trying by hand is not an option.
Thank you very much!

Accepted Answer

Mathieu NOE
Mathieu NOE on 28 Sep 2022
hello Kathy
you needed indeed :
  • to reverse the Y direction for imagesc
  • to create the color matrix ( 3rd argument of imagesc) with as many lines as the difference of y range : like 100 to 133 y range => 13 lines for C color matrix. therefore use of repmat
beside that no major change in your code (removed all unnecessary transpose operators here and there)
% made up example data
W18_T = (1:10);
W18_Y = [0 -0.5 -0.75 -0.3 0.1 0.2 0.3 0.4 0.4 0.3];
W19_T = (11:20)-10;
W19_Y = [1 0.5 0.25 0 -0.1 -0.2 -0.1 0.2 0.4 0.3];
SP18_Y = [100 105 106 103 101 102 103 104 104 103]; SP18_T = [1:10];
SP19_Y = [120 125 126 123 121 122 123 124 124 123]; SP19_T = [11:20]-10;
% identify plot area on the y-axis
schnittpunkt = min([SP18_Y SP19_Y])+((max([SP18_Y SP19_Y])-min([SP18_Y SP19_Y]))/2);
% plot
figure; hold on;
y = [min(SP18_Y) schnittpunkt];
C = repmat(W18_Y,round(y(2)-y(1)),1);
imagesc(W18_T,y,C); % y range should be 100 - 113
y = [schnittpunkt max(SP19_Y)];
C = repmat(W19_Y,round(y(2)-y(1)),1);
imagesc(W19_T,[schnittpunkt max(SP19_Y)],C); % y range should be 113 - 126
set(gca,'YDir','normal'); % important tip here ! https://fr.mathworks.com/matlabcentral/answers/94170-how-can-i-reverse-the-y-axis-when-i-use-the-image-or-imagesc-function-to-display-an-image-in-matlab
plot(SP18_T,SP18_Y,'LineWidth',2,'Color','k')
plot(SP19_T,SP19_Y,'LineWidth',2,'Color','k')

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!