making a single stacked bar with colors from each stack depending on the distribution frequency - matlab

I have a vector of the size 10000x1, which I reduced to 4400x1. I want to make a stacked bar with the single entries of this vector. Each segment of the stacked bar should be colored depending on the distribution frequency. For example: if a value is represented 10 times, it should almost be black. if its only 1, it should be light grey.
1st problem: When I use a small vector, I get a stacked bar. If I use the one I need to I get multiple bars instead of one stacked bar.
2nd problem: How can I make the bar use a gray scale to color them?
I tried to transpose my vector to 1x4400 which didn't change anything.
I used bar(data,'stacked') which works for the small vector (at least I get a stacked bar. To get a single bar I used:
test = [1 2 3 4 5 1 2 3];
bar([test; nan(size(test))],'stacked');
size of data : 4400x1
bar([data; nan(size(data))],'stacked');

When I enter your first example, with an 1x8 matrix:
test = [1 2 3 4 5 1 2 3];
bar([test; nan(size(test))],'stacked');
I see the same plot you see. Then I transpose test to make it an 8x1 matrix:
test = test.';
bar([test; nan(size(test))],'stacked');
and I see something similar to your second plot:
Note how the NaNs cause the right half of the x-axis to have no bars.
Thus, to get stacked bars you need to have a row vector. However, your data is a column vector, a 4400x1 matrix. data = data.' will change that.
Nonetheless, it is likely that MATLAB is not able to make a stacked bar plot with 4400 elements. I'm trying this out on MATLAB Online, and I've been waiting for several minutes and still don't have a plot. In any case, there is no way you would become any wiser with such a plot. Don't do it!
Regarding your second question: you can change the plot color order to gray:
set(gca,'colororder',flipud(gray(256)))
However, the stacked bars are not colored according to their value, they just use the colors of the list in turn.
A better solution would be to plot your data as an image:
test = [1 2 3 4 5 1 2 3];
imagesc(test.')
colormap(flipud(gray(255)))
set(gca,'xlim',[0,2],'xtick',1)
[I just realized you might want to straighten the y-axis, imagesc inverts it. Simply: set(gca,'ydir','normal').]

Related

Matlab draw 3D stacked figure use bar3() for multiple variables [duplicate]

I have a matrix, A, that contains 50 rows and 4 columns, and the entries are filled with integers. My interest is to construct a stacked 3D bar plot from this data. However, using bar3(A,'stacked') creates a row of 50 bars, whereas I want the bars to be plotted at the coordinates of a grid of size 5 (vertical) x 10 (horizontal). So the first bar in the row would be at location (1,1), second bar at (1,2), 11th bar at (2,1) and so on until the 50th bar which would be at (5,10). I can't seem to find a way to do this in Matlab, is this possible at all?
Thank you in advance!
I agree with #cris, there are better ways to represent your data. However, something like this would work if you still want to do use a 3D bar plot:
figure
hold on
for i = 1:5
Ai = A(10*(i-1)+1:10*i,:);
h = bar3(1:10,Ai,'stacked');
for ih = 1 :length(h)
x = get(h(ih), 'Xdata');
set(h(ih), 'Xdata', x+i-1);
end
end
view(3)

Create a plot with a 2D colourmap depending on two variables

I want to display an image (e.g.imshow) and use a colormap to represent the values of my data points.
However, colormap only gives the option to be dependent on a single variable, but I want a "2D colormap" which depends on two variables.
For example I have a simple image 2x2 pixels:
img = [
1 1 5 6;
1 2 8 7;
2 1 4 3;
2 2 15 3]
Here the first two values of each row are the coordinates, the other two are the values describing the pixel (call them x and y).
When displaying the image I want to use a 2D colormap. For example something like this, which picks a colour depending on both variables (x and y):
Is there an option in MATLAB do to this, possibly in one of the extra toolboxes?
If not can this be done manually? I was thinking by overlaying a grey scale image given from the first value over a colormap image given by the second value a similar effect could be achieved.
In your 2D colormap you are actually using the HSV color space.
Basically, your x axis is Hue, and Y axis is Saturation. You can convert any value into this space if its properly scaled. If you make sure that you scale your 3rd and 4rd column in the [0-1] interval you can easily do
colorRGB=hsv2rgb([val3,val4,0.5]);
If you perform this operation for each pixel, you'll get the image you want.
I gave a extended explanation of how HSV works here

Bar graph x-axis titles do not work with many bars

I'm using MATLAB and I want a bar plot.
I plotted the bars, and then give titles to the bars.
If I only using 10 bars it works (see pic1).
But often I need more, mostly 32 bars (see pic2).
In this case the titles are not under the bars. I found out that MATLAB don't overlap the titles. The max value is 15 bars.
Does anyone knows how I can get MATLAB to overlap the titles?
Here is my code for plotting the bars:
y = absSlopes(1, 1:size(absSlopes,2));
hb = bar(y);
set(gca,'XTickLabel',titles);
rotateXLabels(gca, 45);
ylabel('Anzahl');
absSlopes is a matrix and I use the complete first line.
You may want to try XTick in addition to XTickLabel. At least for regular plots you can enforce specific spacings through this setting.
You can do this by an additional setup-command:
set(gca,'XTickLabel',titles);
set(gca,'XTick',absSlopes(1,:));
Of course, you can set both properties in the same line, but I like to keep it simple in case you want to deactiviate/comment one or the other setting later on.

how expand x-axis in bar plot in MATLAB

I have a bar plot with xlim([1 5]) as time. Each time contains 5 different grouped data. They are very compacted and plot understanding is not clear. I am going to expand each xlim unit to every 5 grouped data be more readable in each time. How is it possible?
Also, How can i make more distance between each 5 grouped data in each time? I applied bar(data,10,'hist'); but my 5 grouped data are still compacted in each time.
You could try bar(X,Y,width), with values of width lower than the default, which is 0.8. However, that only makes the bars narrower, not closer to the each other within its group.
To make the bar groups farther apart, you could insert NaN values between them. For example:
bar(1:.5:3,[ rand(1,7); repmat(NaN,1,7); rand(1,7); repmat(NaN,1,7); rand(1,7)] ,.8)
set(gca,'xtick',1:3) % remove unwanted ticks
See figure:

Custom X axis in core plot

I have this X axis in my sample project on Core Plot and I wonder how I can customise it a little bit. As you can see it currently has only 3 values on the X axis:
But the values of the two plots I have (the black and grey ones) go far beyond the number of points in the X axis (29 against 3 points). Because of the fact that I have 3 points, only 3 values for each plot are shown.
I would like to keep displaying the remaining 3 points on the axis but accommodate all my 29 events for my plots (they could be displayed in the middle of 1 and 2 point).
How can I do this?
Increase the length of the xRange of the plot space. The value needed depends on the plot data.