PainterQubits / UnitfulPlots.jl

Plots with unit labels, automatically
Other
9 stars 4 forks source link

Plotting matrices (multiple lines) #7

Open Cody-G opened 7 years ago

Cody-G commented 7 years ago

Plotting multiple line plots by passing a matrix of Quantities fails. Here's a demo and the error message

using Plots, UnitfulPlots
plotlyjs()
import Unitful: μm

a = rand(100)
a_um = a * μm

plot([a_um a_um]) #fails
plot([a a]) #succeeds
ERROR: DimensionError: Inf and 0.8797156427357942 μm are not dimensionally compatible.
 in expand_extrema!(::Plots.Extrema, ::Unitful.Quantity{Float64,Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Meter,Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}}(-6,1//1),),Unitful.Dimensions{(Unitful.Dimension{:Length}(1//1),)}}}) at /home/cody/.julia/v0.5/Plots/src/axes.jl:239
 in expand_extrema!(::Plots.Axis, ::Array{Quantity{Float64, Dimensions:{𝐋}, Units:{μm}},1}) at /home/cody/.julia/v0.5/Plots/src/axes.jl:262
 in expand_extrema!(::Plots.Subplot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}) at /home/cody/.julia/v0.5/Plots/src/axes.jl:289
 in _expand_subplot_extrema(::Plots.Subplot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Symbol) at /home/cody/.julia/v0.5/Plots/src/pipeline.jl:361
 in _process_seriesrecipe(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}) at /home/cody/.julia/v0.5/Plots/src/pipeline.jl:392
 in _plot!(::Plots.Plot{Plots.PlotlyJSBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Quantity{Float64, Dimensions:{𝐋}, Units:{μm}},2}}) at /home/cody/.julia/v0.5/Plots/src/plot.jl:227
 in plot(::Array{Quantity{Float64, Dimensions:{𝐋}, Units:{μm}},2}) at /home/cody/.julia/v0.5/Plots/src/plot.jl:46
Cody-G commented 7 years ago

I assume that I can fix this by adding another recipe? I haven't used RecipesBase before but I will look into how to do that.

ajkeller34 commented 7 years ago

You might be able to fix this with a recipe, but if not you'll need to write a method Plots.expand_extrema!(::Plots.Extrema, ::Unitful.Quantity). In the method Plots.expand_extrema!(::Plots.Extrema, ::Number) there appears to be some comparison being done in a dimensionally unsound way since Plots.Extrema has Float64 fields. The fix is probably going to be pretty ugly until Plots.jl itself becomes dimensionally aware, I'd guess.