This question already has answers here:
Getting a second row of xlabels in matlab graph?
(1 answer)
add data label to a grouped bar chart in matlab
(1 answer)
Closed 7 months ago.
I have two types of plots.
In fist case I need to label individual bar graphs in the grouped bar plot. Here is the example
a=4.54,88.63,27.27,77.27,54.54;31.81,61.36,38.63,68.18,54.54;54.54,61.36,59.09,54.54,50;68.18,27.27,56.81,34.09,50;90.90,11.36,68.18,15.90,40.90];
b=0.40,0.55,0.70,0.85,1;1.39,1.54,1.69,1.84,1.99;2.340,2.49,2.64,2.79,2.94;3.36,3.51,3.66,3.81,3.96;4.29,4.44,4.59,4.74,4.89];
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i))
end
figure,
hold on
for i=1:3
plot(b(i,:),a(:,i))
end
For the bar plot, I like to lable on the horizontal as shown in figure, the numbers are stored in an other matrix, say b
2) Similarly, I also want to XTickLables using values in b for the line plot.
you have first to define xticks and XTickLables
for example you can write:
a=[....];
b=[....];
c=[0.5,1,1.5,2,3];
D = {'0.5','1','1.5','2','3'};
figure,
hold on
for i=1:5
bar(b(i,:),a(:,i));
xticks(i) = c(i); %% first you define the ticks
XTickLables{i} = d(i); %% second you define the labels
end
for the exact same plot like the one you showed, I think you need two nested for loops.
more on that here: https://de.mathworks.com/help/matlab/ref/xticklabels.html
Related
This question already has answers here:
Adding to a legend after each iteration
(5 answers)
Matlab dynamic legend / legend "hold on" like behavior
(1 answer)
Assemble Legend for Many Curves
(1 answer)
Closed 2 years ago.
I have data that I am plotting using a for loop. I dont know how to add a label for each graph to form a legend. This data is a lot and the names will have to be added in a looped manner. Please Advise.
Here is the code:
% Data for examples sake
q=[1;2;3;4;5;6;7;8;9;10];
a=[1;2;3;4;5;6;7;8;9;10];
b=a*2;
c=a*3;
d=a*4;
v_matrix=[a,b,c,d];
labels = ["a","b","c","d"];
%Code
[m,n]=size(v_matrix);
figure;
for i=1:1:n;
ylabel('Velocity (m/s)');
xlabel('Flow Rate (m^3/h)');
plot(q,v_matrix(:,i));
hold on;
end
The labels are generated in the same loop as the loop that generates the v_matrix.
This is what is generated:
This is what I want to be generated with the loop(legend was manually added with the "insert legend" button.
One way to do this would be to give the label of each line in the plot command itself using the 'DisplayName' property and then calling the legend:
figure
hold on
for i = 1:10
% char(97) = 'a', char(98) = 'b', ...
plot(1:10,(1:10).*i,'-','DisplayName',char(96 + i));
end
legend;
This question already has answers here:
MATLAB - How to zoom subplots together?
(3 answers)
Closed 7 years ago.
I created a figure with 8 subplots of timeseries objects, because I wanted to have an overview of the data.
Is there an option which gives me the following possibility:
If I zoom into one subplot (for example: just the range from 5 to 10 on the x-axis is now visible), than all other plots will automatically zoom in (such that the range from 5 to 10 on the x-axis is now just visible for all other subplots) too??
For linking both the x and y axes, you should use the command linkaxes. It takes as input a vector of handles to axis objects you want to link together, and additional options if desired.
Example:
for k = 1:4
ah(k) = subplot(2,2,k);
plot(1:10, rand(1,10));
end
linkaxes(ah);
After this, if you apply e.g. zooming on any of the subfigures, the x and y limits of the other axes will change as well.
If you want to link only, say, the x axis, use instead:
linkaxes(ah,'x');
I am trying to create a bar chart where my x axis shows only one data point, year of 2006, but for 3 different data related to 2006. I am doing the following, but I receive the
error of "X must be same length as Y."
Here is my code and I highly appreciate your help:
X=[2006] %year
% Create data for childhood disease cases
measles = [38556];
mumps = [20178];
chickenPox = [37140];
% Create a vertical bar chart using the bar function
figure;
bar(X, [measles' mumps' chickenPox'],'group');
% Set the axis limits
%axis([0 13 0 40000]);
% Add title and axis labels
title('Childhood diseases by month');
xlabel('Month');
ylabel('Cases (in thousands)');
% Add a legend
legend('Measles', 'Mumps', 'Chicken pox');
You are getting this error because X is only one element and you are using three different Y values.
I think this problem requires you to take a step back a bit. Why are you plotting all three against the same X value? If this worked as you specified, all three would be on top of each other.
Would it not be more informative to the viewer if the X-axis labeled each bar as measles, mumps or chickenPox?
Try:
bar([measles' mumps' chickenPox']);
set(gca,'XTickLabel',{'Measles','Mumps','Chicken Pox'});
Edit:
I see what you're after. You're almost correct. The problem is that since you only have one column of data, matlab assumes you DON'T want to create a group but just plot those 3 values against unique X values. So we need to trick it slightly by creating another data point.
% //Create two columns, one of them zeros
data = [[measles;mumps;chickenPox],[0;0;0]];
bar([X,X+1],data'); % //The 2007 entry will not be visible
This will create a group at 2006 and the 2007 entry will be zeros so you won't see it. You can then set your axes and such to be the same as the other charts.
I need to make a bar graph in matlab with 3 bars. The values for the three bars is stored in x. Now I want a to make a legend: Each bar should be labeled with a letter e.g. 'A','B','C'.
x = rand(1,3); bar(x); legend('A','B','C');
Did not work.
Then I tried this example: http://www.mathworks.com/matlabcentral/fileexchange/35271-matlab-plot-gallery-vertical-bar-plot/content/html/Vertical_Bar_Plot.html
But as soon as you reduce the the number of entries in each category to 1, you'll get an error message, but I think the message is wrong =/ it works with any number but just not with 1...
So is there a simple solution to that problem?
What you want is a "three series plot" - unfortunately, as soon as you make it 1x3, Matlab thinks it's just a single series. We have to trick it into thinking you have three series. Here's how you do that:
x = rand(1,3);
x2 = [x; x*0]; % add a second row of all zeros
figure
bar(x2); % now we have a plot with an ugly second category
xlim([0.5 1.5]); % trick that hides second category
legend('a','b','c'); % add the legend
set(gca, 'XTickLabel', ''); % hide the '1' on the X axis (or make it whatever you want)
title 'Bar plot with legend - demo'
Result is then something like this:
I believe this is exactly what you were asking for.
Consider the following code:
A=0:0.1:4;
for i=1:50,
B(:,i) = sin(A+i*0.01); % each column of B contains "shifted" sin
end
bar3(B); % plot such as each "shifted" sin will have different color
rr=1:size(B,1); % numbers to label different "shifted" sin in legend
l=strtrim(cellstr(num2str(rr'))') % converting numerical labels to strings accepted by "label"
legend(l);
How to display legend entries only for selected profiles e.g. 1st, 25th and last?
Question is similar to:
How to show legend for only a specific subset of curves in the plotting?
But I do not know how to get the figure handles for bar3 as suggested in the answer. Alternatively: does more elegant solution exist?
You get the handles with:
h = bar3(B); % plot such as each "shifted" sin will have different color
Then you can display the legend for selected profiles only:
legend(h([1 25 end]), l{[1 25 end]})