I’m trying to make a stacked bar plot that should show how different types of rock are varying with depth. I have generated some fictive lithology series based on some real data and want to present them in a readable way. So now I have this matrix A:
A =
2.0000 65.0000
1.0000 19.5000
2.0000 0.5000
4.0000 1.5000
2.0000 58.0000
4.0000 2.0000
2.0000 22.5000
3.0000 7.0000
2.0000 14.5000
3.0000 12.5000
4.0000 2.5000
2.0000 31.5000
1.0000 20.0000
2.0000 20.0000
1.0000 15.5000
2.0000 66.0000
4.0000 0.5000
2.0000 2.5000
3.0000 8.0000
2.0000 61.0000
1.0000 17.5000
2.0000 8.0000
5.0000 19.5000
3.0000 24.5000
where the first column represents the different rock types and the second column the thickness (in meters) of each lithology layer. And now I want to plot this looking like a core-data log. So each rock type, from 1-5, should have one specific color, and the thickness of each colored bar should represent the thickness of that rock type. How can I achieve this?
Aggregate per type
You can find the total thickness of each rock type by using accumarray:
It sums up all values of the second column with equal number in the first column. So for your example data, this returns:
The difficult thing is displaying it as a single stacked bar, you can try using this workaround:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/57304
which leaves you with an empty column, which you can hide by setting the x limits:
For now, I haven’t found a better alternative, as matlab won’t stack the bars if you input vector data, it plots the bars separately in that case..
Display all values, same color per type
For plotting all the data (all layers), you can use the same approach, but now set the color data manually with colormap:
For easier adding a legend, I’ll add
Mdummy values to the original data:Now the first
Melements in the legend will correspond to Rock type 1,2,.. M: