Skip to content

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")

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, box or hist on the accessor. kde, density, pie and hexbin raised NotImplementedError with plotly, so a file that names one is left alone.
  • Every argument of those calls is x, y or title. A file with another keyword, such as figsize or ax, is left alone, and so is a file that calls df.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__.py of a package that the file is in
  • a module of the project that the file imports
  • a module that one of those modules imports

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.

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.