Is there any way of annotating multiple plots using arrows with code in MATLAB? - matlab

When doing multiple plots on the same figure in MATLAB, is there any way of annotating them such that the legend entries have arrows pointing to the plots that they're named for?
Here's an example of what I have in mind. I'd like to do this using code.
Note that the MATLAB website mentions a way to do this using the annotation function. The problem with that function is that it takes x and y values (normalized for the plot) and puts the text there. Given that I am not certain where the datapoints will lie, this is unhelpful for what I want to do.
I don't mind if the text shows up at a random place, actually. What's important is to have an arrow or some way of pointing to the plot that it is referencing.

Take a look at the following Math works file exchange post:
http://www.mathworks.co.uk/matlabcentral/fileexchange/10656-data-space-to-figure-units-conversion
The code given here allows you to convert between data-space units and normalised figure units. The example given in the post seems to be doing almost exactly what you are asking.

Related

boxplots from SPSS and Matlab show different outliers and percentiles

I encountered the below problem while trying to check my ANOVA data for outliers with MATLAB. By chance I also did it in SPSS and noticed the below weird thing:
I have a vector with 5 data points (for simplicity).
1.1648 0.4254 0.6796 0.5627 0.6904
I then use the Matlab boxplot command, i.e. boxplot(test) and get this:
looks reasonable.
I then copy the same vector into SPSS (I swear it's the same). I choose 'explore' from the analyze> descriptive stats menu. While I don't think it makes a difference in this case, I choose (from the plots option): boxplots: dependents together. Then I hit OK and get this:
There are two things that seem weird to me here:
The 3rd quartile appears to be larger in Matlab (above 0.8) whereas it is <0.8 in SPSS
Matlab doesn't seem to think there is an outlier. As far as I understand the documentation on "boxplot" in Matlab, outliers are, by default, defined as more than 1.5 times the interquartile range away from the 1st or 3rd quartile. This definition is the same in SPSS as far as I can gather.
I'm probably doing something wrong somewhere but can't figure out where.
Thanks for the help!

What is the official Matlab way to plot the values of histcounts into a histogram with any normalization option?

Assume that I have an array of counts (ideally returned by histcounts). Is there an official Matlab way to plot such a histogram with all the standard normalization options available?
It seems that the best suggestion I have is to get the counts from histcounts and then plot them with bar. Something like:
edges = linspace(0,bound,nbins);
hist_c = histcounts(X,nbins);
bar(edges(1:nbins-1),hist_c);
unfortunately as far as I know it seems that using bar is really not recommended according to this link. Probably because as its obvious from the code, it seems that it moves a lot of implementation details into user code (like produces edges array manually when only needing nbins or having to know if to use 1:nbins-1 vs 2:nbins).
Furthermore, which I believe is the worst, is that it leave the user to have to implement the normalization options on its own. One may point out that histcounts can do the normalization options for you, however, it can only do them given the data matrix X. If one had an extremely large matrix X, then one would be in trouble because producing the histogram counts of X could be done on the fly (as done in this question) but the other normalization options could not be easily be done on the fly. One practice the user could try to implement each normalization option as described by the equations in the documentation but it seems extremely inefficient to have users implement this by hand. Is there a way to get access to the code that actually performs this normalization?
In reality what my question is going for is, is there an official matlab way to produce histogram having only the histogram counts? In particular hiding all the implementation details of producing the counts, normalization, binning, edges, etc?
The ideal code in my mind should look like this to the user:
histogram_counts = get_hist_count(X)
plot_histogram(histogram_counts,'Normalization',normalization)
and produces the desired histogram plot.
Related question:
https://www.mathworks.com/matlabcentral/answers/332178-how-does-one-plot-a-histogram-from-the-histogram-counts
https://www.mathworks.com/matlabcentral/answers/275278-what-is-the-recommended-practice-for-plotting-the-outputs-of-histcounts
https://www.mathworks.com/matlabcentral/answers/91944-how-can-i-combine-the-options-histc-and-stack-in-a-bar-plot-in-matlab-7-4-r2007a#answer_101295

Sympy/Matlab Plot y=mx, without any numerical value of m

I need to plot y = m*x where x ranges from, say 0 to 10. But m is a symbolic constant here, I dont want to supply a specific value.
Here's what my desired graph looks like (similar to how a class teacher would draw this):
[Consider m=a]
Sympy:
Tried doing this:
sympy.plot(m*x,(x,0,10))
but this shows the following error:
ValueError: The same variable should be used in all univariate expressions being plotted.
I cant really understand the error message, bit I am guessing it cant plot m as a (symbolic) constant in this case. Is it so? And in general, how can I do this?
Matlab:
Soon, I wanted to know if this is a limitation of sympy only, and thought maybe popular ones like matlab can do it? But with a bit of search on docs and SO, I couldnt find any. Both plot and fplot doesnt seem to cover this, they expect numerical values.
Others:
I am not acquainted with other plotting or CAS softwares, but it will be interesting to know if they support this out of the box
So, to repeat the main question, how to draw similar graphs, preferably without managing the plotting code yourself ?
The solution must be generic enough like plot to be applied to other equations.
[ The question was heavily edited from a sympy-specific question ]
Only for some functions with specific conditions you can plot thus in Maple. In Python (using matplotlib, sympy or any other packages) or Matlab you need to create code to manage that (assuming values and then replace ticks with literal ticks).

Data interpolation over a non-homogeneous surface

In my project i deal with big data surfaces.
In a certain point, i have a line across the data, and I need the values of the points of the line.
The grid is non,homogeneous, it doesnt go from n:m with fixed steps nor nothing.
Lets ilustrate!
In the figure the 2D proyection of my data can be seen. Each of the points has also other 3 data information. I defined a arbitrary red line with the form y=ax+b. a and b are known.
How can I define i.e. 50 points in the line that has not only the x and y coords (wich is straigforward) but also the interpolation of the 3 data information of each of the points around it.
I know is not an easy question but I can't seem to step forward even a bit.
PD: realize I DONT want code written for me, but the idea of how to achieve my objective.
You could use a tool like triScatteredInterp, which will triangulate the 2-d domain, then interpolate a list of points along your line. Griddata is also an option.
I have a toolbox for problems like this (of course.) It allows me to build a triangulation of the non-convex domain in the (x,y) plane. Then it can form a completely general slice through that surface, interpolating in z also as it does so. The result will be a 1-manifold, in this case a piecewise linear function along that path in (x,y,z). While those tools are not posted on the file exchange, they are available for the person willing to invest the time to learn to use them.
If the surface you describe is a completely general one in 3-d, that might be fairly complex, then you might need a CRUST based tool to define that surface triangulation. These can be found online too. Once a triangulation is available, my tools can then be used to slice them. (Sorry, I never did finish that piece.)
What I did was to define several points in the crack line and then cheack for each one of them in wich quadrilateral it is with inpoligon matlab function (no tthe fastest way but less than 2 secs).
Then I created a triangular plane in the used quadrilaterals using x,y and Z or the othre data , achieving a linear interpolation between the data.
finally i take out all the points that are 0 o Nan.

Dynamic Plotting in Gnuplot (drawnow in MATLAB)

Is it possible to create dynamic plots in Gnuplot? What I require for my purposes is that, as the data is generated through some loop, I will use gnuplot to put some marker on the x-y axis preserving the older ones. So somehow I will be able to observe the evolution of the data instead of just seeing the final batch result.
What I specially want is equivalent to "drawnow" command in MATLAB.
Although not totally related, right now I am using common lisp to generate the data in a loop and cgn in order to plot within lisp using gnuplot. (I can plot data in batch form inside common lisp using cgn which utilizes gnuplot)
Thank you very much in advance for your attention.
edit: I have a written a code in common lisp for this purpose. You can check it here :
Plotting data sequentially from emacs using Common Lisp and Gnuplot
This thread is however more general and asks dynamic plotting in gnuplot. Any suggestions are welcome.
Unfortunately it's not easy to plot single points in gnuplot, but luckily there are some simple hacks as discussed here: Plotting a Single Point with Gnuplot. The echo method discussed there will only work in a Unix environment though.
Using this with replot instead of plot in your program should hopefully give you a graph of points evolving with time that preserves the previous points.
Another way, which is what I use with python, is that I put the data points in a file. In every iteration, I add points to the file then plot with gnuplot again. It's a little ugly, but it does the job in most cases.
I'm not sure that I completely understand what you are asking, but if you're looking to add a plot to the last string you plotted (and you're using gnuplot 4.4), the following does the trick:
gnuplot> plot sin(x),cos(x) #plot sin and cos in an xterm window
gnuplot> eval GPVAL_LAST_PLOT."cos(x+pi/2.5)" #add cos(x+pi/2.5) to the current plot
Anyway, I'm not sure if that's what you're asking for as I don't use Matlab, but I hope it is.