Add mean to boxplot in Julia - julia

In R you can add the mean to a boxplot like this:
set.seed(7)
values <- runif(100, 0, 1)
boxplot(values)
points(mean(values), col = 'red')
I would like to do the same in Julia with StatsPlots. Here is the code:
using StatsPlots
boxplot(repeat(['A'],outer=100),randn(300))
Output:
I am not sure how to add a single point (mean) to the boxplot. Is there a similar function like points? So I was wondering if anyone knows how to add the mean to a boxplot in Julia like above?

Adding context to the tip to use scatter!. The difference between scatter and scatter! already has a good explanation here.
julia> using StatsPlots
julia> using Statistics
julia> using Random
julia> Random.seed!(42)
TaskLocalRNG()
julia> val = randn(300);
julia> boxplot(repeat(['A'], outer=100), val, label = "boxplot")
julia> scatter!(['A'], [mean(val)], label = "mean")

If you want a "point" at the mean, you can use a scatter! with the means on top of the boxplots.

Related

Plotting nxn matrix as a headmap in Gadfly

Hi I am wondering how to plot a heatmap with Gadfly for a n x n matrix. From what I saw the closest thing is Geom.rect or Geom.rectbin, but I am unable to get it in the format of grid.
using Gadfly: Geom, plot
a = rand(3,3)
plot(x = 1:9, y=1:9,color = a, Geom.rectbin)
One option is to use spy():
using Gadfly
a=rand(9,9);
spy(a)

Julia: scatter plot with different colors (using either ColorGradient or other continuous color encodings)

Very simple question but I'm not very familiar with the way the ColorGradient type works so I thought I'd ask here.
Say I have some x, y data that I want to plot in the standard way with scatter(x,y) and I want to color the dots with their unique colors based on a color map determined by a color gradient (see code below):
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,30)]
g = :inferno
(source: https://github.com/JuliaPlots/ExamplePlots.jl/blob/master/notebooks/cgrad.ipynb)
How would I set up my color gradient object and then use it to map each of my (x,y) pairs to a different one of the colors in the color gradient, using the scatter() function?
My guess is something like:
x, y = (rand(10), rand(10))
using Plots
C(g::ColorGradient) = RGB[g[z] for z=LinRange(0,1,10)]
g = :blue
scatter(x,y,c=cgrad(g)|> C)
I just tried that and it works, but I don't really get why...what is with this |> C notation??
The shortest way to get the plot is:
using Plots, Colors
scatter(x,y,c=colormap("Blues",10))
Another colormap worth mentioning is to have very different colors of points:
scatter(x,y,c=distinguishable_colors(10))
The |> operator is just passing an argument to a function so you can write either f(x) or x |> f. Consider the example below:
julia> f(a,b=5) = a+b;
julia> 7 |> f
12

is there any facility to plot a line?

I have some nodes and I want to plot them. some of them connect to others. I don't know How can I plot a line in Julia ? would you please help me?
for example a line as follow:
y=2x+5
thank you
As an addition to the answer above, actually in Plots.jl it is even simpler. Just pass a function to plot like this:
plot(x->2x+5)
you typically will want to pass axis ranges which you can do like this:
plot(x->2x+5, xlim=(0,5), ylim=(5,15))
you can also plot several functions at once:
plot([sin, cos, x->x^2-1], label=["sin", "cos", "x²-1"], xlim=(-2,2), ylim=(-1,3))
The result of the last plotting command is:
Try looking at Julia's documentation about plotting as well as this tutorial found by searching Julia plots on a web search engine.
Plotting y = 2x+5 in Julia v1.1 :
using Pkg
Pkg.add("Plots") # Comment if you already added package Plots
using Plots
plotly() # Choose the Plotly.jl backend for web interactivity
x = -5:5 # Change for different xaxis range, you can for instance use : x = -5:10:5
y = [2*i + 5 for i in x]
plot(x, y, title="My Plot")

Plot colour in Julia

I am starting to learn Juliabox. I have a question about differentiating colour in Julia plot, based on feature value.
data is:
x = [2,4,3,5,3,2,1,5,6,4,3]
m = [0,0,0,1,1,0,1,1,1,0,1]
y = [23,32,43,23,12,54,34,43,56,76,34]
I want to plot points x-y and differentiate the point color based on value m.
I tried :
using PyPlot
plot(x,y,".", color=m)
and it doesn't work. Can anyone help?
Thanks,
Perhaps you want:
PyPlot.scatter(x,y,c=m)

R: Plotting one ECDF on top of another in different colors

I have a couple of cumulative empirical density functions which I would like to plot on top of each other in order to illustrate differences in the two curves. As was pointed out in a previous question, the function to draw the ECDF is simply plot(Ecdf()) And as I read the fine manual page, I determined that I can plot multiple ECDFs on top of each other using something like the following:
require( Hmisc )
set.seed(3)
g <- c(rep(1, 20), rep(2, 20))
Ecdf(c( rnorm(20), rnorm(20)), group=g)
However my curves sometimes overlap a bit and can be hard to tell which is which, just like the example above which produces this graph:
I would really like to make the color of these two CDFs different. I can't figure out how to do that, however. Any tips?
If memory serves, I have done this in the past. As I recall, you needed to trick it as Ecdf() is so darn paramterised. I think in help(ecdf) it hints that it is just a plot of stepfunctions, so you could estimate two or more ecdfs, plot one and then annotate via lines().
Edit Turns out it is as easy as
R> Ecdf(c(rnorm(20), rnorm(20)), group=g, col=c('blue', 'orange'))
as the help page clearly states the col= argument. But I have also found some scriptlets where I used plot.stepfun() explicitly.
You can add each curve one at a time (each with its own style), e.g.
Ecdf(rnorm(20), lwd = 2)
Ecdf(rnorm(20),add = TRUE, col = 'red', lty = 1)
Without using Ecdf (doesn't look like Hmisc is available):
set.seed(3)
mat <- cbind(rnorm(20), rnorm(20))
matplot(apply(mat, 2, sort), seq(20)/20, type='s')

Resources