python - how to get shc.linkage ordered leaves? - scipy

I'm making a dendrogram and after a correlogram, and I want them to have the same order of labels. In order to do that, I'd like to save in a variable the labels of the dendrogram in the order they appear.
So if, this code:
clusters = shc.linkage(df.dropna().T, 'single', 'correlation', optimal_ordering=True)
dend = shc.dendrogram(clusters, orientation='left', labels=df.columns)
plots top-bottom (D,B,A,C,E...G) leaves, I'd like to save in a variable this ordered list of leaves ordering=(D,B,A,C,E...G).
Thank you in advance.

Related

How does Holoviews know which colours to assign to each scatterplot in an overlay?

In the bokeh Holoviews gallery, there is an example called 'Scatter economic'.
http://holoviews.org/gallery/demos/bokeh/scatter_economic.html#bokeh-gallery-scatter-economic
In this plot, notice how one of the options for Scatter is (color=Cycle('Category20')). The last line of the plot is gdp_unem_scatter.overlay('Country').
My question is: How does Holoviews know to connect each Scatter to a particular color in Cycle('Category20')? Is this just a property of Cycle()? Is there some way that the Overlay interacts with the Scatter and with the Cycle automatically?
A slightly related confusion is that if I use the .opts method instead of the cell magic as in the example, it still works. For example, if I use the .opts method with this cycle color on the Scatter (i.e., second to the last line in the above example), and then do an .overlay('Country'), somehow Holoviews knows to assign each Scatter to a particular color based on the Country.
I want to make sure that I am properly plotting what I intend to.
Thank you!
It is now possible to map categories in an NdOverlay (as is used in the example above) by using a so called dim expression and then define an expression to do the mapping:
dim_expr = hv.dim('category').categorize({'A': 'red', 'B': 'green', 'C': 'blue'})
overlay = hv.NdOverlay({chr(65+i): hv.Scatter(np.random.rand(10, 2)) for i in range(3)}, 'category')
overlay.opts(hv.opts.Scatter(color=dim_expr))
In this example we created a dim expression which points to the 'category' dimension and then maps each category ('A', 'B' and 'C') to a color ('red', 'green', 'blue'). We then just assign that to the color option.
How does Holoviews know to connect each Scatter to a particular color in Cycle('Category20')? Is this just a property of Cycle()? Is there some way that the Overlay interacts with the Scatter and with the Cycle automatically?
You are correct that Cycle and Overlay are designed to interact in this way automatically. More explicitly, each color in the Cycle gets assigned to a 'layer' of the overlay until the cycle runs out of colors and it loops.
For example, if I use the .opts method with this cycle color on the Scatter (i.e., second to the last line in the above example), and then do an .overlay('Country'), somehow Holoviews knows to assign each Scatter to a particular color based on the Country.
This is because your call to opts customizes the options on the elements of the data structure before you call the overlay method on (this data structure is a HoloMap). The options set there are propagated to the Scatter elements in the HoloMap which will now have the chosen Cycle specified. This means that when these elements get put into an overlay, HoloViews can look up the Cycle appropriately and apply it correctly to the overlay.
Hope that makes sense!

How to get a list of column names within the same cluster in MATLAB

Lets say I have generated the following dendrogram:
dm=DataMatrix(heatmap_data,rowscell,text); %Matrix, RowNames, ColumnNames
cg = clustergram(dm,'Standardize','none');
cgAxes =plot(cg);
set(cgAxes, 'Clim', [-1,1])
Now, lets say I want to get the ColumnNames within a cluster, per example the ones I have highlighted with a blue rectangle:
So, in the example, should get a list:
AKAP13 BAZ1B Cgord142 BEGAINATRIP ARID4B
Is there any way to get the Column Names items based on the clustering in MATLAB?
thanks

Change markers in dynamic plots

What I have:
hold on
for i =1:length(tspan)
var{i} = Tv(:,i);
str{i} = ['t = ',num2str(tspan(i)), ' s'];
plot(z,var{i},'DisplayName',str{i});
end
legend('-DynamicLegend');
This works perfectly (thanks to this), but it prints out all blue lines.
I tried to set up a colormap (the default) and use it like this, but the output was the same
plot(z,var{i},'DisplayName',str{i},'Color', colormap(i,:));
And I would also like to see different markers for each plot. How is it possible to change them?
EDIT
Thanks to ironzionlion I fixed the colors. How can I do the same with markers?
According to the post that you mention, you have to define that you will need i different colours in your plot. This can be done by using colors = hsv(i)
Your plot sentence will then be: plot(z,var{i},'DisplayName',str{i},'Color', colors(i,:));
Update
I am not aware of the existance of "markermap". You could fix the problem just by define upfront the different markers that you want (quick and dirty solution): mrk={'o','+','*','.'};
Then you will plot by selecting each time the corresponding marker:
plot(z,var{i},'DisplayName',str{i},'Color', cmap(i,:),'Marker', mrk{i});

Location based segmentation of objects in an image (in Matlab)

I've been working on an image segmentation problem and can't seem to get a good idea for my most recent problem.
This is what I have at the moment:
Click here for image. (This is only a generic example.)
Is there a robust algorithm that can automatically discard the right square as not belonging to the group of the other four squares (that I know should always be stacked more or less on top of each other) ?
It can sometimes be the case, that one of the stacked boxes is not found, so there's a gap or that the bogus box is on the left side.
Your input is greatly appreciated.
If you have a way of producing BW images like your example:
s = regionprops(BW, 'centroid');
centroids = cat(1, s.Centroid);
xpos = centroids(:,1); should then be the x-positions of the boxes.
From here you have multiple ways to go, depending on whether you always have just one separated box and one set of grouped boxes or not. For the "one bogus box far away, rest closely grouped" case (away from Matlab, so this is unchecked) you could even do something as simple as:
d = abs(xpos-median(xpos));
bogusbox = centroids(d==max(d),:);
imshow(BW);
hold on;
plot(bogusbox(1),bogusbox(2),'r*');
Making something that's robust for your actual use case which I am assuming doesn't consist of neat boxes is another matter; as suggested in comments, you need some idea of how close together the positioning of your good boxes is, and how separate the bogus box(es) will be.
For example, you could use other regionprops measurements such as 'BoundingBox' or 'Extrema' and define some sort of measurement of how much the boxes overlap in x relative to each other, then group using that (this could be made to work even if you have multiple stacks in an image).

Change label of data tips on bode nichols diagram

When we plot a bode/nichols locus, the name of workspace variable is used
tmp=ss(1,1,1,0);
nichols(tmp);
will use 'tmp' as label.
When using more complex data, matlab is using 'untitled1','untitled2',...
tmp={ss(1,1,1,0) , ss(1.2,1,1,0)};
nichols(tmp{:});
How can I change this label programmatically?
Ideally, I'd like a solution working with Matlab 6.5.1, but I'm also interested in solutions restricted to newer versions.
You can modify the labels programmatically via their graphics handles. It looks like the values you want to change are the DisplayName property of some of the children of the current axis. So in your first example, I can change the display name like this:
ch = get(gca,'Children');
set(ch(1),'DisplayName','Fred');
In general, I'm not sure how to predict which children of the current axis are the ones you need to change. For the second example you give, the two curves appear to be the second and third children when I run your code.