Julia: Plot matrix with Gadfly.jl - julia

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.

Related

Colorgradient not displaying properly on StatsPlots corrplot

I am trying to obtain a corrplot of some three-dimensional data array using Julia. The StatsPlots documentation includes the following example of a corrplot:
M = randn(1000,4)
M[:,2] .+= 0.8sqrt.(abs.(M[:,1])) .- 0.5M[:,3] .+ 5
M[:,3] .-= 0.7M[:,1].^2 .+ 2
corrplot(M, label = ["x$i" for i=1:4])
However, when I try to run the same script, I obtain flat histograms (no color gradient):
I generated the previous figure with the following script:
using StatsPlots
gr()
M = randn(1000,4)
M[:,2] .+= 0.8sqrt.(abs.(M[:,1])) .- 0.5M[:,3] .+ 5
M[:,3] .-= 0.7M[:,1].^2 .+ 2
corrplot(M, label = ["x$i" for i=1:4])
savefig("corrplot_example.png")
I am not sure what I am doing differently. My Julia version is 1.3.1, and my StatsPlots version is 0.14.6.

How can you make a stacked area / line chart in Julia with Plots.jl?

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.

Plot(...) as an output instead of a plot in Julia

with Julia 1.0.3
following this tutorial for plotting with Gadfly (it's from 2015: it might a bit old)
I used the following code:
using RDatasets, Gadfly, Cairo, Plots
sleep = dataset("lme4", "sleepstudy");
plot(sleep, x = "Days", y = "Reaction", Geom.point, Geom.smooth)
and got the following output: Plot(...) instead of a plot. Why am I not seeing a real plot instead
Here is the output of the following command: typeof.(Base.Multimedia.displays):
3-element Array{DataType,1}:
TextDisplay
IJulia.InlineDisplay
Gadfly.GadflyDisplay

how to use xticks in Gadfly spy function

I'm using spy function from Gadfly package to plot a heatmap or a matrix of values.
and i want to show for each column a specific string but i cannot find the correct syntax
plt = Gadfly.spy(mtx, x= ["a","b","c","d"]) #suppose mtx has 4 columns
thanks!
Per the original request
using Gadfly
parameters = ["ξ₁", "η₁", "ξ₂", "η₂"]
spy(rand(4,4), Scale.y_discrete(labels = i->parameters[i]), Scale.x_discrete,
Guide.ylabel("Parameters"), Guide.xlabel("Mode"))

How to plot StatsBase.Histogram object in Julia?

I am using a package(LightGraphs.jl) in Julia, and it has a predefined histogram method that creates the degree distribution of a network g.
deg_hist = degree_histogram(g)
I want to make a plot of this but i am new to plotting in Julia. The object returned is a StatsBase.Histogram which has the following as its inner fields:
StatsBase.Histogram{Int64,1,Tuple{FloatRange{Float64}}}
edges: 0.0:500.0:6000.0
weights: [79143,57,32,17,13,4,4,3,3,2,1,1]
closed: right
Can you help me how I can make use of this object to plot the histogram?
I thought this was already implemented, but I just added the recipe to StatPlots. If you check out master, you'll be able to do:
julia> using StatPlots, LightGraphs
julia> g = Graph(100,200);
julia> plot(degree_histogram(g))
For reference, the associated recipe that I added to StatPlots:
#recipe function f(h::StatsBase.Histogram)
seriestype := :histogram
h.edges[1], h.weights
end
Use the histogram fields .edges and .weights to plot it e.g.
using PyPlot, StatsBase
a = rand(1000); # generate something to plot
test_hist = fit(Histogram, a)
# line plot
plot(test_hist.edges[1][2:end], test_hist.weights)
# bar plot
bar(0:length(test_hist.weights)-1, test_hist.weights)
xticks(0:length(test_hist.weights), test_hist.edges[1])
or you could create/extend a plotting function adding a method like so:
function myplot(x::StatsBase.Histogram)
... # your code here
end
Then you will be able to call your plotting functions directly on the histogram object.

Resources