Pie with legend - matlab

I want to add legend to matlab pie instead of just putting the names close the pie itself. That is, I want names to be displayed in a box, and the percentage to be displayed close to the pie (as it usually is). But if I simply add a legend, as follows, it will be not attached to the patches information:
pieH=pie([.3,.4,.3]);
legend({'Leg1','Leg2','Leg3'},'location','EastOutside');
I've tried turning the patch annotation icon display to on, as follows, but it didn't work:
set(get(get(pieH(1),'Annotation'),'LegendInformation'),'IconDisplayStyle','on')
set(get(get(pieH(3),'Annotation'),'LegendInformation'),'IconDisplayStyle','on')
set(get(get(pieH(5),'Annotation'),'LegendInformation'),'IconDisplayStyle','on')
I also tried passing the patched handles, as it says in the help, also with no effect:
legend([pieH(1:2:end)],{'Leg1','Leg2','Leg3'},'location','EastOutside');
Edit
I was using matlab handle graphics version 2, and it seems that this combinations does not work yet at new graphics version. Since it is a matlab bug and this topic led me to the discovering, I am closing it. But if someone, by chance, already had this issue and knows how to workaround please let me know.

here's a way to make it work:
X=[100 200 300];
h=pie(X);
legend(h(1:2:end), 'Small', 'Medium', 'Large','location','EastOutside');

Related

Plots all red and green

I seem to have made some settings change where all of my MATLAB plots (plot, plot3, surf, etc) show up in this red/green color scale:
I searched around forums and the MATLAB user guide but am not finding anything. It's not a huge deal, as I can enter code to fix this on each plot, but I shouldn't have to.
How to fix this?
Check startup.m you seem to have set your default colormap to autumn or something. Just put it back to either jet (pre 2015a), or parula.
I don't know about the whole settings. These two commands can reset a figure properties (including its colormap) to its default values:
h=gcf;
reset(h)
This way you assign a handle to the last figure you have. Then, the second command resets its options to default values.
Update after Adrian's comment: Check out this page: Default Property Values It clearly shows how is it possible to regenrate the problem you have and you might need use the remove option for removing user-defined default value. There is a factory defined colormap accessible by the function: get(groot,'factoryFigureColormap'). This might help you too.

Bad clarity using export_fig

I am using Export_fig to export figs from matlab, I am getting very good plots generally. But when i add some textboxs and arrows in the fig. the clarity is pathetic.
I use the -transparent property, which does not work either.
export_fig('path', '-pdf','-transparent')
Anyone knows what is happening here. Normally this works very good, only when the text is added it acts like this way. Not sure if it's a glitch in the code or if i am doing anything wrong.
Note: I added the text and arrows with insert option on the menu bar.
If you are not set on using Export_fig for other reasons, consider using MATLAB's built-in print function and increase the resolution with the -r attribute
print('path', '-dpdf', '-r300')
where 'path' is the desired 'path\filename.pdf', '-dpdf' sets the file type, and '-r300' sets the resolution to 300 dpi, also try '-r0' for screen resolution, or higher, till you get what you want.
I'm not sure what the '-transparent' argument is used for but there is probably an equivalent you can find in the documentation.
https://www.mathworks.com/help/matlab/ref/print.html

How to prevent the fill command in MATLAB from creating boxes without "corners"

