matlab stem parametres - matlab

I am using "stem" command to plot discrete values into figure. I dont know how to remove the line connecting the points to the x-axis. I found a lot of parametres that changes the shape and color of the line but none that would remove it.
stem(X, Y)
Is there a way how to do this or am I using wrong command? Plot and bar is not what Im looking for...
I somewhere saw this:
stem(X, Y, 'none')
but it doesnt work.

t = linspace(-2*pi,2*pi,10);
h = stem(t,cos(t),'LineStyle','none');

Related

Matlab Polarplot() and surface color

I am a bit struggling with my polar plot. I am playing with strikes and dips, and for each pair of those, an "intensity". I'd like to plot this surface/contourf/whatever function on my polarplot. I cannot find the handle to do so. Dpp2 contains the intensity value for a given theta and rho/ strike and dip.
xTmp = (0:4:360);
yTmp = (0:22.5:90);
[strike,dip]= meshgrid(deg2rad(xTmp),deg2rad(yTmp));
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
figure('name', 'COLD');
polarplot([0 360],[0 90]);
s = surf(strike2, dip2, DPp2);
polarplot(s);
colormap
I've tried something like that, which obviously doesn't work.
cheers,
Flo
As far as I know there is no way of creating a surface plot directly in a polarplot.
One workaround is to manually create your polar axis plot. You can find an example here.
Another workaround would be to use
polarscatter to create a scatter plot (which looks simmilar in case you have a tight grid) Have a look at this.
Because you mentioned the handle: In case you want a handle to the axes have a look at polaraxes from here.
The polar scatter wasn't working, so I tried another function, which seems to work according to this page: https://fr.mathworks.com/matlabcentral/answers/95796-how-do-i-create-a-contour-plot-in-polar-coordinates
I am note quite there yet, the contour map isn't "wrapped" around my polar plot, but so far it's compiling. If anyone has an idea on how to superimpose the contour map onto the polar plot ?
dip2 = rad2deg(dip);
strike2 =rad2deg(strike);
h = polar([0 360],[0 90]);
hold on;
contourf(strike2,dip2,DPp2);
% Hide the POLAR function data and leave annotations
set(h,'Visible','off')
% Turn off axes and set square aspect ratio
axis off
axis image

Semi-transparent markers on log-log scatter plot

I have been trying to get semi-transparent circle-shape markers on a log-log scatter plot. MATLAB's own scatter plot function doesn't produce semi-transparent markers (see comments below), so I am using this wonderful tool scatter_patches from Central File Exchange and it works very well:
However, I am having issues adapting this code for a log-log scatter plot. In order to obtain a log-log scatter plot with this program, I need to fix two things:
1 . Change the axes to log scale. This can be achieved by adding following after line 61:
set(cax, 'XScale', 'log');
set(cax, 'YScale', 'log');
This produces correct log scale and grid points.
2 . However, after I fixed 1., the size of the markers was not uniform in my plot. On a log plot, the lower-value regions of the axes are zoomed-in. In the scatter_patches plot, the markers at the lower side of axes are bigger while they are smaller towards the higher-value side of axes. So, the marker size is not the same on the entire plot. I tried using log10(cSize) instead of cSize on lines 221-222, but this only slimmed the difference between the largest and smallest marker sizes. It didn't yield the same size markers on the plot.
If anyone has an idea of what I might be missing here, please share.
Any help would be greatly appreciated.
Since Matlab R2014b things got really easy. No additional functions from file exchange are required, just some undocumented features. The basic idea is to get the hidden handle of the markers and apply a value < 1 for the last value in the EdgeColorData to achieve the desired transparency.
Here we go:
%// example data
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
%// plot scatter, get handle
h = scatter(x,y);
drawnow; %// important
%// get marker handle
hMarkers = h.MarkerHandle;
%// get current edge and face color
edgeColor = hMarkers.EdgeColorData
faceColor = hMarkers.FaceColorData
%// set face color to the same as edge color
faceColor = edgeColor;
%// opacity
opa = 0.3;
%// set marker edge and face color
hMarkers.EdgeColorData = uint8( [edgeColor(1:3); 255*opa] );
hMarkers.FaceColorData = uint8( [faceColor(1:3); 255*opa] );
In the event it helps, I have written a collection of Matlab functions for creating semi-transparent markers in plots and legends. The files are available from MATLAB Central as the MarkerTransparency package. A few examples of how to use these functions are included in the download and there is also a Wiki on GitHub. A major benefit of this package is it enables the user to have the semi-transparent markers also appear in the legend. These functions have not been tested for scatter plots but might be easily adapted to meet your needs.
On line 192 change the line with this one:
hh(end+1) = patch( exp(cSize * sin(patchSpec) / ptsPerXUnit + xs(i)), exp(cSize * cos(patchSpec) / ptsPerYUnit + ys(i)), cColor, cPatchArgs{:}); and it will work.
I just add an exp()
Do the same operation on line 222 and 223 to be sure !
Work like a charm Anindya ;).

