MATLAB Answers

How to move line plot to be above a stacked bar chart?

20 views (last 30 days)
Arijit Bhattacharyya
Arijit Bhattacharyya on 19 Sep 2021 at 22:08
Commented: Mathieu NOE on 21 Sep 2021 at 11:15
Hello, here is my current code:
indexValues = [repelem(6, 33), repelem(5,6), repelem(4,6), repelem(3,6), repelem(2,6), repelem(1,74); ...
repelem(1, 131); repelem(4,64), repelem(3,6), repelem(2,6), repelem(1,55); repelem(1, 131); repelem(1, 131); ...
repelem(2, 131)];
lineValues = [repelem(-42.8043, 33), repelem(-48.8249, 6), repelem(-54.8455, 6), repelem(-60.8661, 6), ...
repelem(-66.8867, 6), repelem(-72.9073, 7), repelem(-78.9279, 6), repelem(-84.9485, 6), repelem(-90.9691, 55)];
figure
colororder({'k','k'})
yyaxis left
bar(-120:10, indexValues, 1, 'stacked', 'EdgeColor', 'none')
ylabel('Global Index')
colororder('default')
hold on
grid on
grid minor
yyaxis right
ylabel('Temperature')
plot(-120:10, lineValues, '- . m', 'DisplayName', 'Temperature', 'LineWidth',5)
xlabel('Relative Distance (m)')
xlim([-125 15])
Does anyone know if there is a way to physically move the pink line plot so that it is above the stacked bar chart? I want the curve to ideally be above, so that the multiple colours do not overlap with each other and confuse the user. I have seen that it is possible to move the curve above with the "pan" feature in the figure window, but is there a way to code this in MATLAB to move the pink line automatically? Thank you!

Accepted Answer

Mathieu NOE
Mathieu NOE on 20 Sep 2021 at 9:29
hello
this would be my 2 cents suggestion using ylim :
code :
indexValues = [repelem(6, 33), repelem(5,6), repelem(4,6), repelem(3,6), repelem(2,6), repelem(1,74); ...
repelem(1, 131); repelem(4,64), repelem(3,6), repelem(2,6), repelem(1,55); repelem(1, 131); repelem(1, 131); ...
repelem(2, 131)];
lineValues = [repelem(-42.8043, 33), repelem(-48.8249, 6), repelem(-54.8455, 6), repelem(-60.8661, 6), ...
repelem(-66.8867, 6), repelem(-72.9073, 7), repelem(-78.9279, 6), repelem(-84.9485, 6), repelem(-90.9691, 55)];
figure
colororder({'k','k'})
yyaxis left
bar(-120:10, indexValues, 1, 'stacked', 'EdgeColor', 'none')
ylim([0 max(sum(indexValues,1))+5]); % here
ylabel('Global Index')
colororder('default')
hold on
grid on
grid minor
yyaxis right
ylabel('Temperature')
plot(-120:10, lineValues , '- . m', 'DisplayName', 'Temperature', 'LineWidth',5)
xlabel('Relative Distance (m)')
xlim([-125 15])
ylim([min(lineValues)-40 max(lineValues)+10]); % and here

More Answers (0)

Community Treasure Hunt

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

Start Hunting!