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

WIP: Change how the colorbar axis works #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

gustaphe
Copy link
Collaborator

@gustaphe gustaphe commented Nov 1, 2021

Issue #68

Tries to unify how we treat the axes and axis labels, and apply that to the c axis.

In this state, it fails when applying a plot! call with line_z or marker_z on top of one with fill_z. And it kind of weirds out with implicit fill_z (like plot(x, y, z; st=:surface)). But it correctly saves and checks cbar units for lines and markers, as well as for only fill_z.

using UnitfulRecipes
using Plots
using Unitful
plotly()

gaussian(x, y, x0, y0, wx, wy) = exp(-((x-x0)^2/wx^2 + (y-y0)^2/wy^2))

x = (-2:0.1:2)u"mm";
y = (-2:0.1:2)u"mm";
z = 1u"W/m^2"*gaussian.(x', y, 0u"mm", 0u"mm", 0.5u"mm", 1u"mm");
c = 300u"K" .+ 4u"K/mm".*(x' .+ y);
println(size(c))

plot(x, y, z;
     fill_z=c,
     st=:surface,
     colorbar_title="T"
    )

image

@@ -2,6 +2,7 @@ module UnitfulRecipes

using RecipesBase
using Unitful: Quantity, unit, ustrip, Unitful, dimension, Units
using Infiltrator
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only for development purposes

get!(attr, axislabel, label)
end

u = fixlabel!(attr, axisletter, unit(first(x)))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much of this code needed to be repeated for the c axis, so I factored it out.

# Fix the attributes: labels, lims, ticks, marker/line stuff, etc.
append_unit_if_needed!(attr, axislabel, u)
ustripattribute!(attr, axislims, u)
ustripattribute!(attr, axisticks, u)
ustripattribute!(attr, err, u)
fixmarkercolor!(attr)
fixcolors!(attr)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to run a single pass over all three color arguments, to combine into one axis.

end
getlabel(attr, sp, axisname, labelname) = attr[:plot_object][sp][axisname][labelname]
getlabel(attr, sp, axisname::Nothing, labelname) = attr[:plot_object][sp][labelname]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a limitation in Plots that it's attr[:plot_object][sp][:colorbar_title] rather than attr[:plot_object][sp][:caxis][:guide], but it is what it is.

ustripattribute!(plotattributes, :clims, u)
z = fixaxis!(plotattributes, z, :z)
append_unit_if_needed!(plotattributes, :colorbar_title, u)
x, y, z
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this recipe if x, y and z are correctly identified as type targets for themselves.

@gustaphe gustaphe added the enhancement New feature or request label Nov 1, 2021
@gustaphe gustaphe linked an issue Nov 1, 2021 that may be closed by this pull request
@gustaphe
Copy link
Collaborator Author

Testing:

using UnitfulRecipes
using Test
using Plots
using Unitful
#plotlyjs()
#gr()
pyplot()

default(; label=nothing)

x = randn(5)u"m"
t = randn(5)u"s"
T = randn(5)u"K"
E = 100 * randn(5)u"J"

pl1 = plot(x, t; marker_z=T, st=:scatter, xguide="x", yguide="t", colorbartitle="T")
pl2 = plot(x, t; line_z=T, st=:line, xguide="x", yguide="t", colorbartitle="T")
pl3 = plot(x, t; line_z=T, st=:line)
#plot!(pl3, x, t; marker_z=E, st=:scatter, colorbartitle="T or E?") # Rightfully fails
plot!(pl3, x, t; marker_z=T, st=:scatter, xguide="x", yguide="t", colorbartitle="T")

gaussian(x, y, x0, y0, wx, wy) = exp(-((x - x0)^2 / wx^2 + (y - y0)^2 / wy^2))

x = (-2:0.1:2)u"mm";
y = (-2:0.1:2)u"mm";
z = 1u"W/m^2" * gaussian.(x', y, 0u"mm", 0u"mm", 0.5u"mm", 1u"mm");
c = 300u"K" .+ 4u"K/mm" .* (x' .+ y);
println(size(c))

pl4 = plot(
    x, y, z; fill_z=c, st=:surface, xguide="x", yguide="y", zguide="I", colorbar_title="T"
)
pl5 = plot(x, y, z; fill_z=c, st=:surface)
plot!(
    pl5,
    randn(5)u"mm",
    randn(5)u"mm",
    fill(1.5u"W/m^2", length(x));
    xguide="x",
    yguide="y",
    zguide="I",
    colorbar_title="T",
    st=:line,
    line_z=150u"K" .+ 20 * randn(size(x))u"K",
)

savefig.([pl1, pl2, pl3, pl4, pl5], ["pl1.png", "pl2.png", "pl3.png", "pl4.png", "pl5.png"])

#plot(pl1, pl2, pl3, pl4, pl5)

pl1
pl2
pl3
pl4
pl5

To me, this PR can do what you want it to do. But it feels slightly unpolished: in plotlyjs only pl4 gets a colorbar title at all, and the plots with multiple series get doubled colorbar ticks. On the other hand those seem like upstream bugs so maybe that is acceptable.

If there are no complaints I'll just write tests and docs.

@dmoored4

@gustaphe
Copy link
Collaborator Author

Oh no, there are some regressions when running tests. I'll look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Units from 3rd axis applied to 4th plot
1 participant