<commit_before>.. currentmodule:: pandas
.. _visualization:

.. ipython:: python
   :suppress:

   import numpy as np
   np.random.seed(123456)
   from pandas import *
   import pandas.util.testing as tm
   randn = np.random.randn
   np.set_printoptions(precision=4, suppress=True)
   import matplotlib.pyplot as plt
   plt.close('all')

************************
Plotting with matplotlib
************************

.. note::

    We intend to build more plotting integration with `matplotlib
    <http://matplotlib.sourceforge.net>`__ as time goes on.

We use the standard convention for referencing the matplotlib API:

.. ipython:: python

   import matplotlib.pyplot as plt

.. _visualization.basic:

Basic plotting: ``plot``
------------------------

The ``plot`` method on Series and DataFrame is just a simple wrapper around
``plt.plot``:

.. ipython:: python

   ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
   ts = ts.cumsum()

   @savefig series_plot_basic.png width=4.5in
   ts.plot()

If the index consists of dates, it calls ``gcf().autofmt_xdate()`` to try to
format the x-axis nicely as per above. The method takes a number of arguments
for controlling the look of the plot:

.. ipython:: python

   @savefig series_plot_basic2.png width=4.5in
   plt.figure(); ts.plot(style='k--', label='Series'); plt.legend()

On DataFrame, ``plot`` is a convenience to plot all of the columns with labels:

