Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodePlots: 3d support #4089

Merged
merged 4 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Scratch = "1"
Showoff = "0.3.1, 1.0"
StatsBase = "0.32 - 0.33"
UnicodeFun = "0.4"
UnicodePlots = "2.6"
UnicodePlots = "2.8"
Unzip = "0.1"
julia = "1.6"

Expand Down
14 changes: 10 additions & 4 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ const _unicodeplots_attr = merge_with_base_supported([
:annotations,
:bins,
:guide,
# :grid,
:grid,
:label,
:layout,
:legend,
Expand All @@ -928,18 +928,27 @@ const _unicodeplots_attr = merge_with_base_supported([
:seriesalpha,
:seriescolor,
:scale,
:flip,
:title,
# :marker_z,
:line_z,
])
const _unicodeplots_seriestype = [
:path,
:path3d,
:scatter,
:scatter3d,
:straightline,
# :bar,
:shape,
:histogram2d,
:heatmap,
:contour,
# :contour3d,
:spy,
:surface,
:wireframe,
:mesh3d,
]
const _unicodeplots_style = [:auto, :solid]
const _unicodeplots_marker = [
Expand Down Expand Up @@ -972,9 +981,6 @@ const _unicodeplots_marker = [
]
const _unicodeplots_scale = [:identity, :ln, :log2, :log10]

# Additional constants
const _up_colormap = Ref(:none)

# ------------------------------------------------------------------------------
# hdf5

Expand Down
69 changes: 52 additions & 17 deletions src/backends/unicodeplots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const _canvas_map = (
# do all the magic here... build it all at once,
# since we need to know about all the series at the very beginning
function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
plt.attr[:warn_on_unsupported] = false
plt.o = UnicodePlots.Plot[]

has_layout = prod(size(plt.layout)) > 1
Expand All @@ -25,24 +24,26 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
yaxis = sp[:yaxis]
xlim = collect(axis_limits(sp, :x))
ylim = collect(axis_limits(sp, :y))
zlim = collect(axis_limits(sp, :z))
F = float(eltype(xlim))

# We set x/y to have a single point,
# since we need to create the plot with some data.
# Since this point is at the bottom left corner of the plot,
# it should be hidden by consecutive plotting commands.
x = F[xlim[1]]
y = F[ylim[1]]
x = Vector{F}(xlim)
y = Vector{F}(ylim)
z = Vector{F}(zlim)

# create a plot window with xlim/ylim set,
# but the X/Y vectors are outside the bounds
canvas = if (up_c = get(sp[:extra_kwargs], :canvas, :auto)) == :auto
canvas = if (up_c = get(sp[:extra_kwargs], :canvas, :auto)) === :auto
isijulia() ? :ascii : :braille
else
up_c
end

border = if (up_b = get(sp[:extra_kwargs], :border, :auto)) == :auto
border = if (up_b = get(sp[:extra_kwargs], :border, :auto)) === :auto
isijulia() ? :ascii : :solid
else
up_b
Expand All @@ -52,6 +53,7 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
width = has_layout && isempty(series_list(sp)) ? 0 : UnicodePlots.DEFAULT_WIDTH[]
height = UnicodePlots.DEFAULT_HEIGHT[]

plot_3d = is3d(sp)
blend = get(sp[:extra_kwargs], :blend, true)
grid = xaxis[:grid] && yaxis[:grid]
quiver = contour = false
Expand All @@ -70,6 +72,8 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
grid &= !(quiver || contour)
blend &= !(quiver || contour)

plot_3d && (xlim = ylim = (0, 0)) # determined using projection

kw = (
compact = true,
title = texmath2unicode(sp[:title]),
Expand All @@ -84,11 +88,24 @@ function unicodeplots_rebuild(plt::Plot{UnicodePlotsBackend})
border = border,
xlim = xlim,
ylim = ylim,
# 3d
projection = plot_3d ? :orthographic : nothing,
up = sp[:zaxis][:flip] ? :mz : :pz,
# PyPlot: azimuth = -60 & elevation = 30
azimuth = -45,
elevation = 30,
)

o = UnicodePlots.Plot(x, y, _canvas_map[canvas]; kw...)
o = UnicodePlots.Plot(x, y, plot_3d ? z : nothing, _canvas_map[canvas]; kw...)
for series in series_list(sp)
o = addUnicodeSeries!(sp, o, kw, series, sp[:legend_position] != :none)
o = addUnicodeSeries!(
sp,
o,
kw,
series,
sp[:legend_position] !== :none,
plot_3d,
)
end

for ann in sp[:annotations]
Expand Down Expand Up @@ -125,6 +142,7 @@ function addUnicodeSeries!(
kw,
series,
addlegend::Bool,
plot_3d::Bool,
)
st = series[:seriestype]

Expand All @@ -134,20 +152,21 @@ function addUnicodeSeries!(
elseif st === :shape
shape_data(series)
else
float(series[:x]), float(series[:y])
series[:x], series[:y]
end

# special handling (src/interface)
fix_ar = get(series[:extra_kwargs], :fix_ar, true)
if st === :histogram2d
return UnicodePlots.densityplot(x, y; kw...)
elseif st === :spy
return UnicodePlots.spy(series[:z].surf; fix_ar = fix_ar, kw...)
elseif st in (:contour, :heatmap)
return UnicodePlots.spy(Array(series[:z]); fix_ar = fix_ar, kw...)
elseif st in (:contour, :heatmap) # 2D
colormap = get(series[:extra_kwargs], :colormap, :none)
kw = (
kw...,
zlabel = sp[:colorbar_title],
colormap = (cm = _up_colormap[] === :none) ? up_cmap(series) : cm,
colormap = colormap === :none ? up_cmap(series) : colormap,
colorbar = hascolorbar(sp),
)
if st === :contour
Expand All @@ -156,21 +175,36 @@ function addUnicodeSeries!(
return UnicodePlots.contourplot(
x,
y,
series[:z].surf;
Array(series[:z]);
kw...,
levels = series[:levels],
)
elseif st === :heatmap
return UnicodePlots.heatmap(series[:z].surf; fix_ar = fix_ar, kw...)
# zlim = collect(axis_limits(sp, :z))
return UnicodePlots.heatmap(Array(series[:z]); fix_ar = fix_ar, kw...)
end
elseif st in (:surface, :wireframe) # 3D
colormap = get(series[:extra_kwargs], :colormap, :none)
kw = (
kw...,
zlabel = sp[:colorbar_title],
colormap = colormap === :none ? up_cmap(series) : colormap,
colorbar = hascolorbar(sp),
color = st === :wireframe ? up_color(get_linecolor(series, 1)) : nothing,
lines = st === :wireframe,
)
return UnicodePlots.surfaceplot(x, y, Array(series[:z]); kw...)
elseif st === :mesh3d
return UnicodePlots.lineplot!(
up,
mesh3d_triangles(x, y, series[:z], series[:connections])...,
)
end

# now use the ! functions to add to the plot
if st in (:path, :straightline, :shape)
if st in (:path, :path3d, :straightline, :shape, :mesh3d)
func = UnicodePlots.lineplot!
series_kw = (; head_tail = series[:arrow] isa Arrow ? series[:arrow].side : nothing)
elseif st === :scatter || series[:markershape] != :none
elseif st in (:scatter, :scatter3d) || series[:markershape] !== :none
func = UnicodePlots.scatterplot!
series_kw = (; marker = series[:markershape])
else
Expand All @@ -185,7 +219,8 @@ function addUnicodeSeries!(
up = func(
up,
x[rng],
y[rng];
y[rng],
plot_3d ? series[:z][rng] : nothing;
color = up_color(lc),
name = n == 1 ? label : "",
series_kw...,
Expand Down
6 changes: 1 addition & 5 deletions src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,6 @@ _backend_skips = Dict(
6, # embedded images unsupported
16, # nested layout unsupported
21, # custom markers unsupported
24, # 3D unsupported
26, # nested layout unsupported
27, # polar plots unsupported
29, # nested layout unsupported
Expand All @@ -1311,12 +1310,9 @@ _backend_skips = Dict(
37, # ribbons / filled unsupported
43, # heatmap with DateTime
45, # error bars
47, # mesh3D unsupported
49, # polar heatmap
50, # 3D surface unsupported
51, # embedded images unsupported
52, # 3D quiver unsupported
55, # 3D unsupported
55, # mirror unsupported, resolution too low
56, # barplots
],
:gaston => [
Expand Down