Matlab multi-column legend & subplots - matlab

I have a legend with 7 entries, some of which are on one subplot, some are on another, and some are common to both plots. I have managed to get all of these entries into one legend using some help I found online, which was great.
Only problem I have now is that the strings for each entry are quite long, so I ideally need them in 2 columns with the 7th entry centred in the middle at the bottom of the legend. I have found various options to do a multi-columned legend:
Code for a multicolumn legend in Matlab
http://www.mathworks.co.uk/matlabcentral/fileexchange/27389-columnlegend
http://www.mathworks.co.uk/matlabcentral/fileexchange/29248-gridlegend-a-multi-column-format-for-legends
But I can't work out how to combine them with the way I have already worked out how to do the legend.
This is MWE of what I have so far:
xdata=1:1:10;
ydata=0:0.1:0.9;
% Line colours
cm=[32 114 214;145 205 237;0 129 63;247 217 9;255 127 0;184 18 21];
cm=cm/255;
h1=subplot(2,1,1);
l1=line(xdata,ydata,'Color',cm(1,:));
l2=line(xdata,ydata,'Color',cm(2,:));
l3=line(xdata,ydata,'Color',cm(3,:));
l5=line(xdata,ydata,'Color',cm(5,:));
l7=line(xdata,ydata,'Color','k','LineStyle','--');
h2=subplot(2,1,2);
l4=line(xdata,ydata,'Color',cm(4,:));
l6=line(xdata,ydata,'Color',cm(6,:));
line(xdata,ydata,'Color','k','LineStyle','--');
hL=legend(h2,[l1,l2,l3,l4,l5,l6,l7],{'Second/Multi-year La Niña','First/Single-year La Niña','Neutral ENSO years','First/Single-year El Niño','El Niño years','Second/Multi-year El Niño','All Years (1901-2011)'},'Location','SouthOutside','Orientation','horizontal');
set(hL,'Units','centimeters');
Lpos=get(hL,'Position');
Lpos(1)=0;
Lpos(2)=0;
set(hL,'Position',Lpos);
My actual data doesn't overlap so you can see all the lines. Was just easier to have it like that for the example.
Can anyone help me out with the layout I need for the legend? It may well be that I can use one of the 3 options I already found, but I don't really understand what they do well enough to apply it to my situation.

I found another work around with this, which is to create 3 separate legends and then position them where I want. I made a 3rd subplot which has the 'Visible','off' setting and called my three legends using the following code:
lg1=legend(h1,[l1,l2,l3],{'a','b','c'});
lg2=legend(h2,[l4,l5,l6],{'d','e','f'});
lg3=legend(h3,l7,{'g'});
where h1-3 are the subplots, l1-7 are the 7 lines, and a-g are the legend text. I have then rearranged them using get(lg1,'Position'), changing the position vector, and resetting it using set(lg1,'Position',...) (replace ... with the position vector). I turned the box off around the three legends, but plan on putting a box all round the edge so it looks like it is one legend.
Not the prettiest way to do it, but it worked for what I needed to do.

Related

How to force a line break in a legend of a boxplot?

I need help to skip lines in the legend of boxplots : I have a plot with two boxplots, and each of them has a legend to long to fit on a single line.
I tried this, X being my matrix of data (two colums) :
boxplot(X,'Labels',{'1stpartofthelegend\newline2ndpartofthelegend','1stpartofthelegend\newline2ndpartofthelegend})
This solution works in other types of plots, like a plotspread plot so I don't understand why it is not working here ?
I found this question, but the solutions are not working for me, maybe because I have a more recent version (R2018b).
I actually want to add my boxplots over a plotpsread plot, but when I do that the legends of the boxplots win over the legends of the plotspread (even if I add boxplots without legends, the previous legends of the plotspread are replaced by "1" and "2"). So preventing the boxplot to erase the previous legend would be a nice solution but as I failed to do that I tried to find another way.
Thank you for your help
You want to set the 'Labels' of the boxplot for multiple lines. So use a cell of cells:
Lbl = { {'1st entry (1st line)','2nd entry (1st line)'},...
{'1st entry (2nd line)','2nd entry (2nd line)'} };
boxplot(rand(100,2),'Labels',Lbl)
EDIT
LaTeX commands work in the title and labels but apparently there is no interpreter or TickLabelInterpreter property for boxplot-Labels:
Error using internal.stats.parseArgs (line 42)
Invalid parameter name: TickLabelInterpreter.

Matlab figure visualization

In Matlab, I would like to visualize the test results in a figure with several charts and text. The figure is divided into rows and colums: 3 rows and 5 colums. For better understanding, here a screenshot of the figure with a orange grid which shows the subplot division:
Now I have several questions:
A) How can I include text into a specific section within the figure? i.e. test settings into subplot(6 and 11) and test results into subplot (7, 8, 9, 10).
B) Is it possible to "draw" separator lines between the subplots? i.e. to separate the test settings from the test result subplots for a better visualization.
C) Is it possible to set a title over several subplots such as "input data" and "output data"?
Thanks for your help!
Cheers,
Kevin
I have come this problem many times and haven't yet figured out a decent way to solve it. However what you can do is:
A) Include a Label (help label) in the subplot you want. Alternatively use a "edit locked" edit text field.
B) Yes in a way. Check out panels. Create a subplot, then inside a panel that fills the plot area. with the panel as parent create a figure (or label as in A) )
C) Thats a tricky one and I would use panels again, but I am not sure if that works.
These things are always a pain to do in Matlab itself. I usually ended up exporting my figures, writing a small HTML generator that places the images in divs and a decent CSS to make it look nice. It is way easier to do so if it is only for representing data. If you want it to be interactive you have to do it inside the UI.
Hope that helps
Benjamin