I am currently using the fill command in MATLAB to create a graph of boxes that were created using the 'fill' command (the actual code is based off this StackOverflow Question.
My issue is that the boxes that I create do not have "corners." I am attaching a PNG that illustrates the issue. Note that you have to look a little carefully since the image was heavily rendered, though in this example my arrows also look weird since they don't have edges either)
I am wondering if anyone has an idea of what might be going wrong? The boxes appear this way immediately after I use the fill command, which has the following structure:
fill(X,Y,MyFaceColor,'FaceAlpha',0.5,'EdgeColor', MyEdgeColor,'LineStyle','','LineWidth',box_line_width,'EdgeAlpha',1)
The function fill appears to leave space for corner markers if they are not explicitly defined. Hence, calling fill with the marker property will solve your problem. However, since markers and linewidths seem to work on different scales, you will have to play around with the marker size to get smooth edges.
Example:
fill(X,Y,'r','FaceAlpha',0.5,'EdgeColor', 'k',...
'LineWidth', 5,'EdgeAlpha',1 , 'marker', '.', 'markersize', 15)

How to print figure to clipboard by PRINT function with the quality identical to 'Edit-->Copy Figure' option?

Is there any way to print the figure to the clipboard so that the quality is identical to what the Edit-->Copy Figure option provides?
I used to save the figure to powerpoint file by using saveppt.m obtained from Matlab Central. It worked well until yesterday. I noticed that the stored image quality was somehow degraded. I tried to re-generate some ppt slides with exactly the same script and the same source data, but the new slides are simply of worse quality.
I investigated into this problem a little bit and discovered that when the figure is copied to clipboard by running print -dmeta, the image in the clipboard is already degraded, while if I use the Edit-->Copy Figure option in the figure window, I get the image as clear as the original image in the figure window.
Following is an example for your reference. I copied the image from a figure to the clipboard by two different methods, and paste it to Microsoft Paint program, and cut a piece of it to show below:
The image using print -dmeta:
The image using Edit-->Copy Figure:
If you compare the Xtick label '50', you may see that the image from Edit-->Copy Figure is smoother.
At the beginning I thought it was a problem of the resolution, but setting -rN to change the resolution does not seem to resolve my problem, at least not for N<=300.
Thank you for your help.
The short answer... Use the same function invoked in the callback for that menu item:
editmenufcn(gcf,'EditCopyFigure');
The longer answer... How exactly did I find this? You can look at my previous answer to a related question about reproducing what is done by a File menu option. The concept is the same, just for a different figure menu. For example, this will find the callback you want for the currently active figure window:
>> hCopyFigure = findall(gcf,'Label','Copy &Figure'); %# Handle for the "Copy
%# Figure" menu item
>> get(hCopyFigure,'Callback') %# Callback invoked when that item is selected
ans =
editmenufcn(gcbf,'EditCopyFigure')
The function EDITMENUFCN is another one of those sparsely documented functions, but looking through the code (by typing edit editmenufcn.m) shows that it either invokes Java (if you're on a Mac) or the undocumented function UIMENUFCN.
I think I found the answer myself. Using print -dmeta -painters to specify the renderer resolves my problem.
In File-->Preference-->Figure Copy Template-->Copy Option I noticed there are 3 options:
Metafile
Preserve information
Bitmap
I found that if I select 1, the Edit-->Copy Figure outputs the same image as print -dmeta. So I kind of confirmed the information I need is in the Preserve information option. A quick google search led me to the discussion about the potential difference of the applied renderer, and eventually I confirmed that using painters will print the image to the clipboard in the way I wanted.
The image in the question seems to be generated by the renderer zbuffer and painters, respectively. I still don't know why the default renderer of paint -dmeta changes, though.

In MATLAB, how do I change the background color of a subplot?

I'm trying to change the background color of a single subplot in a MATLAB figure.
It's clearly feasible since the UI allows it, but I cannot find the function to automate it.
I've looked into whitebg, but it changes the color scheme of the whole figure, not just the current subplot.
(I'm using MATLAB Version 6.1 by the way)
You can use the set command.
set(subplot(2,2,1),'Color','Red')
That will give you a red background in the subplot location 2,2,1.
I know you mentioned that you are using MATLAB 6.1, but it bears mentioning that in the newer versions of MATLAB you can specify additional property-value pair arguments in the initial call to SUBPLOT, allowing for a more compact syntax. The following creates an axes with a red background in the top left corner of a 2-by-2 layout:
subplot(2,2,1,'Color','r');
I'm not certain in which version of MATLAB this syntax was introduced, since the release notes going back to Version 7 (R14) don't seem to mention it.
I've not used Matlab in several years, but I think it might well be the whitebg method called after the subplot declaration, similar to the way in which you would set a title.
subplot(3, 2, 4), hist(rand(50)), whitebg('y');