Skip to content

CLN: move plotting funcs to pd.plotting #13579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions doc/source/visualization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ You can also create these other plots using the methods ``DataFrame.plot.<kind>`
In addition to these ``kind`` s, there are the :ref:`DataFrame.hist() <visualization.hist>`,
and :ref:`DataFrame.boxplot() <visualization.box>` methods, which use a separate interface.

Finally, there are several :ref:`plotting functions <visualization.tools>` in ``pandas.tools.plotting``
Finally, there are several :ref:`plotting functions <visualization.tools>` in ``pandas.plotting``
that take a :class:`Series` or :class:`DataFrame` as an argument. These
include

Expand Down Expand Up @@ -823,7 +823,7 @@ before plotting.
Plotting Tools
--------------

These functions can be imported from ``pandas.tools.plotting``
These functions can be imported from ``pandas.plotting``
and take a :class:`Series` or :class:`DataFrame` as an argument.

.. _visualization.scatter_matrix:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from pandas.tools.plotting import scatter_matrix
from matplotlib import cm
feature_names = ['mass', 'width', 'height', 'color_score']
X = fruits[feature_names]
y = fruits['fruit_label']
cmap = cm.get_cmap('gnuplot')
scatter = pd.scatter_matrix(X, c = y, marker = 'o', s=40, hist_kwds={'bins':15}, figsize=(9,9), cmap = cmap)
plt.suptitle('Scatter-matrix for each input variable')
plt.savefig('fruits_scatter_matrix')

Expand All @@ -834,7 +834,7 @@ Scatter Matrix Plot
.. versionadded:: 0.7.3

You can create a scatter plot matrix using the
``scatter_matrix`` method in ``pandas.tools.plotting``:
``scatter_matrix`` method in ``pandas.plotting``:

.. ipython:: python
:suppress:
Expand All @@ -843,7 +843,7 @@ You can create a scatter plot matrix using the

.. ipython:: python

from pandas.tools.plotting import scatter_matrix
from pandas.plotting import scatter_matrix
df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])

@savefig scatter_matrix_kde.png
Expand Down Expand Up @@ -896,7 +896,7 @@ of the same class will usually be closer together and form larger structures.

.. ipython:: python

from pandas.tools.plotting import andrews_curves
from pandas.plotting import andrews_curves

data = pd.read_csv('data/iris.data')

Expand All @@ -918,7 +918,7 @@ represents one data point. Points that tend to cluster will appear closer togeth

.. ipython:: python

from pandas.tools.plotting import parallel_coordinates
from pandas.plotting import parallel_coordinates

data = pd.read_csv('data/iris.data')

Expand Down Expand Up @@ -948,7 +948,7 @@ implies that the underlying data are not random.

.. ipython:: python

from pandas.tools.plotting import lag_plot
from pandas.plotting import lag_plot

plt.figure()

Expand Down Expand Up @@ -983,7 +983,7 @@ confidence band.

.. ipython:: python

from pandas.tools.plotting import autocorrelation_plot
from pandas.plotting import autocorrelation_plot

plt.figure()

Expand Down Expand Up @@ -1016,7 +1016,7 @@ are what constitutes the bootstrap plot.

.. ipython:: python

from pandas.tools.plotting import bootstrap_plot
from pandas.plotting import bootstrap_plot

data = pd.Series(np.random.rand(1000))

Expand Down Expand Up @@ -1048,7 +1048,7 @@ be colored differently.

.. ipython:: python

from pandas.tools.plotting import radviz
from pandas.plotting import radviz

data = pd.read_csv('data/iris.data')

Expand Down Expand Up @@ -1438,11 +1438,11 @@ Also, you can pass different :class:`DataFrame` or :class:`Series` for ``table``

plt.close('all')

Finally, there is a helper function ``pandas.tools.plotting.table`` to create a table from :class:`DataFrame` and :class:`Series`, and add it to an ``matplotlib.Axes``. This function can accept keywords which matplotlib table has.
Finally, there is a helper function ``pandas.plotting.table`` to create a table from :class:`DataFrame` and :class:`Series`, and add it to an ``matplotlib.Axes``. This function can accept keywords which matplotlib table has.

.. ipython:: python

