I want to add objects to my matlab plots which have defined x limits but span the whole y range. Examples are vertical lines or shaded regions delimited by two x values. I am aware of the option to use the current plot limits like this:
plot(1:10)
yl = ylim();
% Use y limits of current plot as y values
patch([ 3 3 5 5 ],[ yl(1) yl(2) yl(2) yl(1) ], 'red');
However I want my users to be able to increase plot y limits afterwards (e.g. to synchronize limits of multiple plots) and also want them to continue profiting from Matlab's automatic setting of plot limits.
This would be archivable if I would use the following code to set the y coordinates of my objects to the largest and smallest possible integers, respectively (intmax() and intmin() in Matlab) and tell Matlab not to consider that object during calculation of plot limits.
plot(1:10)
% Make graphical object which spans the whole possibly y range
p = patch([ 3 3 5 5 ],[ intmin intmax intmax intmin ], 'red');
% Does something like the following function exist?
exemptFromPlotLimitsCalculation(p)
Is this possible in Matlab?
You could plot the patch (or fill) really large (for example by using realmax) and exclude it from rescaling by setting the property YLimInclude to off
patch([3 3 5 5], realmax*[ -1 1 1 -1], 'red', 'YLimInclude', 'off');
have a look at this
In the postActionCallback you can resize your patch
Related
I have multiple histograms generated from various samples that need to be combined in the end. What I have found is that I am not getting good results at the combination stage because different plots have different max values, but if I normalize them to somewhat similar values I get a good result.
For example the below three plots:
Now as can be seen one of the plots peak at around 0.067 while the other two at around 0.4. I cannot combine them in this state, but after looking at the plots visually I know that if I multiply the first plot 0.6 I get this:
Now they are at same level and can be displayed together.
I am doing this visually for every result. Would it be possible to automate this? As its not always like this, sometimes the first and second inputs(plot) are low but the third one is peaked and I would have to divide the third plot by a certain value, which I know after visually looking at the plots.
Matlabs function histogram has some normalization types built in. You can either normalize the number of counts, or the sum of the histogram area (see also), ... but you cannot yet normalize for a maximum value which is what you want probably.
I recommend to compute the histograms without plotting using histcounts, then normalizing them to a common maximum like 1 for example and then plotting them all together or separate in bar plots.
Example:
% generate example data
a = randn(100, 1) + 5;
b = randn(100, 1) * 4 + 8;
nbins = 0:20;
% compute histograms
[na, edges] = histcounts(a, nbins);
centers = mean([edges(1:end-1);edges(2:end)]);
nb = histcounts(b, nbins);
% normalize histograms to maximum equals 1
na = na / max(na);
nb = nb / max(nb);
% plot as bar plots with specified colors (or however you want to plot them)
figure;
bar_handle = bar(centers', [na',nb']);
bar_handle(1).FaceColor = 'r';
bar_handle(2).FaceColor = 'g';
title('histogram normalized to max');
and it looks like
I want to plot graphs in matlab, In hand, I had two raw data obtained from the market, say at year 0.25,0.5,0.75,1,2,3 and 4, corresponding values of product A are [0.9998,0.997,0.887,0.779,0.661,0.442,0.345] and B are [0.878,0.765,0.662,0.594,0.436,0.304,0.211] respectively. When I use
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
However, the graphs produced gives 4 lines. What should be done to fix the problem?
You need to create a graphic handler before modifying the XTick, Use the following
figure
ax = gca ;
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
ax.XTick = [0 0.25 0.5 0.75 1 2 3 4];
let's consider i am plotting two signals together on the same graph which has different limits and thus i want different axis
plot(a)
axis ([-2 10 -2 8])
hold 'on'
plot(b)
axis ([-1 4 -4 7])
hold 'off'
where 'a' and 'b' are two signal expression. the problem here is the signals are getting plot but only the second axis is working and plot a is not getting limited to the first specified axis. the reason being the second axis is obviously overwriting the first axes but any idea how to plot both signals with both axis limits?
You can select the data you wish to plot using logical operators.
Let's consider the case for plot a.
Assign each column of bs to a variable:
x1 = bs(:,1)
y1 = bs(:,2)
Then select only the values that meet the condition specified:
xPlot = x1(x1 > -2 & x1 < 10)
yPlot = y1(y1 > -2 & y1 < 8)
Assuming they both contain the same number of elements you can then plot them.
If not, you need to pad the smaller array with Nan, for example, to avoid getting an error about mismatching dimension.
Once you know which array is smaller, you can do it as follows. Let's say in this case xPlot is smaller than yPlot:
m = max(numel(xPlot),numel(yPlot)) %// Just getting the larger dimension
xPlot(numel(xPlot)+1:m) = NaN
Now you can call
plot(xPlot,yPlot,'b-','LineWidth',2)
and that should work. The same applies for the b plot.
Hope that helps!
You may want to have a look at plotyy to get 2 different y-axis.
If there is nothing in common in your plots, maybe you should plot them on 2 different axes, like
figure('Name', 'Example');
subplot(121);plot(rand(3));
subplot(122);plot(rand(3));
UPDATE
If you absolutely need two axes, you may try something like this
figure('Name', 'plotyy');
h = plotyy([0 1 2 4], 0:4, [4 5], [2 1]);
linkaxes(h, 'off');
axis(h(1), [0 4 0 4]);
axis(h(2), [4 5 1 2]);
If what you're looking for is something along the lines of plotyy but in the other direction, look at plotxx function from matlabcentral that does a similar thing in the x-direction.
You may have to tweak it to get it do everything you need, but it will give you a good starting point.
You can use plotyy which will create 2 y axes with different scales and limits.
I have two time series and I plot some similarity measures using colorbar. However, for one of my metrics, one of the results is very high compared to the other. Therefore, I can't distinguish enough variability in the charts. Is there a way to exclude some too high data from the figure ?
Thnx
How about just apply a threshold before plotting:
%//Code assumes 2D image:
I_th = I;
I_th(I < threshold ) = threshold ; %//where threshold is a constant you define
imagesc(I_th);
you can force the values above a certain threshold to be the threshold value.For example,
A=[1 2 3 4 5];
A(A>3)=3;
this will give you A=[1 2 3 3 3];
Alternatively, instead of excluding values, you might consider doing a color scale with a log transform, so that you can distinguish the colors better.
here is one example:
http://www.mikesoltys.com/2012/03/16/matlab-tip-logarithmic-color-scales-for-contour-and-image-plots/
Scatter Plots in MatLab. I can create a scatter plot with x and y arrays being the same size as follows:
function RasterTest()
clear all % clear memory
clc; % clear command window
close all; % close any figure windows that are open
x=[1 2 3]; % x positions of the charges
y=[4 8 2]; % y positions of the charges
scatter(x,y,'filled')
axis square
end
However, what if I want every x to have multiple y values? I.e. the array sizes are different. I think this is actually called a raster plot but MatLab doesn't seem to have something to do this?
Any help would be great :).
plot allows diferent size vectors
plot(x,[sin(x);2*sin(x);3*sin(x)],'*')
When the array sizes are different, how can you map every y value to the according x value? It's ambigous.
When you generate your data, just make sure that you insert every pair of values into the x and y arrays:
x = [1 2 3 1 3];
y = [3 4 5 6 7];
In the above example you got multiple points for the x values 1 and 3.