How do I make the legend lines match the line colors and width that I have set in MATLAB script?

I'm having trouble making the lines of the legend match the line colors and width that I set in the code. I've tried many methods and read through the MATLAB help but I'm a non-programmer and I can't really understand what to do. I've also tried to figure out the solution by referring to some questions & answers on this site that were similar to my problem, then doing some trial and error to edit the legend, but as you can tell, it hasn't really worked out well, which is why I'm here. Could someone help me out please? Thank you very much!
This an image of my graph:
And my code is down below:
clf
hold on
x = [-55:0.001:65];
y = log(abs(x-31)./(sqrt(x.^2+86)));
plot(x,y);
xlabel('x axis');
ylabel('y axis');
title('Graph of function');
a= -2.7742;
fa=log(abs(a-31)./(sqrt(a.^2+86)));
plot(a,fa,'s');
b= -10.39
fb=log(abs(b-31)./(sqrt(b.^2+86)));
plot(b,fb,'o');
bday=31
vertaxis= [-12:0.1:2]
plot(bday,vertaxis,'LineWidth',2,'color','red');
horizontalaxis=[-55:0.1:65]
horizontal=0
plot(horizontalaxis,horizontal,'LineWidth',2,'color','m');
plot([-55 -10.39],[-12 -12],'LineWidth',10,'color','c');
plot([-10.39 31],[-12 -12],'LineWidth',10,'color','y');
plot([31 65],[-12 -12],'LineWidth',10,'color','g');
legend('function','local maxima','Inflection point','vertical asymptote','horizontal asymptote','concave up','concave down','concave down','Location','Best');
Substitute the following lines:
horizontalaxis=[-55:0.1:65]';
horizontal=zeros(length(horizontalaxis),1);
plot(horizontalaxis,horizontal,'LineWidth',2,'color','m');
vertaxis= [-12:0.1:2]';
bday=31.*ones(length(vertaxis),1);
plot(bday,vertaxis,'LineWidth',2,'color','red');
And it will be alright.
As already mentioned in the comments, the problem was, that you actually weren't plotting lines for your asymptotes, but more than a thousand single points. So your "concave" legends would appear a little late ;)
You can also see that your original plot has a dotted line and not a continuous one.
Edit:
plot([-55,65],[0,0],'LineWidth',2,'color','m');
plot([31,31],[-12,2],'LineWidth',2,'color','red');
would be enough actually. Like you did it below for the blue/yellow/green lines

Remove border around Matlab plot