Custom number format for y-axis on Chart

I have created a chart with 2 axes that acts as a panel chart (see image)
As a panel chart I only want to show the portions of the relevant y-axes to the chart next to them. For example, for the right-most y-axis I used a custom number format to exclude anything less than 0:
_(* #,##0_);_("";_(* 0??_);_(#_)
But for the left most y-axis, I'm stuck. I want to show -400 to positive 400. I've tried 2 different options, but neither is producing the desired effect.
[<0](#,##0);[>500000000]"";#,##0_)
[<0](#,##0);[<500000000]#,##0_);""
Here is the result I'm looking for:
I learned something new today (and a bit weird) regarding formats and chart axes
After some experimenting, this is what I ended up using:
[White][>500]_(#,##0_);(#,##0);0;
The odd part: When you change the Display Units of the axis (for me, millions), then the formatting no longer recognizes the original amount (500,000,000).
Once I figured that out, I was able to work out the solution.

produce several maps in same figure window in matlab

I would like to generate a figure in matlab which looks like the attached .jpeg:
So, the figure should contain an outline of the world and then 3 other figures looking at the USA, UK, and New Zealand where I can then specify individual locations in each country. How would I achieve this?
Subplots to arrange things, images to create the maps, and lines to connect them to points. To create a complicated subplot structure like that I'd suggest you check out Ben Mitch's panel class. The relevant thing you're looking for is its ability to conveniently divide up and manage subplots. Something like this
p = panel('defer');
p.pack('v', [1/5 3/5 1/5]);
p(1).pack('h',[1/5 2/5 2/5]); % top level, US and New Zealand
p(2).pack('h',[1/5 2/5 2/5]); % mid level
p(3).pack('h',[1/5 3/5 1/5]); % bottom level
p(2,2).select();
image(world_image);
p(1,3).select();
image(new_zealand_image);
p(1,3).select();
image(usa_image);
p(1,1).select();
image(uk_iamge);
Then add a few line commands to show where the submaps link to. Note that I haven't gotten a chance to test the above code yet, but will when I get to work. I can't remember offhand if it likes the 'h' argument within the child panels.

Sigmaplot: How to scale x-axis for correctly displaying boxplots

I want to display overlapping boxplots using Sigmaplot 12. When I choose the scale for the x-axis as linear then the boxes do indeed overlap but are much too thin. See figure below. Of course they should be much wider.
When I choose the scale of the x-axis to be "category", then the boxes have the right width, but are arranged along each single x-value.
I want the position as in figure 1 and the width as in figure 2. I tried to resize the box in figure 1 but when I choose 100% in "bar width" than it still looks like Figure 1.
many thanks!
okay, I found the answer myself. In Sigmaplot, there is often the need to prepare "style"-columns, for example if you want to color your barcharts, you need a column that holds the specific color names.
For my boxplot example I needed a column that has the values for "width". These had to be quite large (2000) in order to have an effect. Why ? I have no idea. First I thought it would be because of the latitude values and that the program interprets the point as "1.000"s, but when I changed to values without decimals, it didn´t get better.
Well, here is the result in color.
Have fun !