.. ipython:: python

   df = DataFrame(randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
   df = df.cumsum()

   @savefig frame_plot_basic.png width=4.5in
   plt.figure(); df.plot(); plt.legend(loc='best')

You may set the ``legend`` argument to ``False`` to hide the legend, which is
shown by default.

.. ipython:: python

   @savefig frame_plot_basic_noleg.png width=4.5in
   df.plot(legend=False)

Some other options are available, like plotting each Series on a different axis:

.. ipython:: python

   @savefig frame_plot_subplots.png width=4.5in
   df.plot(subplots=True, figsize=(8, 8)); plt.legend(loc='best')

You may pass ``logy`` to get a log-scale Y axis.

.. ipython:: python

   plt.figure();

   ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
   ts = np.exp(ts.cumsum())

   @savefig series_plot_logy.png width=4.5in
   ts.plot(logy=True)

You can plot one column versus another using the `x` and `y` keywords in
`DataFrame.plot`:

.. ipython:: python

   plt.figure()

   df3 = DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
   df3['A'] = Series(range(len(df)))

   @savefig df_plot_xy.png width=4.5in
   df3.plot(x='A', y='B')


Plotting on a Secondary Y-axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To plot data on a secondary y-axis, use the ``secondary_y`` keyword:

.. ipython:: python

   plt.figure()

   df.A.plot()

   @savefig series_plot_secondary_y.png width=4.5in
   df.B.plot(secondary_y=True, style='g')


Selective Plotting on Secondary Y-axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To plot some columns in a DataFrame, give the column names to the `secondary_y`
keyword:

.. ipython:: python

   plt.figure()

   @savefig frame_plot_secondary_y.png width=4.5in
   df.plot(secondary_y=['A', 'B'])

Note that the columns plotted on the secondary y-axis is automatically marked
with "(right)" in the legend. To turn off the automatic marking, use the
`mark_right=False` keyword:

.. ipython:: python

   plt.figure()

   @savefig frame_plot_secondary_y.png width=4.5in
   df.plot(secondary_y=['A', 'B'], mark_right=False)


Targeting different subplots
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can pass an ``ax`` argument to ``Series.plot`` to plot on a particular axis:

.. ipython:: python<commit_msg>DOC: describe suppression of auto tick locator adjustment #2205<commit_after>.. currentmodule:: pandas
.. _visualization:

.. ipython:: python
   :suppress:

   import numpy as np
   np.random.seed(123456)
   from pandas import *
   import pandas.util.testing as tm
   randn = np.random.randn
   np.set_printoptions(precision=4, suppress=True)
   import matplotlib.pyplot as plt
   plt.close('all')

************************
Plotting with matplotlib
************************

.. note::

    We intend to build more plotting integration with `matplotlib
    <http://matplotlib.sourceforge.net>`__ as time goes on.

We use the standard convention for referencing the matplotlib API:

.. ipython:: python

   import matplotlib.pyplot as plt

.. _visualization.basic:

Basic plotting: ``plot``
------------------------

The ``plot`` method on Series and DataFrame is just a simple wrapper around
``plt.plot``:

.. ipython:: python

   ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
   ts = ts.cumsum()

   @savefig series_plot_basic.png width=4.5in
   ts.plot()

If the index consists of dates, it calls ``gcf().autofmt_xdate()`` to try to
format the x-axis nicely as per above. The method takes a number of arguments
for controlling the look of the plot:

.. ipython:: python

   @savefig series_plot_basic2.png width=4.5in
   plt.figure(); ts.plot(style='k--', label='Series'); plt.legend()

On DataFrame, ``plot`` is a convenience to plot all of the columns with labels:

.. ipython:: python

   df = DataFrame(randn(1000, 4), index=ts.index,
                  columns=['A', 'B', 'C', 'D'])
   df = df.cumsum()

   @savefig frame_plot_basic.png width=4.5in
   plt.figure(); df.plot(); plt.legend(loc='best')

You may set the ``legend`` argument to ``False`` to hide the legend, which is
shown by default.

.. ipython:: python

   @savefig frame_plot_basic_noleg.png width=4.5in
   df.plot(legend=False)

Some other options are available, like plotting each Series on a different axis:

.. ipython:: python

   @savefig frame_plot_subplots.png width=4.5in
   df.plot(subplots=True, figsize=(8, 8)); plt.legend(loc='best')

You may pass ``logy`` to get a log-scale Y axis.

.. ipython:: python

   plt.figure();

   ts = Series(randn(1000), index=date_range('1/1/2000', periods=1000))
   ts = np.exp(ts.cumsum())

   @savefig series_plot_logy.png width=4.5in
   ts.plot(logy=True)

You can plot one column versus another using the `x` and `y` keywords in
`DataFrame.plot`:

.. ipython:: python

   plt.figure()

   df3 = DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
   df3['A'] = Series(range(len(df)))

   @savefig df_plot_xy.png width=4.5in
   df3.plot(x='A', y='B')


Plotting on a Secondary Y-axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To plot data on a secondary y-axis, use the ``secondary_y`` keyword:

.. ipython:: python

   plt.figure()

   df.A.plot()

   @savefig series_plot_secondary_y.png width=4.5in
   df.B.plot(secondary_y=True, style='g')


Selective Plotting on Secondary Y-axis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To plot some columns in a DataFrame, give the column names to the ``secondary_y``
keyword:

.. ipython:: python

   plt.figure()

   @savefig frame_plot_secondary_y.png width=4.5in
   df.plot(secondary_y=['A', 'B'])

Note that the columns plotted on the secondary y-axis is automatically marked
with "(right)" in the legend. To turn off the automatic marking, use the
``mark_right=False`` keyword:

.. ipython:: python

   plt.figure()

   @savefig frame_plot_secondary_y.png width=4.5in
   df.plot(secondary_y=['A', 'B'], mark_right=False)


Suppressing tick resolution adjustment
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Pandas includes automatically tick resolution adjustment for regular frequency
time-series data. For limited cases where pandas cannot infer the frequency
information (e.g., in an externally created ``twinx``), you can choose to
suppress this behavior for alignment purposes.

Here is the default behavior, notice how the x-axis tick labelling is performed:

.. ipython:: python

   plt.figure()

   @savefig ser_plot_suppress.png width=4.5in
   df.A.plot()


Using the ``x_compat`` parameter, you can suppress this bevahior:

.. ipython:: python

   plt.figure()

   @savefig ser_plot_suppress_parm.png width=4.5in
   df.A.plot(x_compat=True)


If you have more than one plot that needs to be suppressed, the ``use`` method
in ``pandas.plot_params`` can be used in a `with statement`:

.. ipython:: python

   import pandas as pd

   plt.figure()

   @savefig ser_plot_suppress_context.png width=4.5in
   with pd.plot_params.use('x_compat', True):
       df.A.plot(color='r')
       df.B.plot(color='g')
       df.C.plot(color='b')


Targeting different subplots
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can pass an ``ax`` argument to ``Series.plot`` to plot on a particular axis:

.. ipython:: python
{'fraction_non_alphanumeric': 0.16461574181564895, 'fraction_numerical': 0.017645693057812863, 'mean_word_length': 3.892106757524134, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3074322', 'n_tokens_mistral': 2986, 'n_tokens_neox': 2778, 'n_words': 869}