How do I create a categorical legend for imagesc with square legend symbols? - image

I have with 5 different values and I would like to create a legend ?
These are continuous data, I need small coloured squares !
How to add legend in imagesc plot in matlab Something like this but with squares, I tried replacing "line" by "rectangle" but that's not the trick apparently !
Thank you

I just used your linked example code and modified it a little:
N=4; % # of data types, hence legend entries
Data = randi(N,30,30); % generate fake data
imagesc(Data) % image it
cmap = jet(N); % assigen colormap
colormap(cmap)
hold on
markerColor = mat2cell(cmap,ones(1,N),3);
L = plot(ones(N), 'LineStyle','none','marker','s','visible','off');
set(L,{'MarkerFaceColor'},markerColor,{'MarkerEdgeColor'},markerColor);
legend('A','B','C','D')
The trick is to use markers instead of the line itself.
it returns:

Related

Plot does not highlight all the unique values of a column represented by hue

My dataframe has a column 'rideable_type' which has 3 unique values:
1.classic_bike
2.docked_bike
3.electric_bike
While plotting a barplot using the following code:
g = sns.FacetGrid(electric_casual_type_week, col='member_casual', hue='rideable_type', height=7, aspect=0.65)
g.map(sns.barplot, 'day_of_week', 'number_of_rides').add_legend()
I only get a plot showing 2 unique 'rideable_type' values.
Here is the plot:
As you can see only 'electric_bike' and 'classic_bike' are seen and not 'docked_bike'.
The main problem is that all the bars are drawn on top of each other. Seaborn's barplots don't easily support stacked bars. Also, this way of creating the barplot doesn't support the default "dodging" (barplot is called separately for each hue value, while it would be needed to call it in one go for dodging to work).
Therefore, the recommended way is to use catplot, a special version of FacetGrid for categorical plots.
g = sns.catplot(kind='bar', data=electric_casual_type_week, x='day_of_week', y='number_of_rides',
col='member_casual', hue='rideable_type', height=7, aspect=0.65)
Here is an example using Seaborn's 'tips' dataset:
import seaborn as sns
tips = sns.load_dataset('tips')
g = sns.FacetGrid(data=tips, col='time', hue='sex', height=7, aspect=0.65)
g.map_dataframe(sns.barplot, x='day', y='total_bill')
g.add_legend()
When comparing with sns.catplot, the coinciding bars are clear:
g = sns.catplot(kind='bar', data=tips, x='day', y='total_bill', col='time', hue='sex', height=7, aspect=0.65)

Changing colors on dimple.js scatter plot

How can I change the color of the circles on a scatter plot based on one of the fields that I'm not using on neither of the axes?
Example, this code:
var myChart3 = new dimple.chart(svg3, data);
myChart3.addMeasureAxis("x", "salary");
myChart3.addMeasureAxis("y", "bonus");
var mySeries = myChart3.addSeries(["Index","a"], dimple.plot.scatter);
myChart3.draw();
produces this graph:
but I also would like to color the bubbles based on a third field called "department"
thanks
The first parameter of addSeries determines colours. In the case of an array the last element is used, so you just need to do:
var mySeries = myChart3.addSeries(["Index","a","department"], dimple.plot.scatter);

Change Default d3.js colors

I was looking for a way to change the default colors of the different categories in d3.js.
I found where the colors are laid out in the main d3.js. They look like this for one category:
var ml = [2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175].map(yt)
I've tried replacing these values with everything from Hex codes to HSL to RGB and it never yields the expected colors.
Any ideas how I can generate the proper numbers for whatever colors I want?
Thanks.
First, just FYI, to see the RGB (i.e. hex) value that corresponds to these numbers:
(2062260).toString(16); // 16 for hex, aka base 16
> "1f77b4"
Next, given an RGB (again, hex) that you want to convert to number:
parseInt("1f77b4", 16); // 16 for hex
> 2062260
And that would be the number you want to use.
The colors you got from the d3 source are used to construct what you get from d3.scale.category10(). You can get the same thing but with your own colors — and without modifying d3's source code — by constructing a d3.scale.ordinal:
var myCategory3 = d3.scale.ordinal()
.domain(["red", "#1f77b4", "rgb(128, 255, 128)"]);// All kinds of colors are possible
myCategory3("X");// "red"
myCategory3("blabla");// "#1f77b4"
myCategory3("X");// "red"
myCategory3(123456);// "rgb(128, 255, 128)"

Add the three channels in a image to obtain a color image MATLAB

I am modifying images in matlab and I have a problem.
I need to separate the 3 channels of color and modify them separately.
I use this to obtain the three channels:
a = imread('./images/penguins.png');
colorlist = {'R','G','B'};
subplot(2,2,1);
imshow(a);
for k=1:3
subplot(2,2,k+1);
imshow( a(:,:,k));
title(colorlist{k});
end
a(:,:,k) is one color of the three. The problem is when I add the three vectors in one, to obtain the color image. I do this:
A=a(:,:,1)+a(:,:,2)+a(:,:,3)
figure; imshow(A);
But it dont works, it only show me a very highlight image, no a color image.
Anyone knows how can I recover the color image? Thanks for yout help^^
You are adding the values of the three layers instead of concatenating them in a 3D array.
Try this:
A= cat(3, a(:,:,1), a(:,:,2), a(:,:,3));
I should also note that you can edit the layers simply by indexing, say you want to switch the red and green components:
I1 = imread('http://i.stack.imgur.com/1KyJA.jpg');
I2=I1;
I2(:,:,1)=I1(:,:,2);
I2(:,:,2)=I1(:,:,1);
imshowpair(I1,I2, 'montage');
Now if I take your title literally, let's say you do want to add the three layers and display the result with a colormap, you can do:
A=a(:,:,1)+a(:,:,2)+a(:,:,3)
imagesc(A); axis image;
colorbar;
Results:

matlab: texture classification

I have a histology image like this:
From the image, we can observe there are two kinds of different cells.
and
Is there any way that I can separate these two types of cells into two groups?
How about using your raw image and previous code to achieve this?
% % % your old code
I=imread(file);
t1=graythresh(I);
k1=im2bw(I,t1);
k1=~k1;
se = strel('disk',1);
k0=imfill(~k1,'holes');
cc = conncomp(k0);
k0(cc.PixelIdxList{1})=0;
k1=imfill(k1,'holes');
mask=k0 | k1;
%%%%%%%%%%%%%%%%%%
This will give you:
I=rgb2hsv(I);
I=double(I);
I1=I(:,:,1); % again, the channel that can maximizing the margin between donut and full circle
Imask=(I1-0.2).*(I1-0.9)<0;
k2=mask-Imask;
k2=bwareaopen(k2,100);
This will give you:
k2=mask-Imask;
I2=zeros(size(I1,1),size(I1,2),3);
I2(:,:,1)=(k2==1)*255;
I2(:,:,3)=((I1-0.2).*(I1-0.9)<0)*255;
imshow(I2)
will finally give you (the two types are stored in two channels in the rgb image):
I would use regionprops
props=regionprops(YourBinaryImage, 'Solidity');
The objects with a high solidity will be the disks, those with a lower solidity will be the circles.
(Edit) More formally:
I=imread('yourimage.jpg');
Bw=~im2bw(I, 0.5);
BWnobord = imclearborder(Bw, 4); % clears the partial objects
Props=regionprops(BWnobord, 'All');
solidity=cell2mat({Props.Solidity});
Images={Props.Image};
Access the elements of Images where the value in solidity is higher than 0.9 and you get your disks. The circles are the other ones.
Hope it helps

Resources