how to plot two x axes and one y axis?

3 views (last 30 days)
Ziga Debevec
Ziga Debevec on 12 Oct 2016
Edited: dpb on 13 Oct 2016
Hi guys, I know this might already been answered, but anyway: I am trying to plot graph with two x and one y axis. Basicaly I have 4 vectors: x1,y1,x2,y2. I would like to plot x1,y1 and x2,y2 and I would like to have y1 and y2 have the same y axis.
  1. how to specify color and markers for each pair?
  2. how to make ticks on both x axis the same as the values in x1 or x2 are?
  3. how specify legend of such plot?
  4. how to specify legend anx axis font style and size?
  5. each element of vectors y1 and y2 has its own standard deviation and I would like to show this in graph.
It's show how I would like to do in the image attached, except I would like to have 2 x-axis. thanks for help in advance!!

Answers (1)

dpb
dpb on 12 Oct 2016
Edited: dpb on 12 Oct 2016
Follow the example in the "Using Multiple X- and Y-Axes" documentation...
hE(1) = errorbar(x1,y1,e1,'r'); % first errorbar, make it red
hAx(1) = gca; % save the axes handle
set(hAx(1),'XColor','r','YColor','r') % make the axes match for clarity
hAx(2) = axes('Position',get(hAx(1),'Position'),...
'XAxisLocation','top',...
'Color','none',...
'XColor','b','YColor','b');
hE(2) = errorbar(x2,y2,e2,'b','Parent',ax2);
The rest is simply using the linestyle and other properties; the one question about the tick marks specifically is just
set(hAx(1),'XTick',x1)
set(hAx(2),'XTick',x2)
Note that if x1 and x2 aren't the same, you'll have competing grid lines if you turn the grid lines on.
With recent versions you can use the dot syntax to address properties rather than set; I'm just old-fashioned and don't have that recent a release installed...
  2 Comments
Ziga Debevec
Ziga Debevec on 13 Oct 2016
Hi, thanks for your answer, it has a minor mistake if I am correct:
hE(2) = errorbar(x2,y2,e2,'b','Parent',hAx(2));
When I run code with correction it does not work.
dpb
dpb on 13 Oct 2016
Edited: dpb on 13 Oct 2016
Good catch--I pasted the test code from command window and then decided to change variable names for the handles and missed one instance...I'll fixup the Answer to match.
For what definition of "does not work?". I created an example plot here with no problems but can't see your terminal from here. Post code and error or resulting figure with what isn't what you think should be...

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!