Skip to content

[FR] CommonPlot interface #5057

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

Open
ocots opened this issue Mar 17, 2025 · 3 comments
Open

[FR] CommonPlot interface #5057

ocots opened this issue Mar 17, 2025 · 3 comments
Labels

Comments

@ocots
Copy link

ocots commented Mar 17, 2025

It would be nice to have a CommonPlot.jl package as CommonSolve.jl where plot and plot! are defined (maybe others too). Then, the plot and plot! from Plots would have to extend those from CommonPlot.

It would be possible to define in any other ecosystem a plot function without ambiguity with Plots. Besides, it would make possible to have Plots.jl as an extension with plot extended twice, first in the main module to return a message like "please make: julia> using Plots" and secondly in the extension.

@BeastyBlacksmith
Copy link
Member

BeastyBlacksmith commented Mar 17, 2025

We already have that in RecipesBase

function plot end

However, if you just want that to avoid naming conflicts, its not really an interface. If your plot function would want to adhere to the Plots.plot interface it would indeed need to be compatible with Plots recipes.

@ocots
Copy link
Author

ocots commented Mar 17, 2025

Thank you for your answer. Actually "interface" is not the right word. I just want to extend the plot function in order to plot a type from our package. I want Plots.jl to be part of the extensions but I want to tell to the user to make a "using Plots" if he tries to plot something from us and forget to do it.

I have added this to my code:

https://github.com/control-toolbox/CTModels.jl/blob/0c2c1c25611572bb8d9b89c4fa1dedbd1c8bec31/src/CTModels.jl#L34-L40

function RecipesBase.plot(sol::AbstractSolution; kwargs...)
    throw(CTBase.ExtensionError(:Plots))
end
function RecipesBase.plot!(p::RecipesBase.AbstractPlot, sol::AbstractSolution; kwargs...)
    throw(CTBase.ExtensionError(:Plots))
end

which is then extended in the extension when Plots is imported. At the end, to plot an optimal control solution (CTModels.Solution), I have:

function Plots.plot!(
    p::Plots.Plot,
    sol::CTModels.Solution;
    layout::Symbol=__plot_layout(),
    control::Symbol=__control_layout(),
    time::Symbol=__time_normalization(),
    solution_label::String="",
    state_style=(),
    control_style=(),
    costate_style=(),
    kwargs...,
)
...
end

function Plots.plot(
    sol::CTModels.Solution;
    layout::Symbol=__plot_layout(),
    control::Symbol=__control_layout(),
    time::Symbol=__time_normalization(),
    size=__size_plot(sol, control),
    solution_label::String="",
    state_style=(),
    control_style=(),
    costate_style=(),
    kwargs...,
)
...
end

@BeastyBlacksmith
Copy link
Member

BeastyBlacksmith commented Mar 18, 2025

I'd say you should also define methods for RecipesBase.plot instead of Plots.plot in your extension.

Does this work for you? And if not what is the error?

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

No branches or pull requests

2 participants