Range and increment of data in Matlab - plot

Lets consider I am given a plot and I do not have its x and y vectors but I would like to extract them from the plot in Matlab. Also I am interested to know the increment of data (step size) in both horizontal and vertical axis(x and y axis).
I was thinking of using :
h=gca % Get current axis
X=get(h,'xdata');
Y=get(h,'ydata');
stepsize=X(2)-X(1);
But these command produce an error message that :
xdata and ydata are not accessible property of axis. Any suggestion how to find the x and y vectors for any given curve.

If I understand correctly, these are the two things you want to know:
You have a figure containing a plot of some arbitrary 2d line, whose x_vec, y_vec are unknown to you and you want to extract them from the figure\axes.
You want to get the xtick and ytick positions used in the figure you have.
The reason your code does not work, is because you're trying to access a property of the axes, whereas what you want to access is the property of the line (i.e. the curve in the plot).
To solve your first problem, you can resort to the following methods:
Manual: using the edit plot figure tool you can get to the XData and YData properties of the line, in the following manner:
Programmatic: you need to find the handle (i.e. pointer) to the line, and then use your code on that handle (and not on gca):
%// If there's only one entity (child) in the axes:
hLine = get(gca,'Children');
%// If there's more than one child:
hChildren = findobj(gca,'Type','line');
hLine = hChildren(1); %// Or any other logic you need to pick the correct line
%// Then comes your code:
xD = get(hLine,'XData'); yD = get(hLine,'YData');
For the second problem you can use gca to get XTick and YTick:
xT = get(gca,'XTick'); yT = get(gca,'YTick');
To get the step size I'd suggest simply using diff().

I'm not sure I quite understand your question. You mean get x and y data of a curve? If yes, then maybe it'll help looking into 'ginput'.
For example, picking 10 points from a figure window you can use the following command
[x,y] = ginput(10)

Related

Is there a way to create a ggplot2-version of a (x,y) = (log(lambda), coefficient)-plot from glmnet?

I haven't really played around with it, as I'm not aware of a method to add a second x-axis to the top of a ggplot, and even if I were, I wouldn't really know how to 'collect' how many parameters are still in the model as lambda increases, as shown in a plot.glmnet()
In other words, do you have any idea how to make a plot like below in ggplot? Is it even possible?
plot.glmnet with x = log(lambda), y = coefficients and top x-axis = number of parameters still left in the model, given a specific value of log(lambda)
Haven't tried anything - I'm just curious :-)

Automated labeling of logarithmic plots

I would like to automate the graph axis values for a series of plots in Stata 13.
In particular, I would like to show axis labels like 10^-1, 0, 10^1, 10^2 etc. by feeding the axis options macros.
The solution in the following blog post gives a good starting point:
Labeling logarithmic axes in Stata graphs
I could also also produce nicer labels by using "10{sup:`x'}".
However, I cannot find the solution to the following additional elements:
the range of the axis labels would run from 10^-10 up to 10^10. Moreover, my baseline is ln, so log values are 2.3, 4.6, etc. In particular, the line below which only takes integers as input:
label define expon `vallabel', replace
I would like to force the range of the axis values by graph (e.g. a particular axis runs from 10^-2 to 10^5). I understand that range() only extends axes, but does not allow to trim them.
Any ideas on either or both of the above?
This is a very straightforward output in R or Python, even standard without many additional arguments, but unfortunately not so in Stata.
Q1. You should check out niceloglabels from SSC as announced in this thread. niceloglabels together with other tricks and devices in this territory will be discussed in a column expected to appear in Stata Journal 18(1) in the first quarter of 2018.
Value labels are limited to association with integers but that does not bite here. All you need focus on is the text which is to be displayed as axis labels at designated points on either axis; such points can be specified using any numeric value within the axis range.
Your specific problem appears to be that one of your variables is a natural logarithm but you wish to label axes in terms of powers of 10. Conversion to a variable containing logarithms to base 10 is surely easy, but another program mylabels (SSC) can help here. This is a self-contained example.
* ssc inst mylabels
sysuse auto, clear
set scheme s1color
gen lnprice = ln(price)
mylabels 4000 8000 16000, myscale(ln(#)) local(yla)
gen lnweight = ln(weight)
mylabels 2 3 4, myscale(ln(1000*#)) suffix(" x 10{sup:3}") local(xla)
scatter lnprice lnweight, yla(`yla') xla(`xla') ms(Oh) ytitle(Price (USD)) xtitle(Weight (lb))
I have used different styles for the two axes just to show what is possible. On other grounds it is usually preferable to be consistent about style.
Broadly speaking, the use of niceloglabels is often simpler as it boils down to specifying xscale(log) or yscale(log) with the labels you want to see. niceloglabels also looks at a variable range or a specified minimum and maximum to suggest what labels might be used.
Q2. range() is an option with twoway function that allows extension of the x axis range. For most graph commands, the pertinent options are xscale() or yscale(), which again extend axis ranges if suitably specified. None of these options will omit data as a side-effect or reduce axis ranges as compared with what axis label options imply. If you want to omit data you need to use if or in or clone your variables and replace values you don't want to show with missing values in the clone.
FWIW, superscripts look much better to me than ^ for powers.
I have finally found a clunky but working solution.
The trick is to first generate 2 locals: one to evaluate the axis value, another to denote the axis label. Then combine both into a new local.
Somehow I need to do this separately for positive and negative values.
I'm sure this can be improved...
// define macros
forvalues i = 0(1)10 {
local a`i' = `i'*2.3
local b`i' `" "10{sup:`i'}" "'
local l`i' `a`i'' `"`b`i''"'
}
forvalues i = 1(1)10 {
local am`i' = `i'*-2.3
local bm`i' `" "10{sup:-`i'}" "'
local lm`i' `am`i'' `"`bm`i''"'
}
// graph
hist lnx, ///
xl(`lm4' `lm3' `lm2' `lm1' `l0' `l1' `l2' `l3' `l4')

Plotting straight surface with lattice::wireframe()

Assume I want to plot the following dataframe:
df <- data.frame(expand.grid(1:10,1:10),rep(10,100))
colnames(df) <- c("x","y","z")
with the lattice wireframe() function:
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE)
How do I get it to plot the given coordinates? I would assume it has something to do with having to scale/adjust the z-axis as the automatic scaling within wireframe is probably confused by all z-coordinates being equal.
This is from the help page scales section: "The most common use for this argument is to set arrows=FALSE, which causes tick marks and labels to be used instead of arrows being drawn (the default)." So just add that as a list value to 'scales':
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE,
scales=list(arrows=FALSE), zlim=c(0,10.1))
The failure of wireframe to display anything when the plotted plane is at one of the extremes seems to be at least "unexpected behavior" if not a bug. I suspect you would not see this in real data. Your use of drape doesn't make much sense since the entire data-plane plane gets displayed at the white midpoint. (Again this is probably not a problem if you have something other than this pathological example.)
Just add a zlim argument.
wireframe(z~x*y,df,colorkey=TRUE,drape=TRUE, zlim=c(0,20))

