Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting histogram side by side in Matlab

I have two vectors, c and d, whose histogram I need to plot side by side in the same figure in matlab. when i do hist(c); hold on; hist(d) the scale changes and I cant see the histogram of c vector. Where am i going wrong? Any help will be appreciated.

like image 743
Maddy Avatar asked Dec 31 '25 13:12

Maddy


2 Answers

If you want the two to be in the same figure, you could try adjusting the X and Y limits to suit your needs (try help xlim and help ylim). However plotting them in the same figure might not always suit your needs, as a particular plot has to of course maintain a certain limit for X and Y.

If displaying them side by side in different figures would suffice however, you could consider using subplot():

>> A=[1 1 1 2 2];
>> B=[1 2 2 2 2];
>> figure(1);
>> hold on;
>> subplot(1,2,1);
>> hist(A);
>> subplot(1,2,2);
>> hist(B);

Resultant figure:

Notice how the different axis limits are maintained.

like image 91
Roney Michael Avatar answered Jan 02 '26 15:01

Roney Michael


You can use axis([xmin xmax ymin ymax]) to control the x and y axis and select a range that will display both histograms. Depending on what you want your plot to look like, you may also want to try using nelements = hist(___) to get the number of elements in each bin and then plot them using bar(x,nelements) to control the location of each bar.

like image 22
Molly Avatar answered Jan 02 '26 15:01

Molly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!