from pandas.tools.plotting import table
from pandas.plotting import table
fig, ax = plt.subplots(1, 1)

table(ax, np.round(df.describe(), 2),
Expand Down
26 changes: 26 additions & 0 deletions doc/source/whatsnew/v0.20.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Highlights include:

- Building pandas for development now requires ``cython >= 0.23`` (:issue:`14831`)
- The ``.ix`` indexer has been deprecated, see :ref:`here <whatsnew_0200.api_breaking.deprecate_ix>`
- The ``pandas.tools.plotting`` module has been deprecated, moved to ``pandas.plotting``. See :ref:`here <whatsnew_0200.api_breaking.plotting>` (:issue:`12548`)

Check the :ref:`API Changes <whatsnew_0200.api_breaking>` and :ref:`deprecations <whatsnew_0200.deprecations>` before updating.

Expand Down Expand Up @@ -194,6 +195,31 @@ Using ``.iloc``. Here we will get the location of the 'A' column, then use *posi
df.iloc[[0, 2], df.columns.get_loc('A')]


.. _whatsnew_0200.api_breaking.deprecate_plotting

Deprecate .plotting
^^^^^^^^^^^^^^^^^^^

``pandas.tools.plotting`` module has been deprecated, moving directory under the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pandas.tools.plotting module has been deprecated, in favor of the top level namespace pandas.plotting

top namespace ``pandas.plotting``. All the public plotting functions should be available
from ``pandas.plotting``.

Also, ``scatter_matrix`` function imported directly under ``pandas`` namespace is also deprecated.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pandas.scatter_matrix

Users shoud use ``pandas.plotting.scatter_matrix`` instead.

Previous script:

.. code-block:: python

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add import pandas as pd

pd.tools.plotting.scatter_matrix(df)
pd.scatter_matrix(df)

Should be changed to:

.. code-block:: python

pd.plotting.scatter_matrix(df)

.. _whatsnew_0200.api_breaking.index_map

Map on Index types now return other Index types
Expand Down
10 changes: 9 additions & 1 deletion pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@
from pandas.tools.merge import (merge, concat, ordered_merge,
merge_ordered, merge_asof)
from pandas.tools.pivot import pivot_table, crosstab
from pandas.tools.plotting import scatter_matrix, plot_params

# deprecate tools.plotting, and scatter_matrix on the top namespace
import pandas.tools.plotting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can actually deprecate pandas.tools.plotting with pandas.util.deprecate_module

from pandas.plotting import plot_params
# do not import deprecate to top namespace
scatter_matrix = pandas.util.decorators.deprecate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason NOT to remove plot_parms as well from top-level?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on moving plot_params as well

'pandas.scatter_matrix', pandas.plotting.scatter_matrix,
'pandas.plotting.scatter_matrix')

from pandas.tools.tile import cut, qcut
from pandas.tools.util import to_numeric
from pandas.core.reshape import melt
Expand Down
2 changes: 1 addition & 1 deletion pandas/api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestPDApi(Base, tm.TestCase):
lib = ['api', 'compat', 'computation', 'core',
'indexes', 'formats', 'pandas',
'test', 'tools', 'tseries',
'types', 'util', 'options', 'io']
'types', 'util', 'options', 'io', 'plotting']

# top-level packages that are c-imports, should rename to _*
# to avoid naming conflicts
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/config_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def mpl_style_cb(key):
stacklevel=5)

import sys
from pandas.tools.plotting import mpl_stylesheet
from pandas.plotting.style import mpl_stylesheet
global style_backup

val = cf.get_option(key)
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
import pandas.core.ops as ops
import pandas.formats.format as fmt
from pandas.formats.printing import pprint_thing
import pandas.tools.plotting as gfx
import pandas.plotting.core as gfx

