Plot two graphs on the same plot in gui and create two axis in the same plot

6 views (last 30 days)
Hi people.
I try to create in gui graph with 2 plots in the same graphs.
More than that I want to create two axis which will be corresponding to each graph. I need to use each axes after that.
What I did as following:
CV1Plot=plot(handles.axes1,cv1(:,1),cv1(:,2),'b');
hold on;
CV2Plot=plot(handles.axes1,cv2(:,1),cv2(:,2),'r');
hold off;
However CV1Plot gives me the link to the same axes. I need each graph will be corresponding to different axes. Like: CV1Plot to axes1,CVPlot2 to axes2.
handles.axes1 this is the axes in the gui.
Thank you very much.

Accepted Answer

Dimani4
Dimani4 on 18 Apr 2024
Edited: Dimani4 on 18 Apr 2024
Hi Voss,
I figured out how to do this.
I just created two axes one on another, like handles.axes1,handles.axes2. Then you need to make the background color and Xcolor and Ycolor invisible.
set(handles.axes2,'Color','none','XColor','none','YColor','none');
It's good to make the both axes the same limits, like:
set(handles.axes1,'ylim',[ymin ymax]);
set(handles.axes2,'ylim',[ymin ymax]);
Then for my case I paid attention that to reach the specific axes, axes1 or axes2 you need to put forward the axes you want to work with. So you do it like
uistack(CV1Plot.Parent, 'top');
CV1Plot this is the Parent of plot.
CV1Plot=plot(handles.axes1,cv1(:,1),cv1(:,2),'b','Tag','cv1');
This is it.
Thanks.

More Answers (1)

Voss
Voss on 3 Apr 2024
Edited: Voss on 3 Apr 2024
Do you want two lines in one axes, or one line in each of two axes?
If two lines in one axes, it looks like you already have that, if the screen shot is what you have now.
If one line in each of two axes, create a second axes in App Designer or GUIDE, whichever you are using, and plot in each axes, e.g.:
CV1Plot=plot(handles.axes1,cv1(:,1),cv1(:,2),'b');
CV2Plot=plot(handles.axes2,cv2(:,1),cv2(:,2),'r');
"CV1Plot gives me the link to the same axes"
CV1Plot is a line in handles.axes1. In your example code, CV2Plot is a line in handles.axes1. In my example code above, CV2Plot is a line in handles.axes2.
"I try to create in gui graph with 2 plots in the same graphs."
If you are having trouble with that and are using App Designer (i.e., a uifigure), you need to specify the axes to hold in a uifigure, by default. That is:
CV1Plot=plot(handles.axes1,cv1(:,1),cv1(:,2),'b');
hold(handles.axes1,'on');
CV2Plot=plot(handles.axes1,cv2(:,1),cv2(:,2),'r');
hold(handles.axes1,'off');
However, the existence of the variable handles indicates you are likely using GUIDE (i.e., a figure), in which case specifying the axes to hold wouldn't usually be necessary. On the other hand, there's no reason you can't have a handles variable in App Designer, so who knows?
If these tips didn't sufficiently help, please clarify the situation: What are you using, GUIDE or App Designer? What exactly is your objective? What is the problem you are running into in trying to achieve that objective?
  2 Comments
Dimani4
Dimani4 on 3 Apr 2024
Hi Voss,
I used guide.
The objective is to deal with each graph separately. I used some function which used specific axes, i.e. I deliver to her the handle to that specific axes which has this specific graph. I want this function only deal with that specific graph (for example 1) which corresponding to that specific axes. Each time I want this function will deal with specific graph, either 1 or 2.
Thank you very much.
Voss
Voss on 3 Apr 2024
Whatever inputs your function expects, you can pass it the right ones.
Example, calling that function (which I've named fun) in a for loop over all axes:
% 1x2 vector of axes:
ax = [handles.axes1 handles.axes2];
% 1x2 vector of lines, one in each axes:
CVPlot = [ ...
plot(ax(1),cv1(:,1),cv1(:,2),'b') ...
plot(ax(2),cv2(:,1),cv2(:,2),'r') ...
];
% calling fun for each axes:
for ii = 1:numel(ax)
fun(ax(ii)) % passing an axes to fun()
end

Sign in to comment.

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!