-
Notifications
You must be signed in to change notification settings - Fork 250
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
Enable plotting of Unitful values #1105
Conversation
@bjarthur do you need me to include the fixes to the y-axis label with this PR? |
by fixing the y-axis label, to you actually mean y-axis ticks? if so, the relevant code is here. compare with the x-axis tick code immediately above to see what the difference might be. also, if we're serious about supporting this in future, you should add your simple test case above to test/testscripts/. this would necessitate adding Unitful to test/REQUIRE. @tlnagy what do you think of adding a test dependency like this? previously you had mentioned refactoring Measures out in favor of Unitful i believe. |
I don't have any problems with adding a test dependency since that wouldn't bloat our dependency graph for a normal install. We should definitely add a test for using
You're not testing unitful quantities on the y axis though? @non-Jedi Some other edge cases: does this work with unitful quantities on the y axis? On both axes? |
We should make sure that this works in all basic cases before merging this PR. We can leave this open for one of us (or someone else) to update if you don't have time. You can keep using your local branch with this modification for your immediate plotting needs. |
One thing I would like to point out is that it is custom to have the units in axes labels instead in the ticks. Like so p = plot(x=[1;2;3;4], y=[2;4;8;16]u"kg", Geom.line, Guide.ylabel("y (kg)")) but here the information is duplicated and there is no easy way to hide the units in the ticks p = plot(x=[1;2;3;4], y=[2;4;8;16]u"kg", Geom.line, Guide.yticks(ticks=[2,4,6,8]))
DimensionError: 2 and 2 kg are not dimensionally compatible.
Stacktrace:
[1] min(::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Gram,Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}}(3, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Mass}(1//1),)}}}, ::Int64) at ./operators.jl:361
[2] apply_statistic(::Gadfly.Stat.TickStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /Users/tamasnagy/.julia/v0.6/Gadfly/src/statistics.jl:784
[3] apply_statistics(::Array{Gadfly.StatisticElement,1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /Users/tamasnagy/.julia/v0.6/Gadfly/src/statistics.jl:41
[4] render_prepare(::Gadfly.Plot) at /Users/tamasnagy/.julia/v0.6/Gadfly/src/Gadfly.jl:708
[5] render(::Gadfly.Plot) at /Users/tamasnagy/.julia/v0.6/Gadfly/src/Gadfly.jl:761
[6] show at /Users/tamasnagy/.julia/v0.6/Gadfly/src/Gadfly.jl:965 [inlined]
[7] limitstringmime(::MIME{Symbol("image/svg+xml")}, ::Gadfly.Plot) at /Users/tamasnagy/.julia/v0.6/IJulia/src/inline.jl:24
[8] display_dict(::Gadfly.Plot) at /Users/tamasnagy/.julia/v0.6/IJulia/src/execute_request.jl:33
[9] (::Compat.#inner#17{Array{Any,1},IJulia.#display_dict,Tuple{Gadfly.Plot}})() at /Users/tamasnagy/.julia/v0.6/Compat/src/Compat.jl:488
[10] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/tamasnagy/.julia/v0.6/IJulia/src/execute_request.jl:186
[11] (::Compat.#inner#17{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() at /Users/tamasnagy/.julia/v0.6/Compat/src/Compat.jl:488
[12] eventloop(::ZMQ.Socket) at /Users/tamasnagy/.julia/v0.6/IJulia/src/eventloop.jl:8
[13] (::IJulia.##14#17)() at ./task.jl:335 |
🤦 yep, that explains it. I'll plan on pushing a commit to this branch sometime this weekend testing a To be honest, I see the current content of this PR as a bugfix with the topic of |
Interesting note: using Unitful here enforces the no-multiple axes principle |
Codecov Report
@@ Coverage Diff @@
## master #1105 +/- ##
==========================================
+ Coverage 74.07% 83.53% +9.45%
==========================================
Files 35 35
Lines 4042 4032 -10
==========================================
+ Hits 2994 3368 +374
+ Misses 1048 664 -384
Continue to review full report at Codecov.
|
86a85e7
to
a40eb33
Compare
@bjarthur @tlnagy FYI, from the perspective of getting all basic Unitful |
ahah, so nominally unitful should've worked all along, and the changes you had to make in this PR were simply to fix oversights due to lack of unit testing combining Percent with hvabline, scales, ticks, etc. okay, so i'm fine then with merging. my only reservation is adding Unitful to test/REQUIRE, because 1. there are other packages which also provide unit types; why should we single out that one?, and 2. Gadfly already has a huge dependency tree which many complain about. would you consider adding a gridstack to test/percent.jl and moving your three test scripts over there? you also need to resolve conflicts. thanks for this! looks great. |
I think
This is only in Main things remaining before this is ready:
|
it is now, but for how long? there have been many others in the past. i'd rather gadfly be agnostic. |
If a new one emerges, we can always switch the tests over to the new library. None of the changes this PR makes in |
do bug fixes like this warrant a mention in NEWS? i'd suggest holding off on that until the units appear in the label and not the ticks. that would be a big enough step that a whole new section in the Tutorial dedicated to units could be added too. also, i am still really in favor of just having the unit tests in this PR use the Percent type that dcjones already has in the tests. the code in the tests as they are now could be saved and used for the docs later.
|
I was thinking of it more like we're adding feature (e.g. Gadfly now supports unitful values too!)
If you feel strongly about this then we shouldn't include Gadfly.jl/test/testscripts/percent.jl Lines 7 to 9 in 602f2d6
|
as @non-Jedi pointed out, units have nominally worked all the way back to #77. they just broke over time because of lack of testing. so yes, let's just expand testscripts/percent.jl to test everything that this PR fixes. then we don't need Unitful in test/REQUIRE and Gadfly code and tests are agnostic. i don't mind using Unitful in the docs, but think it looks funny to have the units in the ticks not labels, so would wait to do so until that is changed. |
On the subject of units on ticks vs labels, it looks like nicely formatting this That said, if there's consensus on not testing against Unitful, I'll go ahead |
(On another note, since Showoff is still a dependency of Gadfly and not really used by anything else afaict, shouldn't it get moved into the GiovineItalia organization?) |
What's wrong with where it is? julia> Pkg.dependents("Showoff")
4-element Array{AbstractString,1}:
"Plots"
"Gadfly"
"Mamba"
"MixedModels" If you want to make a PR against Showoff adding that convenience function, I would be happy to review it and merge. Thanks for your efforts here! |
No way of doing it without making Showoff depend on Unitful which would then end up pulling in Unitful to Gadfly where we're trying to avoid it. :/ Unless y'all know how to do some sort of an optional dependency, I was thinking that I would just write some more thorough documentation about overriding (Thanks for showing me |
optional dependencies will soon be added to Pkg3. i look forward to making Gadfly optionally depend on Cairo and DataFrames. in this case though, an example in the documentation of how to use Showoff with Unitful would be great. i'd even devote an entirely new section in the Tutorial or Manual to it. could do that now and not wait until we've figured out how to automatically add it to labels. |
Hey @non-Jedi, we could use |
This is also generally more correct. convert(T, one(T)) isn't necessarily defined for any arbitrary T, but oneunit(T) should be.
For what it's worth, this is now good to go with Julia 1.0. Only thing blocking merge now is the request to remove the test dependency on Unitful. I hope to have time to clean that up sometime in the next month or so, but no promises. |
fbd7a0c
to
e5436d0
Compare
I'm not going to be able to test this effectively without Unitful. As an example, take a look at https://github.com/GiovineItalia/Gadfly.jl/pull/1105/files#diff-d568a2b9faaba434884de694f9537f19R15 where I'm plotting distance versus time and giving slope in terms of velocity; that's too much of Unitful to reimplement just for running a test. @bjarthur, could you please reconsider allowing Unitful as a test dependency? |
thanks for this! would be great to some day document how to use Showoff to get the units in the labels and out of the ticks. |
Following test case works:
Generates:
Played around with it a bit afterward, and I didn't run into any other breaking issues. Would be nice if the ylabel showed units like the xlabel, but this at least makes Gadfly compatible with Unitful (and generally with well-behaved custom number types).
Closes #1104