Displaying Points in Scatterplot

Currently trying to write a simple r script that when passed in 2 vectors of values would calculate some relationship between them (in the given case, r_square) and display it in a graph with the best fit line.
temp1 <- sample(20000,1367,replace=F,prob=NULL)
temp2 <- sample(20000,1367,replace=F,prob=NULL)
fit <- lm(temp1 ~temp2)
plot(temp1,temp2,ann="true")
abline(fit)
(here using sample in lack of real data).
The problem is that i'm trying to add interactivity which would display point's value (X/Y coordinates of sort) on hover.
I've managed to find a few functions that identify them by the their order in the vectors (HWidentify, identify, etc). But none of them give the actual value (x,y) so i was wondering if it's possible to print out coordinates that aren't permanent.
If you're trying to print the coordinates, you could use the labels argument to the identify() function.
identify(temp1, temp2, labels=paste(temp1, temp2, sep=","))
The HWidentify function also has a labels argument that you can set to whatever you want, using the paste function like #JeanV.Adams works similarly and then you have the hover functionability.

Gnuplot: How to remove vectors below a certain magnitude from vector field?

I have a 2D CFD code that gives me the x and y flow velocity at every point on a grid. I am currently visualizing the data using a vector field in gnuplot. My goal is to see how far the plume from an eruption extends, so it would be much cleaner if I could prevent vectors from showing up at all in the field if they fall below a certain magnitude. Does anyone have an idea how to go about this? My current gnuplot script is below. I can also modify the input file as necessary.
reset
set nokey
set term png
set xrange [0:5.1]
set yrange [0:10.1]
do for [i=0:10] {
set title 'Eruption simulation: Timestep '.i
set output 'path/FlowVel'.sprintf('%04.0f',i).'.png'
plot 'path/Flow'.sprintf('%04.0f',i).'.dat' using 1:2:3:4 with vec
}
I guess you want a kind of filtering, which gnuplot doesn't really have, but can be achieved with the following trick (taken from "help using examples" in gnuplot):
One trick is to use the ternary `?:` operator to filter data:
plot 'file' using 1:($3>10 ? $2 : 1/0)
which plots the datum in column two against that in column one provided
the datum in column three exceeds ten. `1/0` is undefined; `gnuplot`
quietly ignores undefined points, so unsuitable points are suppressed.
Or you can use the pre-defined variable NaN to achieve the same result.
So I guess you would want something like this in your case
plot "data.dat" u 1:2:($3**2+$4**2>mag_sq?$3:NaN):($3**2+$4**2>mag_sq?$4:NaN) w vector
where mag_sq is the square of your desired magnitude.

Resources