Pluto cell warning about density and StatsPlots, not recognizing earlier

Hi All,

Still very new to Julia and Pluto. I’ve encountered a strange warning in a cell regarding plotting calls that involve seriestype density. The warning is:

seriestype density has been moved to StatsPlots. To use: Pkg.add("StatsPlots"); using StatsPlots

However, in the first cell of the notebook, I do call using StatsPlots and in the relevant code I am making specific calles to StatsPlots.density.

The whole notebook can be found at https://github.com/wccarleton/neanderthalsinarabia/tree/master/Src/Julia

And the cell issuing the warning contains the following (still messy work in progress):


begin
	cols = ["PC1", "PC2", "PC3"]
	plots = []
	l = fill((label = :º, blank = false),(3,3))
	sho = true
	for j in 1:3, k in 1:3
		if j == 1
			sho = ["neanderthal" "sapiens"]
		else
			sho = nothing
		end
		if isequal(j,k)
			#l[j,k] = (label = :_, blank = true)
			p = StatsPlots.density(levallois_proj_df[:,cols[j]], 
							levallois_proj_df[:,cols[k]], 
							group = levallois_proj_df.Sp,
							color = ColorSchemes.mk_15[[5 6]],
							fill=(0, .25, ColorSchemes.mk_15[[5 6]]),
							linewidth = 2,
							legend = true,
							lab = sho)
			Plots.annotate!(p, [((0.9, 0.9), (cols[j], 10, :white, :center))])
			push!(plots, p)
		elseif j < k
			#l[j,k] = (label = :_, blank = true)
			species = ["sapiens","neanderthal"]
			nspecies = length(species)
			grads = []
			for c in 5:6
				r,g,b = (ColorSchemes.mk_15[c].r, 
						ColorSchemes.mk_15[c].g, 
						ColorSchemes.mk_15[c].b)
				col1 = RGBA(r, g, b, 0)
				col2 = RGBA(r, g, b, 1)
				gradient = Plots.cgrad([col1, col2])
				push!(grads, gradient)
			end
			df = levallois_proj_df[isequal.(levallois_proj_df.Sp, species[1]),:]
			kxy = KernelDensity.kde((df[:,cols[j]], df[:,cols[k]]))
			p = Plots.contour(kxy.x, kxy.y, kxy.density, fill = true, levels = 10, color=grads[1],legend=false)
			#for j in 2:nspecies
			df = levallois_proj_df[isequal.(levallois_proj_df.Sp, species[2]),:]
			kxy = KernelDensity.kde((df[:,cols[j]], df[:,cols[k]]))
			Plots.contour!(p, kxy.x, kxy.y, kxy.density, fill = true, levels = 10, color=grads[2],legend=false)
			#end
			push!(plots, p)
		else
			push!(plots, Plots.scatter(levallois_proj_df[:,cols[j]],
							levallois_proj_df[:,cols[k]],
							color=ColorSchemes.mk_15[[5 6]],
							markersize=2, 
							group = levallois_proj_df.Sp,
							markerstrokewidth = 0,
							lab = nothing))
		end
	end
	P = Plots.plot(plots..., layout = l)
end

Any ideas would be very welcome. The warning is issued once for each iteration of the loop (9 times, corresponding to 9 cells in a matrix of plots).

I think this was also noted here and tracked to an upstream issue in Plots.jl if you want to follow there :+1:t5:

In the meantime, if you feel like checking out a different plotting library, Makie makes it pretty easy to build these kinds of plots up (and is also just fun to use):

using CairoMakie, CSV, DataFrames
using Makie.Colors
const kde = Makie.KernelDensity.kde
COLORS = parse.(Colorant, [:lightsalmon, :cornflowerblue])

function pair_plot!(fig, df; color=:blue)
    n = length(names(df))
    data = Array(df)
    for j ∈ 1:n, i ∈ 1:n
		ax = fig[i, j]      
		if i == j
			k = kde(data[:, i])
			kx, kd = k.x, k.density
			lines!(ax, kx, kd; linewidth=3, color)
			band!(ax, kx, zero(kx), kd; color=(color, 0.5))
			# density!(ax, data[:, i]; color) # "non-outlined" version
		elseif i > j          
			scatter!(ax, data[:, j], data[:, i]; color)
		else
			Z = kde((data[:, i], data[:, j]))
			n_levels = 4
			levels = compute_levels(Z.density, n_levels)
			colormap = cgrad(range(color, colorant"white", n_levels), alpha=0.5)
			contourf!(ax, Z; levels, colormap)
			contour!(ax, Z; levels, color)
		end
    end
end

# Number of levels `n` to show in contour plots
compute_levels(A, n) = reverse(
	range(maximum(A), step=-maximum(A)/(n+1), length=(n+1))
)

df_all = let
	N = 1_000
	df = DataFrame(randn(N, 3), :auto)
	df.grp = repeat(["groupA", "groupB"], N ÷ 2)
	df[df.grp .== "groupA", Not(:grp)] .+= 1.0
	df
end
fig = Figure()

# Global stuff
params = names(df_all[:, Not(:grp)])
n = length(params)
gdf = groupby(df_all, :grp)
lower, upper = [], [] # Lower and upper triangle for linking axes later
elems = MarkerElement[] # Legend markers
elem_labels = String[] # Will update legend in same order as gdf groups

# Set up grid 
for j ∈ 1:n, i ∈ 1:n
	ax = Axis(fig[i, j])   
	if i < j
		hidedecorations!(ax; grid=false)
		push!(upper, ax)
	end
	i > j && push!(lower, ax)
	i == j && hideydecorations!(ax)
	(i == j || i > j) && i != n && hidexdecorations!(ax, grid=false)
	i > j && 1 < j < n && hideydecorations!(ax, grid=false)
 end

# Plot
for (((label, ), df), color) ∈ zip(pairs(gdf), COLORS)
	pair_plot!(fig, df[:, Not(:grp)]; color)
	push!(elems, MarkerElement(; marker='◇', color, strokecolor=color))
	push!(elem_labels, label)
end

# Shared legend
Legend(fig[1, end+1], elems, elem_labels;
	halign = :left,
	valign = :top,
	markersize = 20,
	markerstrokewidth = 1,
)

# Link up and square up
for (i, param) ∈ enumerate(params)
	Label(fig[i, i], param;
		halign = :right,
		valign = :top,
		padding = (0, 10, 0, 10),
		tellwidth = false,
		tellheight = false,
	)
end
linkxaxes!(filter(x -> x isa Axis, fig.content)...)
linkyaxes!.(lower..., upper...)
colsize!.(Ref(fig.layout), 1:n, Ref(Aspect(1, 1.0)))
colgap!(fig.layout, 5)
rowgap!(fig.layout, 5)
resize_to_layout!(fig)

fig

1 Like

Thanks!