-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Configurable plotting backends #20885
Comments
I think static typing will be difficult to impossible unless instead of all of them using |
I guess you'd have to put the backend argument in the final method rather than the namespace then you could use overloads but that is suboptimal since you're just going to want to set it in config and not type it out everywhere |
🤔 that's a good point, typing the return type may be problematic in cases where the backend is set just in
🤔 not sure about putting all these on |
You could add Example to demonstrate how typing could work for the interface import typing
import plotly.express as px
import matplotlib.pyplot as plt
from matplotlib.figure import Figure as MatplotlibFigure
from plotly.graph_objects import Figure as PlotlyFigure
class MatplotlibArguments(typing.TypedDict, total=False):
alpha: float
class PlotlyArguments(typing.TypedDict, total=False):
log_x: bool
log_y: bool
@typing.overload
def plot(backend: typing.Literal["matplotlib"], x: list[int], y: list[int], **kwargs: typing.Unpack[MatplotlibArguments]) -> MatplotlibFigure:
...
@typing.overload
def plot(backend: typing.Literal["plotly"], x: list[int], y: list[int], **kwargs: typing.Unpack[PlotlyArguments]) -> PlotlyFigure:
...
def plot(backend: str, x: list[int], y: list[int], **kwargs: typing.Any) -> PlotlyFigure | MatplotlibFigure:
if backend == "matplotlib":
fig = plt.figure()
plt.scatter(x, y, **kwargs, ax=fig.axes)
return fig
elif backend == "plotly":
fig = px.scatter(x=x, y=y, **kwargs)
return fig
else:
raise ValueError()
plot("matplotlib", x=[1, 2], y=[3, 4], alpha=0.5)
plot("plotly", x=[1, 2], y=[30, 400], log_y=True) |
thanks @etrotta
Not totally sure about this, it would slightly detract from ergonomics if people have to do
|
As long as you're not chaining extra methods then
I really don't like typing the backend as a string for the overload, it just feels weird and is more typing than It could be that |
A potentially controversial take, but maybe exposing the plotting backends at a top level namespace could be fine.
|
that would be my preference too but I can see how it'd get too cluttered between plotly, altair, matplotlib, etc and so I assume that's a non-starter. |
This is more a brainstorm than a commitment to follow through but I did this #20904 |
Polars has a
plot
namespace which allows to conveniently create simple plots: https://docs.pola.rs/api/python/dev/reference/dataframe/plot.html. It currently uses AltairSeveral users have said that for some use cases they prefer other libraries. Plotly in particular comes up quite a lot. The good news is that when Plotly v6.0.0 comes out, it will have native support for Polars, meaning that plotly.express will be able to plot Polars dataframes without converting to other dataframe libraries and without other dataframe libraries being required. The perf increase is often ~3x, but even >10x for some plots, especially those which involve grouping over multiple dimensions, see https://plotly.com/blog/chart-smarter-not-harder-universal-dataframe-support/ for some results.
It could be nice there for to make
.plot
configurable, so that users can do:A few principles to abide by, to make sure this doesn't get out of hand:
polars.DataFrame.plot.line
should be some simple dimensions which all plotting backends can respect (e.g.x
,y
,color
, ...). Anything else should be backend-specific.properties
or.configure_*
, in Plotly there's various methodsupdate_*
methods. Links to their respective docs can be provided, but Polars should make no attempt to standardise on theseI don't have a tonne of time now unfortunately, but if anyone wanted to implemented this I'd make it a priority to review it. Else, I will get to it, just not in the immediate future
The text was updated successfully, but these errors were encountered: