更改 Pandas 绘图后端以获得交互式绘图而不是 matplotlib 静态绘图

问题描述:

当我使用 pandas df.plot() 时,它有 matplotlib 作为默认绘图后端.但这会创建静态图.

我想要交互式绘图,所以我必须更改熊猫绘图背景.

当我使用 .plot() 时,如何更改 Pandas 的绘图后端以使用不同的库来创建我的绘图?

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()?

您需要 pandas >= 0.25 来更改 pandas 的绘图后端.

可用的绘图后端是:

因此,默认设置是:

pd.options.plotting.backend = 'matplotlib'

您可以按如下方式更改pandas 使用的绘图库.在这种情况下,它设置 hvplot/holoviews 作为绘图后端:

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'

或者你也可以使用(基本相同):

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

现在你有 hvplot/holoviews 作为你的 Pandas 绘图后端,它将为你提供交互式全息视图而不是静态 matplotlib 绘图.

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

当然,您需要安装库 hvplot/holoviews + 依赖项才能使其工作.

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

这是一个生成交互式绘图的代码示例.它使用标准的 .plot() 熊猫语法:

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