I want to write ALOHA simulation but I plot this picture only G=0 1 2 3.What I need to add?
using Plots
A = []
for G in 0.0:3.0
S = G*ℯ^(-2G)
push!(A, S)
end
println(A)
plot(A)
Your code could be written like this, is this what you need?:
using Plots
G=0.0:0.01:3.0
A= G.*ℯ.^(-2G)
plot(G,A)
Another option is
using Plots
plot(x -> x * exp(-2x), 0, 3)
which also gives
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 would like to create a stacked area chart, similar to this for example, in Julia using Plots.
I know / suppose that you can do this if you directly use the Gadfly or PyPlot backends in Julia, but I was wondering if there was a recipe for this. If not, how can you contribute to the Plots Recipes? Would be a useful addition.
There's a recipe for something similar in
https://docs.juliaplots.org/latest/examples/pgfplots/#portfolio-composition-maps
For some reason the thumbnail looks broken now though (but the code works).
The exact plot in the matlab example can be produced by
plot(cumsum(Y, dims = 2)[:,end:-1:1], fill = 0, lc = :black)
As a recipe that would look like
#userplot AreaChart
#recipe function f(a::AreaChart)
fillto --> 0
linecolor --> :black
seriestype --> :path
cumsum(a.args[1], dims = 2)[:,end:-1:1]
end
If you want to contribute a recipe to Plots you can open a pull request on Plots, or, eg. on StatsPlots - there's a good description of contributing here: https://docs.juliaplots.org/latest/contributing/
It's a bit of reading, but very generally useful as an introduction to contributing to Julia packages.
You can read this thread in the Julia discourse forum where the question is developed in deep.
One solution posted there using Plots is :
# a simple "recipe" for Plots.jl to get stacked area plots
# usage: stackedarea(xvector, datamatrix, plotsoptions)
#recipe function f(pc::StackedArea)
x, y = pc.args
n = length(x)
y = cumsum(y, dims=2)
seriestype := :shape
# create a filled polygon for each item
for c=1:size(y,2)
sx = vcat(x, reverse(x))
sy = vcat(y[:,c], c==1 ? zeros(n) : reverse(y[:,c-1]))
#series (sx, sy)
end
end
a = [1,1,1,1.5,2,3]
b = [0.5,0.6,0.4,0.3,0.3,0.2]
c = [2,1.8,2.2,3.3,2.5,1.8]
sNames = ["a","b","c"]
x = [2001,2002,2003,2004,2005,2006]
plotly()
stackedarea(x, [a b c], labels=reshape(sNames, (1,3)))
(by user NiclasMattsson)
Other ways presented there include using the VegaLite.jl package.
I learn to program in Julia language.
I want to test which color is better, so I use the following code:
using Plots
x = 1:10; y = rand(10,2);
for i in [0 0.1 0.2]
plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i))
end
How, nothing show.
Can anyone tell me how to generate plot with loop?
P.S.
I have tried another way,
[plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)) for i in 0:0.1:1]
Nothing happened with above code.
The following code does not work either:
map(i->plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)), [0 0.1 0.2])
In a script plot is not shown unless you use display function to show it (see section Plotting in scripts here http://docs.juliaplots.org/latest/tutorial/).
You have several options here. This one is the simplest:
for i in [0 0.1 0.2]
display(plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)))
sleep(1)
end
It will plot your figures in a sequence. I use sleep(1) to make Julia pause between plotting.
Otherwise you can do:
p = [plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)) for i in 0:0.1:1]
and then plot your figures from Julia REPL like this:
julia> p[5]
will plot fifth figure. Now you do not have to use display because in REPL it will be invoked anyway as I did not use ; at the end of the line. You could write display(p[5]) to get the same effect.
Finally you can consider saving the figures to PNG file and inspecting them later. You can do it either using p array defined above like this:
foreach(i -> savefig(p[i], "fig$i.png"), eachindex(p))
or in a loop like this:
for i in [0 0.1 0.2]
p = plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i))
savefig(p, "fig$i.png)
end
I am working with a large number of observations and to really get to know it I want to do histograms using Plots.jl
My question is how I can do multiple histograms in one plot as this would be really handy. I have tried multiple things already, but I am a bit confused with the different plotting sources in julia (plots.jl, pyplot, gadfly,...).
I don't know if it would help for me to post some of my code, as this is a more general question. But I am happy to post it, if needed.
There is an example that does just this:
using Plots
pyplot()
n = 100
x1, x2 = rand(n), 3rand(n)
# see issue #186... this is the standard histogram call
# our goal is to use the same edges for both series
histogram(Any[x1, x2], line=(3,0.2,:green), fillcolor=[:red :black], fillalpha=0.2)
I looked for "histograms" in the Plots.jl repo, found this related issue and followed the links to the example.
With Plots, there are two possibilities to show multiple series in one plot:
First, you can use a matrix, where each column constitutes a separate series:
a, b, c = randn(100), randn(100), randn(100)
histogram([a b c])
Here, hcat is used to concatenate the vectors (note the spaces instead of commas).
This is equivalent to
histogram(randn(100,3))
You can apply options to the individual series using a row matrix:
histogram([a b c], label = ["a" "b" "c"])
(Again, note the spaces instead of commas)
Second, you can use plot! and its variants to update a previous plot:
histogram(a) # creates a new plot
histogram!(b) # updates the previous plot
histogram!(c) # updates the previous plot
Alternatively, you can specify which plot to update:
p = histogram(a) # creates a new plot p
histogram(b) # creates an independent new plot
histogram!(p, c) # updates plot p
This is useful if you have several subplots.
Edit:
Following Felipe Lema's links, you can implement a recipe for histograms that share the edges:
using StatsBase
using PlotRecipes
function calcbins(a, bins::Integer)
lo, hi = extrema(a)
StatsBase.histrange(lo, hi, bins) # nice edges
end
calcbins(a, bins::AbstractVector) = bins
#userplot GroupHist
#recipe function f(h::GroupHist; bins = 30)
args = h.args
length(args) == 1 || error("GroupHist should be given one argument")
bins = calcbins(args[1], bins)
seriestype := :bar
bins, mapslices(col -> fit(Histogram, col, bins).weights, args[1], 1)
end
grouphist(randn(100,3))
Edit 2:
Because it is faster, I changed the recipe to use StatsBase.fit for creating the histogram.
I'm trying to plot a matrix with Gadfly, like I can do with PyPlot's matshow:
using PyPlot
p = eye(5)
p[5,5] = -1
matshow(p)
But I took a look at the docs, and found nothing. How can I do it with Gadfly?
Gadfly has a spy() function which does the same thing.
using Gadfly
p = eye(5)
p[end, end] = -1
spy(p)
You can check out the source for more information.