Matlab is displaying a black border around a plot and I would like to remove it. I think i should be using something like:
set(Figure#,'PropertyName',PropertyValue);
But I'm not sure what PropertyName or PropertyValue should be used...
Edit:
I tried all of the suggestions including:
set(gca,'box','off');
set(gca,'xcolor','w','ycolor','w','xtick',[],'ytick',[]);
axis off;
The figure still has a black boarder and looks like this:
Edit 2:
This is a simplified package that reproduces the black box. Run the script called "runPlot". Here it is:
http://dl.dropbox.com/u/8058705/plotTest.zip
I can't figure out why the box is still visible. This might be due to the line in "plotTEC.m"
axis([-.65 .6 .25 1.32]) % hardwiring axis length since the coastline runs off of the plot
#Pursuit: If I use "plot browser" I get a recursive error....I am not familiar with the matlab plotting package but this seems strange. Does anyone else get this error? Again, thank you guys for your advise!
Does anyone have any other suggestions?
Thanks in advance!
You want to experiment with the properties of the axis. Some properties of interest.
xcolor %The color of the x-axis line and the x axis labels
ycolor %
box %'on', or 'off' indicating if one or both sides of a plot should have lines
xtick %Where to place the labels
ytick
For a completely bare plot, use:
figure
set(gca,'xcolor','w','ycolor','w','xtick',[],'ytick',[])
To set the figure background to white as well
set(gcf,'color','w')
Depending on your exact problem, you can try the 'box' property, to see how it affects your plots
set(gca,'box','on')
set(gca,'box','off')
If you want to turn off the actual plots lines but keep the plot labels then I am not aware of a simple solution. I think that I would need to remove the axes as described above, and then manually add labels using the text function.
Edit: As I just learned from this question, Plot Overlay MATLAB you can also try
axis off
axis on
Which I think is equivalent to
set(gca,'visible','off')
set(gca,'visible','on')
Edit 2:
If nothing else works, activate the "plot browser" in your figure. Use "view" --> "plot browser". From the plot browser panel, uncheck each object until you figure out which object is drawing the lines that you need to remove.
Then double click on the offending object to bring up the "property editor" panel, and mostly likely click "More properties" to view all possible properties of that object. From that view you can (hopefully) figure out what object is drawing the offending lines.
After you have figured out the object and property to edit, you can probably figure out where in the code that object is created, and set the property programmatically.
Try:
set(gca, 'Box', 'off');
Solution to remove 'gray' background in imagesc
I = imread('imgname.jpg');
[rows columns] = size(I);
posX = 100; posY = 100; %you can set any value for posX and posY - try to keep it on screen
f = figure (1);
imagesc(I);
set(gcf,'Position',[posX posY columns rows]);
set(gca,'units','pixels');
set(gca,'units','normalized','position',[0 0 1 1]);
axis off;
axis tight;
This should save the image with same size as that of the original, using imagesc.
Cheers!
set( gca , 'Visible' , 'off' );

Fixing the Radial Axis on MATLAB Polar Plots

I'm using polar plots (POLAR(THETA,RHO)) in MATLAB.
Is there an easy way to fix the range for the radial axis to say, 1.5?
I'm looking for something analogous to the xlim, ylim commands for cartesian axes. Haven't found anything in the docs yet.
this worked for me... i wanted the radius range to go to 30, so i first plotted this
polar(0,30,'-k')
hold on
and then plotted what i was actually interested in. this first plotted point is hidden behind the grid marks. just make sure to include
hold off
after your final plotting command.
Here's how I was able to do it.
The MATLAB polar plot (if you look at the Handle Graphics options available) does not have anything like xlim or ylim. However, I realized that the first thing plotted sets the range, so I was able to plot a function with radius range [-.5 .5] on a [-1 1] plot as follows:
theta = linspace(0,2*pi,100);
r = sin(2*theta) .* cos(2*theta);
r_max = 1;
h_fake = polar(theta,r_max*ones(size(theta)));
hold on;
h = polar(theta, r);
set(h_fake, 'Visible', 'Off');
That doesn't look very good and hopefully there's a better way to do it, but it works.
Simple solution is to make a fake graph and set its color to white.
fake=100;
polar(0,fake,'w');
hold on;
real=10;
polar(0,real,'w');
In case anyone else comes across this, here's the solution:
As Scottie T and gnovice pointed out, Matlab basically uses the polar function as an interface for standard plots, but with alot of formatting behind the scenes to make it look polar. Look at the values of the XLim and YLim properties of a polar plot and you'll notice that they are literally the x and y limits of your plot in Cartesian coordinates. So, to set a radius limit, use xlim and ylim, or axis, and be smart about the values you set:
rlim = 10;
axis([-1 1 -1 1]*rlim);
...that's all there is to it. Happy Matlabbing :)