import pandas.lib as lib
import pandas.algos as _algos
Expand Down Expand Up @@ -5721,7 +5721,7 @@ def _put_str(s, space):
@Appender(_shared_docs['boxplot'] % _shared_doc_kwargs)
def boxplot(self, column=None, by=None, ax=None, fontsize=None, rot=0,
grid=True, figsize=None, layout=None, return_type=None, **kwds):
import pandas.tools.plotting as plots
import pandas.plotting as plots
import matplotlib.pyplot as plt
ax = plots.boxplot(self, column=column, by=by, ax=ax, fontsize=fontsize,
grid=grid, rot=rot, figsize=figsize, layout=layout,
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3997,7 +3997,7 @@ def count(self):
return self._wrap_agged_blocks(data.items, list(blk))


from pandas.tools.plotting import boxplot_frame_groupby # noqa
from pandas.plotting.core import boxplot_frame_groupby # noqa
DataFrameGroupBy.boxplot = boxplot_frame_groupby


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -2997,7 +2997,7 @@ def __init__(self, *args, **kwargs):
# ----------------------------------------------------------------------
# Add plotting methods to Series

import pandas.tools.plotting as _gfx # noqa
import pandas.plotting.core as _gfx # noqa

Series.plot = base.AccessorProperty(_gfx.SeriesPlotMethods,
_gfx.SeriesPlotMethods)
Expand Down
1 change: 1 addition & 0 deletions pandas/plotting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from pandas.plotting.api import * # noqa
20 changes: 20 additions & 0 deletions pandas/plotting/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""
Plotting api
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a bit the style in other pandas modules as well, but can't we just put this in __init__.py ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is cleaner here actually. And it is only imported explicity (unlike __init__ which is implicity imported)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with implicitly imported?

The difference with the other modules where we use this pattern, is that, eg in pandas.io, pandas.io.api are those functions that are imported top-level (imported in pandas/__init__.py), not what makes up the pandas.io namespace itself. Here, we don't have a collection of functions (put in api.py) which we want to have top level, but we just have the pandas.io namespace. So IMO it makes more sense and is cleaner to have it just in the init file here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean is, unless you explicity reference these imports, like from pandas.plotting import .....) you would by-default actually import anything.

thoughI guess for back-compat this is needed.

IOW, the imports for things like matplotlib would only be done when the pandas.plotting namespace is used (IOW, you try to plot something), so the import is done lazily later, rather than on startup now (to register converters and such).

"""

# flake8: noqa

try: # mpl optional
from pandas.plotting import converter
converter.register() # needs to override so set_xlim works with str/number
except ImportError:
pass

from pandas.plotting.misc import (scatter_matrix, radviz,
andrews_curves, bootstrap_plot,
parallel_coordinates, lag_plot,
autocorrelation_plot)
from pandas.plotting.core import (boxplot, scatter_plot, grouped_hist,
hist_frame, hist_series)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mentioned this before, but is there use in exposing grouped_hist, hist_frame, hist_series as public functions?
There are also not documented I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation argument is actually also true for boxplot and scatter_plot, and for both the functionality is also available in DataFrame.plot.scatter or DataFrame.boxplot/plot.box

from pandas.plotting.style import plot_params
from pandas.plotting.tools import table
51 changes: 51 additions & 0 deletions pandas/plotting/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# being a bit too dynamic
# pylint: disable=E1101
from __future__ import division

from distutils.version import LooseVersion


def _mpl_le_1_2_1():
try:
import matplotlib as mpl
return (str(mpl.__version__) <= LooseVersion('1.2.1') and
str(mpl.__version__)[0] != '0')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add these checks for '0' when matplotlib's versioneer was broken for 1.5. I believe it's fixed now. Do you want to remove them in here, or a separate PR?

except ImportError:
return False


def _mpl_ge_1_3_1():
try:
import matplotlib
# The or v[0] == '0' is because their versioneer is
# messed up on dev
return (matplotlib.__version__ >= LooseVersion('1.3.1') or
matplotlib.__version__[0] == '0')
except ImportError:
return False


def _mpl_ge_1_4_0():
try:
import matplotlib
return (matplotlib.__version__ >= LooseVersion('1.4') or
matplotlib.__version__[0] == '0')
except ImportError:
return False


def _mpl_ge_1_5_0():
try:
import matplotlib
return (matplotlib.__version__ >= LooseVersion('1.5') or
matplotlib.__version__[0] == '0')
except ImportError:
return False


def _mpl_ge_2_0_0():
try:
import matplotlib
return matplotlib.__version__ >= LooseVersion('2.0')
except ImportError:
return False
Loading