Skip to content

Instantly share code, notes, and snippets.

View jkrumbiegel's full-sized avatar

Julius Krumbiegel jkrumbiegel

View GitHub Profile
@jkrumbiegel
jkrumbiegel / valign_legends.jl
Created January 28, 2022 17:04
one legend per line with valign
f = Figure()
ax = Axis(f[1, 1])
ls = map(0:3) do i
lines!(ax, 1:10, (1:10) .* i)
end
for (i, l) in zip(0:3, ls)
Legend(f[1, 2], [l], ["$i"], framevisible = false, padding = (0, 0, 0, 0),
valign = i / 3)
@jkrumbiegel
jkrumbiegel / tagged_unions.jl
Last active February 1, 2022 14:59
playing around with tagged unions
function struct_reinterpret(::Type{T}, x)::T where T
ref = Base.RefValue(x)
GC.@preserve ref unsafe_load(Ptr{T}(pointer_from_objref(ref)))
end
struct TaggedUnion
type::UInt8
bytes::NTuple{8, UInt8} # would be the max size of all types used, determined automatically
end
@jkrumbiegel
jkrumbiegel / portaudio.jl
Last active December 5, 2021 20:37
basic portaudio
using Pkg
pkg"activate @portaudio"
using PortAudio.LibPortAudio
mutable struct Audio
ptr::Ptr{Float32}
buffer::Vector{Float32} # vector reference so no problems with garbage collection
len::Int
@jkrumbiegel
jkrumbiegel / multi_archimedes_spiral_makie.jl
Created September 19, 2021 08:20
multiple archimedes spiral makie
using CairoMakie
##
s = Scene(resolution = (500, 500), camera = campixel!, show_axis = false)
o = Point2f(250, 250)
n = 5
linewidth = 10
center_offset = 0
rotation_gain = 1.5
@jkrumbiegel
jkrumbiegel / makie_axis_bracket.jl
Created September 9, 2021 08:12
makie axis bracket
function bracketlabel!(ax, label, mi, ma, axoffset, bracketwidth, labeloffset)
bracketpoints = lift(ax.finallimits, ax.scene.px_area) do lims, px_area
x = minimum(lims)[1]
pmin = Makie.project(ax.scene, Point2f(x, mi))
pmax = Makie.project(ax.scene, Point2f(x, ma))
ps = Point2f[
pmin,
pmin - Point2f(bracketwidth, 0),
@jkrumbiegel
jkrumbiegel / makie_gantt.jl
Created August 20, 2021 07:18
makie gantt bar chart
task_starts = [0, 3, 5, 10]
task_ends = [3, 6, 9, 12]
task_positions = [1, 2, 1, 2]
barplot(task_positions, task_ends, fillto = task_starts,
direction = :x)
text!("task " .* string.(1:4),
position = Point2f0.(
(task_starts .+ task_ends) ./ 2,
task_positions
@jkrumbiegel
jkrumbiegel / cleveland.jl
Created May 2, 2021 17:30
cleveland gridlayout
f = Figure()
nrows, ncols = (3, 7)
real_axes = map(Iterators.product(1:nrows, 1:ncols)) do (i, j)
ax = Axis(f[2i, j])
if i < nrows
hidexdecorations!(ax, grid = false)
end
if 1 < j < ncols
@jkrumbiegel
jkrumbiegel / subplots_fixed_size.jl
Created May 2, 2021 15:07
makielayout set figure size according to fixed width/height subplots
f = Figure()
[Axis(f[i, j], width = 300, height = 250) for i in 1:3, j in 1:3]
sz = ceil.(Int, GridLayoutBase.determinedirsize.(
Ref(f.layout),
(GridLayoutBase.Col(), GridLayoutBase.Row())))
resize!(f.scene, sz)
@jkrumbiegel
jkrumbiegel / stairs_recipe.jl
Created April 22, 2021 11:01
Basic stairs recipe
@recipe(Stairs) do scene
a = Attributes(
step = :pre, # :center :post
)
merge(a, AbstractPlotting.default_theme(scene, Lines))
end
AbstractPlotting.conversion_trait(x::Type{<: Stairs}) = AbstractPlotting.PointBased()
function AbstractPlotting.plot!(p::Stairs{<:Tuple{<:AbstractVector{<:Point2}}})
@jkrumbiegel
jkrumbiegel / integer_log_ticks.jl
Created April 21, 2021 11:39
integer log ticks
struct IntegerTicks end
MakieLayout.get_tickvalues(::IntegerTicks, vmin, vmax) = ceil(Int, vmin) : floor(Int, vmax)
lines(0.01:0.001:0.99, axis = (yscale = log10, yticks = LogTicks(IntegerTicks())))