I have a problem with the interactive plots.
using Interact, Vega, Distributions
#manipulate for μ in -10:10, σ = 0.1:0.5:5.1
x = -20:0.01:20
lineplot(y = pdf(Normal(μ, σ), x), x=x)
end
This is my result, isn't the widget for manipulate de parameters:
Related
So time ago i asked the same question here and someone answered just what i wanted!
Using fit(histogram...) and weights you can do it! (just like the picture below).
julia> using StatsBase, Random; Random.seed!(0);
julia> x1, x2 = rand(100), rand(100);
julia> h1 = fit(Histogram, x1, 0:0.1:1);
julia> h2 = fit(Histogram, x2, 0:0.1:1);
julia> using Plots
julia> p1 = plot(h1, α=0.5, lab="x1") ; plot!(p1, h2, α=0.5, lab="x2")
julia> p2 = bar(0:0.1:1, h2.weights - h1.weights, lab="diff")
julia> plot(p1, p2)
The problem is i can't use fit, i need to use Histogram(...). And this one doesn't have .weights.
How can i do this using Histogram ?
This is what i'm using:
using Plots
using StatsBase
h1 = histogram(Group1, bins= B, normalize =:probability, labels = "Group 1")
h2 = histogram(Group2 , bins= B, normalize =:probability, labels ="Group 2"))
Technically there is no Histogram function in any common Julia package; perhaps you mean either the Histogram (capital h) type provided by StatsBase, or the histogram (lowercase h) function provided by Plots.jl? In either case though, the answer is "you can't".
If you mean histogram from Plots.jl there is unfortunately no practical way to access that underlying data. If you mean Histogram from StatsBase on the other hand, that only works with fit (it's a type, not a function that can be used on its own).
There are other histogram packages though if for any reason you cannot or do not want to use StatsBase and fit, including FastHistograms.jl and NaNStatistics.jl, both of which are additionally somewhat faster than StatsBase for simple cases. So, for example
using NaNStatistics, Plots
a,b = rand(100), rand(100)
dx = 0.1
binedges = 0:dx:1
aw = histcounts(a, binedges)
bw = histcounts(b, binedges)
bar(binedges, aw-bw, label="difference", bar_width=dx)
I am trying get familiar with packages in Julia like Interpolations.jl and Plots.jl. The contour plot in Julia using the GR backend is very simple as shown in the link:
https://docs.juliaplots.org/latest/examples/gr/#contours
And to carry out interpolation using Interpolations.jl:
https://github.com/JuliaMath/Interpolations.jl/blob/master/doc/Interpolations.jl.ipynb
I tried to make a contour plot using an interpolated function as following:
using Interpolations
using Plots
gr()
xs = 1:5
ys = 1:8
g = Float64[(3x + y ^ 2) * abs(sin(x) + cos(y)) for x in xs, y in ys]
gitp_quad2d = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
xc = 1:0.1:5
yc = 1:0.1:5
p1 = contour(xc, yc, gitp_quad2d, fill=true)
plot(p1)
However this gives a plot without any contour curves on it, with a message saying "Arrays have incorrect length or dimension.". But the contour function seems to accept, as in the link above, arbitrary x, y arrays and a function of x, y, and returns a contour plot. This should not give rise to any dimension problems. What is wrong with the code?
[Edit]
using Interpolations
using Plots
gr()
xs = 1:0.5:5
ys = 1:0.5:8
g = Float64[(3x + y ^ 2) for x in xs, y in ys]
f(x, y) = (3x + y ^ 2)
g_int = interpolate(g, BSpline(Quadratic(Line(OnCell()))))
gs_int = scale(g_int, xs, ys)
xc = 1:0.1:5
yc = 1:0.1:5
println("gs_int(3.2, 3.2) = ", gs_int(3.2, 3.2))
println("f(3.2, 3.2) = ", f(3.2, 3.2))
p1 = contour(xs, ys, gs_int(xs, ys), fill=true)
p2 = contour(xc, yc, f, fill=true)
plot(p1, p2)
The result:
The interpolation seems to work fine but the result from the contour plot doesn't seem to convey the same message.
You need to specify at what points you want the interpolation to happen - otherwise you will just get the input resolution of the interpolated object, which is different from that of the new xc and yc (try size(gitp_quad2d). There isn't a recipe built into Plots for doing that automatically on the x and y inputs.
Try
contourf(xc, yc, gitp_quad2d[xc, yc])
EDIT: updated to reflect update of question
On your plot, the reason you have the contour looking strangely is that your interpolated matrix is transposed relative to the x and y variables. The transposition expected of contour/heatmaps is always a discussion in plotting (should it be the same as matrices, as a normal plot or as an image? - see this issue for a good discussion https://github.com/JuliaPlots/Makie.jl/issues/205). Anyway, transposing it back will help (either p1 = contourf(xs, ys, gs_int(xs, ys)') or p1 = contourf(xs, ys, gs_int(xs, ys), transpose = true)
When using plot.surface() of the R fields package I need to change the "method" parameter of the contour() function from its default setting of "flattest" to "simple".
The contour() function is inside of plot.surface().
The plot.surface() documentation says you can pass additional parameters to two other functions appearing in plot.surface(), but no mention of how to pass parameters to contour().
I need to do this because the contour lines in my plot come out straight, resulting in no numbers on the lines. I think I could get numbers to appear on my contour lines if I can change the contour method from "flattest" to "simple" or "edge".
Here is the contour image:
The code used to generate the image:
inMat <- mat_Qe
surface <- list(x = xtick_labs,
y = ytick_labs,
z = inMat)
plot.surface(surface, type = "C",
xlab = "Mean factory efficiency (kL Ethanol / MT Root)",
ylab = "Mean farm cost (lcu / MT Root)", labcex = 1, col = mapPalette(45))
title(main = "Equilibrium Quantity Map (MT / day)", cex.main = 1)
Sorry it's not reproducible, but I think reproducibility is not really necessary in this case. I just need someone to tell me how to pass the method parameter to contour() inside of plot.surface().
Modifying the example in ?plot.surface, the following will pass method to contour. If you run plot.surface you'll see that the ellipses (...) are given to contour if type = "c" although this does not seem to be stated in the documentation. Note this is lowercase c, not C. With capital "C" argument are not passed to contour, but to image.plot.
library("fields")
# Toy data
x <- seq( -2,2,,80)
y <- seq( -2,2,,80)
z <- outer( x,y, "+")
obj <- list(x=x, y=y, z=z)
# Pass method to contour when type = "c"
plot.surface(obj, col="red", type="c", method = "simple")
plot.surface(obj, type="c", col="red", method = "edge")
I was wondering if Julia has an easily already built in capability to pass arguments meant for a function in a function?
For example,
I'm working with Gadfly but I want to create a function that makes a specific plot, let's say one that does a line graph with the plots pointed already.
So for a working example
using Gadfly, Random
Random.seed!(50)
x = randn(10)
y = 10 * x .+ 2 .+ randn(10)/10
function dummy1(x, y; plotOptionsToPass...)
plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
end
And I want to be able to pass in all different types of Gadfly plot options such as
dummy1(x, y; Theme(panel_fill = nothing))
so that it makes the dummy1 function turn into something like
plot(x = x, y = y, Geom.point, Geom.line; Theme(panel_fill = nothing))
without me having to actually prespecify all of the types of options Gadfly allows plot() to take.
Not sure what you are after, but maybe it helps to see that you can define a new function inside dummy1 and return it. The retruned fucntion will use less arguments. dummy1 becomes a drawing function 'constructor'.
function dummy1(;plotOptionsToPass...)
function foo(x, y)
plot(x = x, y = y, Geom.point, Geom.line; plotOptionsToPass...)
end
return foo
end
# create new drawing function
new_artist = dummy1(Theme(panel_fill = nothing))
# draw something
new_artist(x, y)
I need help in customizing the smoothing to my time series data. The code below smooths the data using sm.regression and approx functions, but the degree of smoothing is not user controlled, i.e. By changing function parameters I want the ability to control whether the smoothed curve follows the underlying data more closely or less closely.
find.extrema <- function(x)
{
if(is.xts(x)) {
y = as.vector( Cl(x) )
} else {
y = x
}
n = len(y)
t = 1:n
h = h.select(t, y, method = 'cv')
temp = sm.regression(t, y, h=h, display = 'none')
mhat = approx(temp$eval.points, temp$estimate, t, method='linear')$y
#mhat = y #to exactly match underlying data
return (mhat)
}
Any help would be appreciated.
Thanks.
There are not many questions regarding the sm package. Perhaps it is not widely used nowadays, but I still remember playing with it a lot when doing my MRes degree.
You can't control smoothness because you are using cross-validation for auto-selection of smoothing parameter. Just get rid of the h.select line and pass h as an argument of your function.
find.extrema <- function(x, h = NULL)
{
if(is.xts(x)) {
y = as.vector( Cl(x) )
} else {
y = x
}
n = len(y)
t = 1:n
## if not given, do auto-selection from data
if (is.null(h)) h = h.select(t, y, method = 'cv')
temp = sm.regression(t, y, h=h, display = 'none')
mhat = approx(temp$eval.points, temp$estimate, t, method='linear')$y
#mhat = y #to exactly match underlying data
return (mhat)
}
The whole point of sm package on kernel smoothing and / or kernel density estimation, is the cross-validation part. If you don't utilize it, you can just use ksmooth from R base for Nadaraya-Watson kernel estimator. You may read more about it from Scatter plot kernel smoothing: ksmooth() does not smooth my data at all. I did made a comparison with sm.regression there.