How to visualize correlation matrix as a schemaball in Matlab - matlab

I have 42 variables and I have calculated the correlation matrix for them in Matlab. Now I would like to visualize it with a schemaball. Does anyone have any suggestions / experiences how this could be done in Matlab? The following pictures will explain my point better:
In the pictures each parabola between variables would mean the strength of correlation between them. The thicker the line is, the more correlation. I prefer the style of picture 1 more than the style in picture 2 where I have used different colors to highlight the strength of correlation.

Kinda finished I guess.. code can be found here at github.
Documentation is included in the file.
The yellow/magenta color (for positive/negative correlation) is configurable, as well as the fontsize of the labels and the angles at which the labels are plotted, so you can get fancy if you want and not distribute them evenly along the perimeter/group some/...
If you want to actually print these graphs or use them outside matlab, I suggest using vector formats (eg eps). It's also annoying that the text resizes when you zoom in/out, but I don't know of any way to fix that without hacking the zoom function :/
schemaball % demo
schemaball(arrayfun(#num2str,1:10,'uni',false), rand(10).^8,11,[0.1587 0.8750],[0.8333 1],2*pi*sin(linspace(0,pi/2-pi/20,10)))
schemaball(arrayfun(#num2str,1:50,'uni',false), rand(50).^50,9)

I finished and submitted my version to the FEX: schemaball and will update the link asap.
There are a some differences with Gunther Struyf's contribution:
You can return the handles to the graphic object for full manual customization
Labels are oriented to allow maximum left-to-rigth readability
The figure stretches to fit labels in, leaving the axes unchanged
Syntax requires only correlations matrix (but allows optional inputs)
Optimized for performance.
Follow examples of demo, custom labels and creative customization.
Note: the first figure was exported with saveas(), all others with export_fig.
schemaball
x = rand(10).^3;
x(:,3) = 1.3*mean(x,2);
schemaball(x, {'Hi','how','is','your','day?', 'Do','you','like','schemaballs?','NO!!'})
h = schemaball;
set(h.l(~isnan(h.l)), 'LineWidth',1.2)
set(h.s, 'MarkerEdgeColor','red','LineWidth',2,'SizeData',100)
set(h.t, 'EdgeColor','white','LineWidth',1)
The default colormap:
To improve on screen rendering you can launch MATLAB with the experimental -hgVersion 2 switch which produces anti/aliased graphics by default now (source: HG2 update | Undocumented Matlab). However, if you try to save the figure, the file will have the usual old anti-aliased rendering, so here's a printscreen image of Gunther's schemaball:

Important update:
You can do this in Matlab now with the FileExchange submission:
http://www.mathworks.com/matlabcentral/fileexchange/48576-circulargraph
There is an exmample by Matlab in here:
http://uk.mathworks.com/examples/matlab/3859-circular-graph-examples
Which gives this kind of beautiful plots:

Coincidentally, Cleve Moler (MathWorks Chief Mathematician) showed an example of just this sort of plot on his most recent blog post (not nearly as beautiful as the ones in your example, and the connecting lines are straight rather than parabolic, but it looks functional). Unfortunately he didn't include the code directly, but if you leave him a comment on the post he's usually very willing to share things.
What might be even nicer for you is that he also applies (and this time includes) code to permute the rows/columns of the array in order to maximize the spatial proximity of highly connected nodes, rather than randomly ordering them around the circumference. You end up with a 'crescent'-shaped envelope of connecting lines, with the thick bit of the crescent representing the most highly connected nodes.
Unfortunately however, I suspect that if you need to enhance his code to get the very narrow, high-resolution lines in your example plots, then MATLAB's currently non-anti-aliased graphics aren't quite up to it yet.

I've recently been experimenting with MATLAB data and the D3 visualization library for similar graphs - there are several related types of circular visualizations you may be interested in and many of them are interactive. Another helpful, well-baked, and freely available option is Circos which is probably responsible for most of the prettier versions of these graphs you've seen in popular press.

Related

Change size of figure in MATLAB to use them in LaTeX

I have generated several plots in MATLAB and I haven't set the ( width or height) to any of them. I save them as pdf to place them in LaTeX but each figure appear with different size. I can change each figure size in LaTeX but I wonder if there is an easy way in MATLAB to get same size for all figures.
Can I reset the figure size after generating in MATLAB or I have to regenerate each figure with specific size?
What is the best way to export figures in MATLAB to use them in LaTeX?
Exporting figures from MATLAB to LaTeX is best done via .eps or directly to PDF. MATLAB's saveas() functionality supports both. The benefit of these file-types is that thy are vector files, meaning, loosely, that coordinates of corners in the lines are saved, as opposed to a bunch of fixed pixels such as with PNG or JPG figures. Using then these figures in LaTeX gives infinite scalability (theoretically, usefulness of highly-zoomed figures may vary).
If you need different aspect ratios, i.e. one figure taller than broad, the other broader than tall, You'd have to set the proper aspect ratio for each figure independently, then save using one of the aforementioned vector file types.
One of the best ways to export pretty figures is the export_fig() functionality written by Oliver Woodford and currently maintained by Yair Altman and freely available on the FileExchange.
Answering your first question: yes you can resize figures after generating them. Either by hand (change the window size), or programatically: you can make a figure handle (h=figure();, where h is your handle), which has several options to control figure size; h.position being the most prominent: [upper-left, lower-left, upper-right, lower-right] being its content in terms of corner coordinates. Changing those changes the figure size.

Using the Overlay feature in panel but showing only an averaged line

I have created an overlay of 100 curves. I thought the image looked impressive but one of my reviewers stated that I should only show the averaged line. Meaning, only show a line that is the average of the 100 curves. Is this possible using the xtline feature, or do I need to get deeper into programming code to produce the graphic? Alternatively, it would be great if I could show both (the 100 curves and the averaged curve) in the same graphic.
You don't need anything complicated here. Just calculate the mean across the panel and show it directly. Here is some technique. I am guessing that in your real example with 100 curves a legend is pointless. Note the c(L) and read the help to find what it does.
webuse grunfeld, clear
set scheme s1color
gen log_invest = log(invest)
egen mean_log_invest = mean(log_invest), by(year)
line log_invest year, legend(off) lc(gs12) c(L) || line mean_log_invest year, c(L) scheme(s1color) ytitle(something sensible)

Matlab: Export a figure made with myaa

I am trying to get better quality phase plots of complex functions made with the Complex Function Explorer of E. Wegert CFE. For this purpose I apply the Matlab anti-aliasing function myaa.m to the phase plots that are made with the CFEGUI.m. For the example screenshot of the result window below I used the setting myaa([8 8]) in the Matlab command window which means that the supersampling enlarge the figure 8x and then downscale it to 1/8 to get the original h x w.
As one can see the window of the figure has no operation icons or menu options for save or print. My question is what to do with such a figure (beside making screenshots)? Can I somehow use export-fig to save such a figure or load it in an image array and if yes, how?
It is also possible to use the setting myaa([8 1]) which results in a very large figure window that is larger than my screen (and has the unpleasant attribute that it can not be moved). It would be even better if such a whole figure could be saved (not only the visible part).
You can use getframe to grab current figure information in pixels.
However, while I do not know how this affects you, notice that MATLAB versions above 2014b (included) have already anti-aliasing. The code you linked seem to be from 2008, I am not 100% sure if it has become obsolete now.

MATLAB and high quality EPS figures

I am looking to export my MATLAB plot as a high quality figure. Specifically, I would like to save it as a vector based file format such as EPS or SVG.
I have tried print and saveas commands:
saveas(h,'myFileName','epsc2');
print('-r150','-depsc2', 'myFilename');
On all occasions this produces poor quality parts of the graph, although the axis-labels are indeed vector. Why does MatLab do some horrible rendering before putting it into an EPS?
Example of poor quality plot here:
http://users.ox.ac.uk/~pemb2372/myFileName.eps
Edit:
It is also worth noting that if you use a Mac viewing an EPS file from Matlab, 'Preview' app may render inner graph content rasterized and poor quality, while leaving the axis and labels vectorized and high quality. This is very misleading but when you open said EPS file in, for example, Inkscape, the quality is actually vector and quite high.
Edit 2:
My university hosting account has expired, so you can no longer view the figure. Suffice it to say that it showed a poor quality raster-style plot within high quality beautiful axis lines, ticks and labels.
I thought I would share the issue I had, and how I overcame it...
I was getting terrible results because I had the wrong renderer set to default. In my startup.m, I had the zbuffer renderer enabled. This is an example eps output.
I made that eps output with: print(gcf,'-depsc2','filename.eps'). This eps is so OBVIOUSLY rasterised. It makes me angry at matlab. Then, I had a brainwave - perhaps my default renderer zbuffer is interfering with the image save process. So, adding the line:
set(gcf,'renderer','painters')
and running the print command as before, here is the output:
Note that I just took screenshots of the eps output files at 100%. And I can confirm the second image is actually vector. Here is a good question/explanation on choosing Renderers in MATLAB.
Matlab can export to pdf with better quality than EPS, but with its own caveats of setting decent margins and font sizes.
edit:
Examples are similar to the EPS case as explained in the help of e.g. print:
saveas(gcf,'filename.pdf')
or
print('-dpdf','filename.pdf')
You might also want to take a look at the PaperSize, PaperPosition and PaperUnits properties of your figure (by means of the set and get functions).
edit: Another option is to use one of the functions available on FileExchange such as the ones mentioned by #user664303 below. My personal favorite for use with LaTeX is matlab2tikz for which the latest version can be gotten from GitHub. Together with the external library of TikZ, I think this delivers some of the most nicest graphs around.
Probably it's also best to mention that I have been actively involved in the matlab2tikz project since 2012.
The export_fig function on the MATLAB file exchange is a reasonably reliable way of accurately exporting figures to eps and pdf (as well as bitmap formats) in MATLAB.
The plot2svg function, also from the file exchange, allows you to export in svg format. It provides some additional benefits, such as being able to export translucent patch objects in vector format.
A comparison of exporting methods is given in this blog post.
I always acquire the final plots (those which are supposed to be inserted into papers and publications) by matplotlib library of python.
You can bet on the amazing quality of the generated plots, both .pdf and .eps formats.

Using imtophat in MATLAB

I'm trying to do top hat filtering in MATLAB. The imtophat function looks promising, but I have no idea how to use it. I don't have a lot of work with MATLAB before. I am trying to look find basically small spots several pixels wide that are local maxima in my 2 dimensional array.
I think you have more problem undertanding how to use STREL, than IMTOPHAT. The later can be described as simple threshold but per structural element, not the whole image.
Here is another good examples of using STREL and IMTOPHAT:
http://www.mathworks.com/matlabcentral/fx_files/2573/1/content/html/R14_MicroarrayImage_CaseStudy.html
This series of posts on Steve Eddins blog might be useful for you:
http://blogs.mathworks.com/steve/category/dilation-algorithms/
tophat is basically an "opening" procedure followed by a subtraction of the result from the original image. the best and most helpful explanation of opening I've found here:
http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm
"The effect of opening can be quite easily visualized. Imagine taking
the structuring element and sliding it around inside each foreground
region, without changing its orientation. All pixels which can be
covered by the structuring element with the structuring element being
entirely within the foreground region will be preserved. However, all
foreground pixels which cannot be reached by the structuring element
without parts of it moving out of the foreground region will be eroded
away."
The documentation on imtophat has an example .. did you try it? The following images are from the MATLAB documentation.
Code
I = imread('rice.png');
imshow(I)
se = strel('disk',12);
J = imtophat(I,se);
figure, imshow(J,[])
Original
(image source: mathworks.com)
Top Hat with a disk structuring element
(image source: mathworks.com)