Change pandas plotting backend to get interactive plots instead of matplotlib static plots

我的未来我决定 提交于 2020-12-05 05:29:44

问题


When I use pandas df.plot() it has matplotlib as a default plotting backend. But this creates static plots.

I would like interactive plots, so I have to change the pandas plotting background.

How do I do change the plotting backend of pandas to have a different library creating my plots when i use .plot()?


回答1:


You need pandas >= 0.25 to change the plotting backend of pandas.

The available plotting backends are:

  • matplotlib
  • hvplot >= 0.5.1
  • holoviews
  • pandas_bokeh
  • plotly >= 4.8
  • altair

So, the default setting is:

pd.options.plotting.backend = 'matplotlib'

You can change the plotting library that pandas uses as follows. In this case it sets hvplot / holoviews as the plotting backend:

pd.options.plotting.backend = 'hvplot'

Or you can also use (which is basically the same):

pd.set_option('plotting.backend', 'hvplot')

Now you have hvplot / holoviews as your plotting backend for pandas and it will give you interactive holoviews plots instead of static matplotlib plots.

Of course you need to have library hvplot / holoviews + dependencies installed for this to work.

Here's a code example resulting in an interactive plot. It uses the standard .plot() pandas syntax:

import numpy as np
import pandas as pd

import hvplot
import hvplot.pandas

pd.options.plotting.backend = 'hvplot'

data = np.random.normal(size=[50, 2])

df = pd.DataFrame(data, columns=['x', 'y'])

df.plot(kind='scatter', x='x', y='y')



回答2:


As of plotly 4.8.0 you can use plotly for interactive plotting with pandas 1.0+.

Update with pip install -U plotly

Set the plotting backend to plotly:

pd.options.plotting.backend = "plotly" 

df = pd.DataFrame(dict(a=[1, 2, 3], b=[2, 4, 6]))
df.plot()

Here's the announcement




回答3:


To change the pandas plotting backend for the whole session, use:

pd.options.plotting.backend = 'plotly'

To change the pandas plotting backend only for one particular plot, use:

df.plot(backend='plotly')

See also:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.html https://pandas.pydata.org/docs/user_guide/options.html?highlight=plotting%20backend#available-options



来源:https://stackoverflow.com/questions/58440524/change-pandas-plotting-backend-to-get-interactive-plots-instead-of-matplotlib-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!