how expand x-axis in bar plot in MATLAB - 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:

Related

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

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').]

How to plot percentage on x-axis?

I managed to read a large excel sheet in MATLAB, sorted the data and plotted the graph of the wanted data. The problem that I am experiencing is that I am trying to make the values of x-axis as a percentage. I have a variable of around 1000 integers which may increase as I add data. I have assigned a variable to check the length of variable so that I do not have to change it manually. The problem is that I need to assign the x-axis in terms of 10%, 20%... 100%. How can I do that without reducing data for the y-axis?
The following is the code for the x-axis:
A = round(length(data),2,'significant');
d = 0.1*A
e = 0:d:A
f = e/A*100
set(gca,'xticklabel', f)
The x-axis is showing up till 90%. The length of data is 16990, therefor A results in 17000, d results to be 1700, value of variable f is from 0 to 100 but the plot is still showing to 90%.
The image below shows the graph with the data, without adding limits for the ticks:
On the other hand, when I have changed 'ticklabels', it only reduced to the first points of the graph. Whilst when I have converted ticklabels to 'ticks', the graph is shown in full but the ticks are as shown below:
What I am trying to do is convert the 18000 into 100%.
set(gca,'xticklabel', f)
sets the tick labels. It doesn't change anything else about the graph. Thus, it assigns different labels to existing tick marks. It will confuse the readers of the graph!
What you want to do is change the 'xlim' property, which determines the portion of the x-axis that you can see:
set(gca,'xlim',f([1,end]))
You might also want to set the ticks themselves:
set(gca,'xtick', f)

set displayed scale of axis in plot?

In the model I'm working on, ticks are months, and line plots are updated once every year, i.e. every 12 ticks. As ticks accumulate, the scale within the graph changes, of course, and the number in the lower right corner of the plot increases. This number reflects the number updates that have been made to the plot--i.e. the number of years. The number is the number of years that can be represented on the plot given its current scale.
Is there a way to change this number, without changing the rest of the plot, and without changing the idea that ticks are months? It would be convenient for this maximum x-axis value to show the number of months--i.e. ticks that could be displayed, even though the plot is only updated every 12 ticks.
Use the set-plot-x-range primitive. http://ccl.northwestern.edu/netlogo/docs/dictionary.html#set-plot-x-range

How to plot different data in parallel (in continuation of the previous one)

I have some energy 24 hour consumption data of many days.
Plotting a specific day gives me vertical axis of consumption and horizontal axis of time.
I would like to plot for lets say 1 year.
If I use "hold on/off" command, it plots all days together on top of each other.
How can i plot in a way that for the second day, the plots goes to the continue of the first plot (horizontal axis extends automatically)? So, when I have the complete plot, it shows 365 days of energy consumption based on hour. It's like the horizontal axis is repeating while the vertical axis is updating. I'm talking about MATLAB.
You can still use hold on and plot each day separately (if I understand your question properly, this is what you want, separate plotting). Simply make sure your x-axis values are correct. So e.g. if you have one measurement value per hour, the plot day 1:
plot(1:24,valDay1,'k-')
then for day 2:
plot(25:48,valDay2,'r-')
etc. This will line things up correctly. Also, consider using a datetime as x axis values
So, I found my solution which is very simple. I don't know how it didn't occur earlier.
I just had to use ";" and that's it.
Like this:
DAY=[day1;day2;day3]
plot(DAY)

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.