I"m trying to color-code each of 4 histograms using a predefined color scheme. Here's a minimal working example:
colours = [0 32 96;
192 0 0;
71 207 255;
255 143 143;
] / 255;
for i=1:4
x = randn(1,100);
subplot(1,4, i)
values = histogram(x, 'FaceColor', colours (i, :));
end
However, in the image I'm getting, the colors are actually (slightly) different, for instance for the first histogram I get (102,121,160) instead of (0,32,96):
The default histogram FaceAlpha is 0.6 (ref.). Setting it to 1 should do the trick.
histogram(x,'FaceColor',[0 1 0],'FaceAlpha',1);
Very cumbersome and unexpected.
Related
I have a discrete signal x of length N traced in MATLAB using the command
stem(abs(x)); axis([0 N+6 0 4]);
The resulted figure is shown below:
My question I need only some values corresponding for example to index [7 10 11 12 15 18 48 50 52 60] to be colored with different color , let's say with red.
How can I do that into my figure ?
Using Multiple Plots by hold on and Matrix Indexing
You could possibly and alternatively place a plot on top of plot by using hold on. This does require an adjustment where you need a vector in this case Sample and Indices which specify the sample number/data point index. You can also use matrix indexing to get the amplitude/data points corresponding to the key point, Indicies.
%Vector relating to the sample/data point number%
Sample = linspace(1,70,70);
%Random test data%
X = randi([0,2],1,70);
stem(Sample,X);
hold on
%Key indices to change colour%
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
%Matrix indexing to get values/amplitudes corresponding to key indices%
X_Prime = X(Key_Indices);
stem(Key_Indices,X_Prime,'r');
axis([0 70 0 3]);
hold off
Ran using MATLAB R2019b
This code makes just the circles red and not the stems
plot with selected red circles
%Vector relating to the sample/data point number
Sample = linspace(1,70,70);
%Random test data
X = randi([0,2],1,70);
stem(Sample,X);
%Key indices to change color
Key_Indices = [7 10 11 12 15 18 48 50 52 60];
line(Sample(Key_Indices), X(Key_Indices), 'linestyle', 'none', 'marker', 'o', 'color', 'r')
axis([0 70 0 3])
grid on
I am plotting trajectories in Matlab using contourf. I am having an issue with the colors matching the data. I have posted my current image below. All areas that do not have data should be white as they are zero which I specifically specified in the script(they are currently blue-ish which is the the 0.1 to 1 range). In addition, values that are yellow, should be in the blue range(<1). Any suggestions?
Here is the part of my script where I do the plotting:
axesm('mercator', 'MapLatLim', latlim, 'MapLonLim', lonlim,...
'Frame', 'on', 'Grid', 'on', 'MeridianLabel', 'on', 'ParallelLabel', 'on')
setm(gca,'mlabelparallel',-20)
load coastlines
Contours = [0.001 0.01 0.1 1 10 100];
[c,h] = contourfm(latlim, lonlim, u, log(Contours));
colorbar('YTick', log(Contours), 'YTickLabel', Contours);
myColorMap = jet(256).^.3;
myColorMap(1,:) = [1];
colormap(myColorMap)
colorbar
caxis(log([Contours(1) Contours(length(Contours))]));
colorbar('FontSize', 12, 'YTick', log(Contours), 'YTickLabel', Contours);
geoshow(coastlat, coastlon,'Color', 'k')
Contour level, V, in contourfm(lat,lon,Z, V) does not scale your data or colour. It works in a different way than what you thought.
Let's see one example first:
u = rand(8)+0.1; u(1:2,:) = 0; u(5:6,:) = 10; u(7:8,:) = 100;
V = [0,1,40,100];
contourfm([0,1], [0,1], u, V);
mycm = jet(256).^.3; mycm(1,:) = 1;
colormap(mycm)
contourcbar('FontSize', 12, 'YTick', V, 'YTickLabel', V);
where u is
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0.1947 0.6616 0.2413 0.6511 0.4403 0.9112 1.0016 0.5654
0.8422 0.3159 0.5695 0.6478 0.9933 0.1686 0.8387 0.5362
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100
As you can see, for V = [0,1,40,100] all values from 0 to 1 are white, values from 1 to 40 are cyan and above are red.
Therefore, you must scale your u then assign appropriate contour level. Use contourcbar instead of colorbar to check the colours first.
Apart from the problem with contour level, I suspect the u parameter contains negative values. The colour at the bottom of the colour bar is always assigned to the minimum z value. You must ensure 0 is the minimum value in u, i.e. remove the negative values.
I have a series of ordered points (X, Y, Z) and I want to plot a 3D histogram, any suggestions?
I'm trying to do it by this tutorial http://www.mathworks.com/help/stats/hist3.html , but points are random here and presented as a function. My example is easier, since i already know the points.
Furthermore, depending on the number value of Z coordinate, i'd like to colour it differently. E.g. Max value - green, min value - red. Similar as in this case Conditional coloring of histogram graph in MATLAB, only in 3D.
So, if I have a series of points:
X = [32 64 32 12 56 76 65]
Y = [160 80 70 48 90 80 70]
Z = [80 70 90 20 45 60 12]
Can you help me with the code for 3D histogram with conditional coloring?
So far the code looks like this:
X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));
figure;
h = bar3(A); % get handle to graphics
for k=1:numel(h),
z=get(h(k),'ZData'); % old data - need for its NaN pattern
nn = isnan(z);
nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point
nz(nn) = NaN; % used saved NaN pattern for transparent faces
set(h(k),'CData', nz); % set the new colors
end
colorbar;
Now I just have to clear the lines and design the chart to make it look useful. But how would it be possible to make a bar3 without the entire mesh on 0 level?
Based on this answer, all you need to do is rearrange your data to match the Z format of that answer. After than you might need to remove edgelines and possibly clear the zero height bars.
% Step 1: rearrange your data
X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));
% Step 2: Use the code from the link to plot the 3D histogram
figure;
h = bar3(A); % get handle to graphics
set(h,'edgecolor','none'); % Hopefully this will remove the lines (from https://www.mathworks.com/matlabcentral/newsreader/view_thread/281581)
for k=1:numel(h),
z=get(h(k),'ZData'); % old data - need for its NaN pattern
nn = isnan(z);
nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point
nz(nn) = NaN; % used saved NaN pattern for transparent faces
nz(nz==0) = NaN; % This bit makes all the zero height bars have no colour
set(h(k),'CData', nz); % set the new colors. Note in later versions you can do h(k).CData = nz
end
colorbar;
I randomly generated initial 10 population(let each of size n) of genetic algorithm as follows
for i = 1:10
for j=1:n
population(i,j)=randi([MinIntensity,MaxIntensity]);
end
end
Assume that I have the values of one population.For example let the first population of size 10 is [100 110 120 130 140 150 160 170 180 190].Is it possible to generate the remaining 9 population such that the values are near to the first population?(It is for the quick convergence of genetic algorithm).Also each population is a grayscale image with intensity values represented in row major order.Hence the intensity values should be in the range of 0 - 255.
Please help.Thanks in advance
You can do one thing. Use the first string as it is for the rest of the 9 strings except randomly generate an index (between 1 to n) and assign a random integer only to that positions with that random index.
population(1,:) = [100 110 120 130 140 150 160 170 180 190];
for i = 2:10
idx = randi([1 10]);
population(i,:) = population(1,:);
population(i,idx) = randi([0 255]);
end
With this you will get ten strings differing in only one position.
Edit: Image.
Assuming you have a MXN image. Create a mask for example
randi([-10 10], M , N)
Now add this to your original image. Now you get a new image whose all the pixels are modified but only within the range of -10 to 10. Some of the pixel values might go out of range in that case just modify as below
x(find(x < 0)) = 0 %Here X is your new image.
x(find(x > 255)) = 255
My code:
Screen('OpenWindow', 0, [0 0 0], [0 0 600 600])
Screen('FillRect', win, [0 255 0 ], [0 0 50 50]);
Screen('Flip', win);
I understand that the documented line is:
Screen('FillRect', windowPtr [,color] [,rect] )
With windowPtr as just a placeholder which needs to be replaced with a variable name to identify this particular shape. However when I'm using win to identify it, I am constantly getting the error:
Undefined function or variable 'win'.
Error in Practice_Script_1 (line 17)
Screen('FillRect', win, [0 255 0 ], [0 0 50 50]);
I don't understand what I'm doing wrong and it's probably just some noob mistake that is really frustrating me.
I don't have the Psychtoolbox, but this error message typically means that the (in this case) win variable is not defined. Have you initialized this variable prior to calling the above lines of code?
The following link creating experiments using MATLAB and Psychtoolbox has some sample code and they define the win variable as
win = Screen('OpenWindow',0, [900 900 1000], [10,10, 1100,1100]);
You will need to do something similar. Another link MATLAB cookbook does the following
% Initialize the screen with a black background
% rect is the coordinates of the screen
[win rect] = Screen('OpenWindow', 0, [0 0 0]);
ovalColor = [0 255 0]; % RGB color for the oval
rectColor = [255 0 0]; % RGB color for the rectangle
ovalRect = [100 100 300 200]; % Coordinates [x1 y1 x2 y2]
rectRect = [100 250 300 350]; % Coordinates [x1 y1 x2 y2]
Screen('FillOval', win, ovalColor, ovalRect);
Screen('FillRect', win, rectColor, rectRect);
Screen('Flip', win);
Try either option and see what happens.