How to plot Histogram/bar graph for two data sets!!

I have data set having two variables say EQ and MASS. Their length is same. I can plot the scatter plot as shown.
What i want to do is to plot a histogram between the EQ and MASS showing that how much MASS is present at which EQ. I mean summing the scatter points at each EQ and showing it as a bar graph or histogram.
Can anyone guide me..I have tried but as i see histogram can be plotted for only one variable and no of bins.
Also I have tried combining them as one variable and plotting but still now working....

 Respuesta aceptada

I think there are a few ways to interpret your question.
Do you mean you want to show two bar charts describing the histogram of the two variables? This will be awkard to do on one x-axis because the scales are very different:
load sat
figure
histogram(EQ)
hold on
histogram(MASS)
But it would be easy to do on two axes with nexttile and tiledlayout:
figure
tiledlayout(2,1)
nexttile;histogram(EQ)
nexttile;histogram(MASS)
But I think what actually wanted is a 2-D (bivariate) histogram, showing a 3-D bar? histogram2 is good for this...
figure
histogram2(EQ,MASS)
xlabel('EQ')
ylabel('MASS')
zlabel('Count')
If I did this, I might consider trying to flip the x and y axis directions so that the tall bars are in the back corner (because it's hard to see what they might obscure):
figure
histogram2(EQ,MASS)
set(gca,'XDir','reverse','YDir','reverse')
xlabel('EQ')
ylabel('MASS')
zlabel('Count')
Although I generally think these are better with a image-like display style:
histogram2(EQ,MASS,'DisplayStyle','tile')
xlabel('EQ')
ylabel('MASS')
c=colorbar;
c.Label.String='Count';
Or maybe you just wanted to capture histograms alongside the scatter above? You can do this manually using histogram, scatter, and tiledlayout...normally I'd recommend the function scatterhistogram for this but it sadly doesn't look right here.
figure
t=tiledlayout(1,1,'TileSpacing','tight');
scatax=nexttile;
scatter(EQ,MASS)
ax=axes(t);
ax.Layout.Tile='north';
histogram(ax,EQ)
ax.XLim = scatax.XLim;
ax.Visible='off';
ax=axes(t);
ax.Layout.Tile='east';
histogram(ax,MASS)
view([90 90])
ax.XDir='reverse';
ax.XLim = scatax.YLim;
ax.Visible='off';

5 comentarios

Usama Bin Khalid
Usama Bin Khalid el 27 de Nov. de 2021
Editada: Usama Bin Khalid el 27 de Nov. de 2021
Hello., Appreciate your answer But my question was to plot the accumulated mass at each corresponding EQ in terms of a bar
For example if you see only 1 color in this graph(forget other colors becasue these represnt different instances and once i plot one instance i can hold on and plot others).. it shows the sum of all mass at that particular EQ (that particular EQ can exist more than once). So is there any way I can do this with histogram or bar plot or anyother way: showing the corresponding accumulated mass at each EQ. i did it in scatter but what i want is like i show here... sum all the mass at each EQ and show as a bar or histogram...Thanks
Dave B
Dave B el 27 de Nov. de 2021
Editada: Dave B el 27 de Nov. de 2021
I'm sure the answer is yes, but it's still pretty hard for me to understand what you're looking for. To be clear, a histogram represents a frequency distribution - it's about counting occurrences of data at various locations.
Here are some more histograms represented as bar charts:
load sat
[cnts,EQedges,MASSedges]=histcounts2(EQ,MASS);
% this shows counts of various MASS values for a given EQ
figure
x = MASSedges(1:end-1)+diff(MASSedges);
bar(x,cnts(1,:))
% This shows a bar for two ranges of EQ
figure
EQBinLabels=string([EQedges(1:end-1);EQedges(2:end)]').join(' - ');
bar(x,cnts(1:2,:))
leg=legend(EQBinLabels(1:2));
title(leg,'EQ Range')
% this shows a bar for each of the ranges in EQ
% (I've limited the x axis so that the distribution is visible)
figure
bar(x,cnts)
leg=legend(EQBinLabels(1:2));
title(leg,'EQ Range')
xlim([0 1e-8])
leg=legend(EQBinLabels,'Location','eastoutside');
title(leg,'EQ Range')
% The 'accumulated' version of the above, accumulating over mass (so that
% each EQ Range accumulates over each mass)
figure
bar(x,cumsum(cnts,2))
leg=legend(EQBinLabels(1:2));
title(leg,'EQ Range')
xlim([0 1e-8])
leg=legend(EQBinLabels,'Location','eastoutside');
If instead you're looking for the average mass for each range of EQ, this isn't a histogram. It's quite easy to compute these using groupsummary:
figure
numbins=10;
[mu,bins]=groupsummary(MASS,EQ,numbins,@mean);
bar(bins,mu)
xlabel('EQ Range')
ylabel('Average MASS')
If you want the sum of all MASS for each EQ range, just use sum instead of mean in the above example.
load sat
numbins=10;
[mu,bins]=groupsummary(MASS, EQ, numbins, @sum);
bar(bins,mu)
xlabel('EQ Range')
ylabel('sum(MASS)')
Usama Bin Khalid
Usama Bin Khalid el 27 de Nov. de 2021
Editada: Usama Bin Khalid el 27 de Nov. de 2021
Hello dave thank you for your answer...I more or less wanted something you showed in the last but with sum.. I will look into group summary further. Just one question lets say i have another data set having EQ_A and MASS_A..can i plot it side by side with group summary of EQ and MASS in a different color to get an idea about the comparison between two instances... Thanks
figure
numbins=10;
[mu,bins]=groupsummary(MASS,EQ,numbins,@sum);
bar(bins,mu)
xlabel('EQ Range')
ylabel('Average MASS')
It's pretty easy to plot two datasets with bar, but to combine this with groupsummary it will be important to give it the same exact bins. So instead of specifying the number of bins, you'll want to provide groupsummary with some specific bins:
load sat
MASS2=MASS+(rand(size(MASS))-.5)*1e-9;
EQ2=EQ+rand(size(EQ))-.5;
[msum,~]=groupsummary(MASS,EQ,10,@sum);
[msum2,bx]=groupsummary(MASS2,EQ2,10,@sum);
bar(bx,[msum msum2])
xlabel('EQ Range')
ylabel('Sum(MASS)')
legend(["MASS1","MASS2"])

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2020b

Preguntada:

el 26 de Nov. de 2021

Comentada:

el 27 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by