plot graph and bar on same figure with 2 x-asis and 2 y-axis with different scales
2 views (last 30 days)
Show older comments
I need to plot this 2 graphs in the same figure, one of them using bottom-x Vs. left-y, and the other using top-x Vs. right-y but I cant figure it out, nothing worked for me.
x1=[0.0153 0.2479 0.4366 0.6620 0.8544 0.9197 0.9625 0.9961 1.0000];
y1=[1000 900 800 700 600 500 400 300 200];
p1=plot(x1,y1);
x2=[1000 900 800 700 600 500 400 300 200];
y2=[0.0153 0.2326 0.1888 0.2253 0.1924 0.0653 0.0429 0.0336 0.0039];
b1=bar(x2,y2,1);
I tried yyaxis, plotyy, creating and modifying my own axis, etc...nothing worked.
0 Comments
Answers (1)
Kostas
on 16 Mar 2019
Edited: Kostas
on 16 Mar 2019
You need to create a second axis to plot your line.
See this example
Try something like the following
% Create the data
x1=[0.0153 0.2479 0.4366 0.6620 0.8544 0.9197 0.9625 0.9961 1.0000];
y1=[1000 900 800 700 600 500 400 300 200];
x2=[1000 900 800 700 600 500 400 300 200];
y2=[0.0153 0.2326 0.1888 0.2253 0.1924 0.0653 0.0429 0.0336 0.0039];
figure;
% make the bar plot
b1=bar(x2,y2,1);
ax1 = gca; % get current axes properties
ax1.XColor = 'b'; % change the X axis color to blue
ax1.YColor = 'b';
ax1_pos = ax1.Position; % position of first axes
% Create the second axis to plot the line
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
line(ax2,x1,y1,'Color','k')
See Also
Categories
Find more on Two y-axis 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!