How to Automatically Generate Labels for `ComposedFunction`s in `Plots.jl`?

I have the following code:

using Plots

f(x) = sin(x)
plot(1:10, f.(1:10); label="$f")  # This works as expected; the label is "f".

g = f ∘ f
plot(1:10, g.(1:10); label="$g")  # The label here is "Main.f ∘ Main.f", but I want it to be "f ∘ f".

The Main module prefix appears in the label when plotting composed functions. Is there a way to generate the correct label dynamically (e.g., “f ∘ f”) without manually specifying it? Ideally, I’d like this to work even if I compose functions multiple times (e.g., f ∘ f ∘ f).

How can I achieve this? Is there a general solution for automatically generating labels for such composed functions?

As Julius Krumbiegel and Michael Abbott suggested on Slack, this is not a Plots.jl issue. I should use:

julia> g = f ∘ f
f ∘ f

julia> repr(g, context = :module => Main)
"f ∘ f"

julia> repr(g)
"Main.f ∘ Main.f"

Thanks to Julius Krumbiegel!

1 Like