pandas/plot-without-plotly-backend
| Rule | plot-without-plotly-backend |
|---|---|
| Pack | pandas |
| Severity | info |
| Fix | unsafe, with xpd check --fix --unsafe-fixes |
| Library versions | every version |
A file that draws with df.plot and sets no backend gets matplotlib charts that xpd's plotly rules cannot check. Set pd.options.plotting.backend = "plotly" once after the import.
import pandas as pd
pd.options.plotting.backend = "plotly"
df.plot.bar(x="team", y="score")What the rule reports
Section titled “What the rule reports”The rule reports a file that draws with a plot method of the accessor and sets no plotting backend. The report is on the first import of pandas, once per file. xpd reports only a file where the setting changes no other line:
- The file calls
line,bar,barh,area,scatter,boxorhiston the accessor.kde,density,pieandhexbinraisedNotImplementedErrorwith plotly, so a file that names one is left alone. - Every argument of those calls is
x,yortitle. A file with another keyword, such asfigsizeorax, is left alone, and so is a file that callsdf.plot(...)itself. - Each call stands alone as a statement or is returned. A file that assigns the result is left alone, because the result is a matplotlib Axes there.
- The file imports neither matplotlib nor seaborn.
The rule also reports nothing when one of the following modules sets the backend:
- the
__init__.pyof a package that the file is in - a module of the project that the file imports
- a module that one of those modules imports
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix adds the line pd.options.plotting.backend = "plotly" after import pandas as pd. df.plot then returns a plotly Figure in place of a matplotlib Axes. Any other first import of pandas has no fix.
Message
Section titled “Message”xpd prints this message with the issue. A name that starts with $ stands for code from your file.
This file draws with df.plot and sets no plotting backend, so pandas draws with matplotlib and returns an Axes. Set pd.options.plotting.backend = "plotly" once. With it df.plot.bar(x="team", y="score") returned the same figure as px.bar(df, x="team", y="score"), and xpd checks that figure with its plotly rules. The plot calls of this file pass only x, y and title, which plotly takes too.Browse the other rules of the pandas pack.