首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子类pandas.plotting._core.PlanePlot导致ValueError

子类pandas.plotting._core.PlanePlot导致ValueError
EN

Stack Overflow用户
提问于 2018-03-16 17:32:12
回答 1查看 398关注 0票数 0

背景

在下面的示例中,我尝试将自己的绘图方法添加到pandas.DataFrame.plot中,我从熊猫那里复制并粘贴了ScatterPlot,并将名称更改为VscatterPlot。然后,我将这个类连同对应辅助函数pandas.DataFrame.plot添加到他们各自的位置(参见下面)。

示例类VscatterPlot

代码语言:javascript
复制
import pandas
import numpy as np


class VscatterPlot(pandas.plotting._core.PlanePlot):
    _kind = 'vscatter'

    def __init__(self, data, x, y, s=None, c=None, **kwargs):
        if s is None:
            # hide the matplotlib default for size, in case we want to change
            # the handling of this argument later
            s = 20
        
        super(VscatterPlot, self).__init__(data, x, y, s=s, **kwargs)
        
        if is_integer(c) and not self.data.columns.holds_integer():
            c = self.data.columns[c]
        
        self.c = c

    def _make_plot(self):
        x, y, c, data = self.x, self.y, self.c, self.data
        ax = self.axes[0]

        c_is_column = is_hashable(c) and c in self.data.columns

        # plot a colorbar only if a colormap is provided or necessary
        cb = self.kwds.pop('colorbar', self.colormap or c_is_column)

        # pandas uses colormap, matplotlib uses cmap.
        cmap = self.colormap or 'Greys'
        cmap = self.plt.cm.get_cmap(cmap)
        color = self.kwds.pop("color", None)
        
        if c is not None and color is not None:
            raise TypeError('Specify exactly one of `c` and `color`')
        
        elif c is None and color is None:
            c_values = self.plt.rcParams['patch.facecolor']
        
        elif color is not None:
            c_values = color
        
        elif c_is_column:
            c_values = self.data[c].values
        
        else:
            c_values = c

        if self.legend and hasattr(self, 'label'):
            label = self.label
        
        else:
            label = None
        
        scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
                             label=label, cmap=cmap, **self.kwds)
        
        if cb:
            img = ax.collections[0]
            kws = dict(ax=ax)
            if self.mpl_ge_1_3_1():
                kws['label'] = c if c_is_column else ''
            self.fig.colorbar(img, **kws)

        if label is not None:
            self._add_legend_handle(scatter, label)
        
        else:
            self.legend = False

        errors_x = self._get_errorbars(label=x, index=0, yerr=False)
        errors_y = self._get_errorbars(label=y, index=0, xerr=False)
        
        if len(errors_x) > 0 or len(errors_y) > 0:
            err_kwds = dict(errors_x, **errors_y)
            err_kwds['ecolor'] = scatter.get_facecolor()[0]
            ax.errorbar(data[x].values, data[y].values,
                        linestyle='none', **err_kwds)

修正pandas.plotting._core

代码语言:javascript
复制
# Set VscatterPlot as an attribute of pandas.plotting._core
setattr(pandas.plotting._core, "VscatterPlot", VscatterPlot)

# Create the vscatter helper function
def vscatter(self, x, y, s=None, c=None, **kwds):
    return self(kind='vscatter', x=x, y=y, c=c, s=s, **kwds)

# Set the helper function
setattr(pandas.plotting._core.FramePlotMethods, "vscatter", vscatter)

# Append the class to pandas.plotting._core._klasses
pandas.plotting._core._klasses.append(pandas.plotting._core.VscatterPlot)

# Add the class to the pandas.plotting._core._plot_klass dict
pandas.plotting._core._plot_klass[VscatterPlot._kind] = pandas.plotting._core.VscatterPlot

测试

代码语言:javascript
复制
example = pandas.DataFrame(np.random.random((5,2)), columns=["x", "y"])
example.plot.vscatter(x="x", y="y")

输出

代码语言:javascript
复制
ValueError: 'vscatter' is not a valid plot kind

问题

我在这里遗漏了什么?vscatterpandas.plotting._core._plot_klass,所以它为什么要抛出这个ValueError?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-16 18:08:13

pandas.plotting._core中有两个列表,它们决定如何实例化类。您需要将"vscatter"放在这些列表中。

代码语言:javascript
复制
pandas.plotting._core._dataframe_kinds.append("vscatter")
pandas.plotting._core._all_kinds.append("vscatter")

此外,还有一些进口品不见了。以下代码

代码语言:javascript
复制
import matplotlib.pyplot as plt
import pandas
import numpy as np
from pandas.core.dtypes.common import is_integer, is_hashable


class VscatterPlot(pandas.plotting._core.PlanePlot):
    _kind = 'vscatter'

    def __init__(self, data, x, y, s=None, c=None, **kwargs):
        if s is None:
            # hide the matplotlib default for size, in case we want to change
            # the handling of this argument later
            s = 20

        super(VscatterPlot, self).__init__(data, x, y, s=s, **kwargs)

        if is_integer(c) and not self.data.columns.holds_integer():
            c = self.data.columns[c]

        self.c = c

    def _make_plot(self):
        x, y, c, data = self.x, self.y, self.c, self.data
        ax = self.axes[0]

        c_is_column = is_hashable(c) and c in self.data.columns

        # plot a colorbar only if a colormap is provided or necessary
        cb = self.kwds.pop('colorbar', self.colormap or c_is_column)

        # pandas uses colormap, matplotlib uses cmap.
        cmap = self.colormap or 'Greys'
        cmap = self.plt.cm.get_cmap(cmap)
        color = self.kwds.pop("color", None)

        if c is not None and color is not None:
            raise TypeError('Specify exactly one of `c` and `color`')

        elif c is None and color is None:
            c_values = self.plt.rcParams['patch.facecolor']

        elif color is not None:
            c_values = color

        elif c_is_column:
            c_values = self.data[c].values

        else:
            c_values = c

        if self.legend and hasattr(self, 'label'):
            label = self.label

        else:
            label = None

        scatter = ax.scatter(data[x].values, data[y].values, c=c_values,
                             label=label, cmap=cmap, **self.kwds)

        if cb:
            img = ax.collections[0]
            kws = dict(ax=ax)
            if self.mpl_ge_1_3_1():
                kws['label'] = c if c_is_column else ''
            self.fig.colorbar(img, **kws)

        if label is not None:
            self._add_legend_handle(scatter, label)

        else:
            self.legend = False

        errors_x = self._get_errorbars(label=x, index=0, yerr=False)
        errors_y = self._get_errorbars(label=y, index=0, xerr=False)

        if len(errors_x) > 0 or len(errors_y) > 0:
            err_kwds = dict(errors_x, **errors_y)
            err_kwds['ecolor'] = scatter.get_facecolor()[0]
            ax.errorbar(data[x].values, data[y].values,
                        linestyle='none', **err_kwds)

#Amending the pandas.plotting._core
# Set VscatterPlot as an attribute of pandas.plotting._core
setattr(pandas.plotting._core, "VscatterPlot", VscatterPlot)

# Create the vscatter helper function
def vscatter(self, x, y, s=None, c=None, **kwds):
    return self(kind='vscatter', x=x, y=y, c=c, s=s, **kwds)

# Set the helper function
setattr(pandas.plotting._core.FramePlotMethods, "vscatter", vscatter)

# Append the class to pandas.plotting._core._klasses
pandas.plotting._core._klasses.append(pandas.plotting._core.VscatterPlot)

# Add the class to the pandas.plotting._core._plot_klass dict
pandas.plotting._core._plot_klass[VscatterPlot._kind] = pandas.plotting._core.VscatterPlot

pandas.plotting._core._dataframe_kinds.append("vscatter")
pandas.plotting._core._all_kinds.append("vscatter")
#Testing
example = pandas.DataFrame(np.random.random((5,2)), columns=["x", "y"])
example.plot.vscatter(x="x", y="y")

plt.show()

产生这个输出

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49326496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档