Visualization RST
Visualization RST
_visualization:
{{ header }}
*******************
Chart visualization
*******************
.. note::
.. ipython:: python
plt.close("all")
.. note::
.. _visualization.basic:
The ``plot`` method on Series and DataFrame is just a simple wrapper around
:meth:`plt.plot() <matplotlib.axes.Axes.plot>`:
.. ipython:: python
np.random.seed(123456)
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000",
periods=1000))
ts = ts.cumsum()
@savefig series_plot_basic.png
ts.plot();
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
plt.figure();
@savefig frame_plot_basic.png
df.plot();
You can plot one column versus another using the ``x`` and ``y`` keywords in
:meth:`~DataFrame.plot`:
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
np.random.seed(123456)
.. ipython:: python
@savefig df_plot_xy.png
df3.plot(x="A", y="B");
.. note::
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.other:
Other plots
-----------
Plotting methods allow for a handful of plot styles other than the
default line plot. These methods can be provided as the ``kind``
keyword argument to :meth:`~DataFrame.plot`, and include:
.. ipython:: python
plt.figure();
@savefig bar_plot_ex.png
df.iloc[5].plot(kind="bar");
You can also create these other plots using the methods ``DataFrame.plot.<kind>``
instead of providing the ``kind`` keyword argument. This makes it easier to
discover plot methods and the specific arguments they use:
.. ipython::
:verbatim:
In [14]: df = pd.DataFrame()
.. _visualization.barplot:
Bar plots
~~~~~~~~~
For labeled, non-time series data, you may wish to produce a bar plot:
.. ipython:: python
plt.figure();
@savefig bar_plot_ex.png
df.iloc[5].plot.bar();
plt.axhline(0, color="k");
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
np.random.seed(123456)
.. ipython:: python
@savefig bar_plot_multi_ex.png
df2.plot.bar();
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
.. ipython:: python
@savefig bar_plot_stacked_ex.png
df2.plot.bar(stacked=True);
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
.. ipython:: python
@savefig barh_plot_stacked_ex.png
df2.plot.barh(stacked=True);
.. _visualization.hist:
Histograms
~~~~~~~~~~
.. ipython:: python
df4 = pd.DataFrame(
{
"a": np.random.randn(1000) + 1,
"b": np.random.randn(1000),
"c": np.random.randn(1000) - 1,
},
columns=["a", "b", "c"],
)
plt.figure();
@savefig hist_new.png
df4.plot.hist(alpha=0.5);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig hist_new_stacked.png
df4.plot.hist(stacked=True, bins=20);
.. ipython:: python
:suppress:
plt.close("all")
You can pass other keywords supported by matplotlib ``hist``. For example,
horizontal and cumulative histograms can be drawn by
``orientation='horizontal'`` and ``cumulative=True``.
.. ipython:: python
plt.figure();
@savefig hist_new_kwargs.png
df4["a"].plot.hist(orientation="horizontal", cumulative=True);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig hist_plot_ex.png
df["A"].diff().hist();
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig frame_hist_ex.png
df.diff().hist(color="k", alpha=0.5, bins=50);
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
np.random.seed(123456)
.. ipython:: python
data = pd.Series(np.random.randn(1000))
@savefig grouped_hist.png
data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4));
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. versionchanged:: 1.4.0
.. ipython:: python
data = pd.DataFrame(
{
"a": np.random.choice(["x", "y", "z"], 1000),
"b": np.random.choice(["e", "f", "g"], 1000),
"c": np.random.randn(1000),
"d": np.random.randn(1000) - 1,
},
)
@savefig grouped_hist_by.png
data.plot.hist(by=["a", "b"], figsize=(10, 5));
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.box:
Box plots
~~~~~~~~~
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
@savefig box_plot_new.png
df.plot.box();
Boxplot can be colorized by passing ``color`` keyword. You can pass a ``dict``
whose keys are ``boxes``, ``whiskers``, ``medians`` and ``caps``.
If some keys are missing in the ``dict``, default colors are used
for the corresponding artists. Also, boxplot has ``sym`` keyword to specify fliers
style.
When you pass other type of arguments via ``color`` keyword, it will be directly
passed to matplotlib for all the ``boxes``, ``whiskers``, ``medians`` and ``caps``
colorization.
.. ipython:: python
color = {
"boxes": "DarkGreen",
"whiskers": "DarkOrange",
"medians": "DarkBlue",
"caps": "Gray",
}
@savefig box_new_colorize.png
df.plot.box(color=color, sym="r+");
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
@savefig box_new_kwargs.png
df.plot.box(vert=False, positions=[1, 4, 5, 6, 8]);
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
:okwarning:
df = pd.DataFrame(np.random.rand(10, 5))
plt.figure();
@savefig box_plot_ex.png
bp = df.boxplot()
You can create a stratified boxplot using the ``by`` keyword argument to create
groupings. For instance,
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
:okwarning:
@savefig box_plot_ex2.png
bp = df.boxplot(by="X")
You can also pass a subset of columns to plot, as well as group by multiple
columns:
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
:okwarning:
plt.figure();
@savefig box_plot_ex3.png
bp = df.boxplot(column=["Col1", "Col2"], by=["X", "Y"])
.. ipython:: python
:suppress:
plt.close("all")
.. versionchanged:: 1.4.0
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
.. ipython:: python
:okwarning:
plt.figure();
@savefig box_plot_ex4.png
bp = df.plot.box(column=["Col1", "Col2"], by="X")
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.box.return:
In ``boxplot``, the return type can be controlled by the ``return_type``, keyword.
The valid choices are ``{"axes", "dict", "both", None}``.
Faceting, created by ``DataFrame.boxplot`` with the ``by``
keyword, will affect the output type as well:
.. ipython:: python
:okwarning:
np.random.seed(1234)
df_box = pd.DataFrame(np.random.randn(50, 2))
df_box["g"] = np.random.choice(["A", "B"], size=50)
df_box.loc[df_box["g"] == "B", 1] += 3
@savefig boxplot_groupby.png
bp = df_box.boxplot(by="g")
.. ipython:: python
:suppress:
plt.close("all")
The subplots above are split by the numeric columns first, then the value of
the ``g`` column. Below the subplots are first split by the value of ``g``,
then by the numeric columns.
.. ipython:: python
:okwarning:
@savefig groupby_boxplot_vis.png
bp = df_box.groupby("g").boxplot()
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.area_plot:
Area plot
~~~~~~~~~
When input data contains ``NaN``, it will be automatically filled by 0. If you want
to drop or fill by different values, use :func:`dataframe.dropna`
or :func:`dataframe.fillna` before calling ``plot``.
.. ipython:: python
:suppress:
np.random.seed(123456)
plt.figure()
.. ipython:: python
@savefig area_plot_stacked.png
df.plot.area();
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
.. ipython:: python
@savefig area_plot_unstacked.png
df.plot.area(stacked=False);
.. _visualization.scatter:
Scatter plot
~~~~~~~~~~~~
.. ipython:: python
:suppress:
np.random.seed(123456)
plt.close("all")
plt.figure()
.. ipython:: python
@savefig scatter_plot.png
df.plot.scatter(x="a", y="b");
To plot multiple column groups in a single axes, repeat ``plot`` method specifying
target ``ax``.
It is recommended to specify ``color`` and ``label`` keywords to distinguish each
groups.
.. ipython:: python
:okwarning:
.. ipython:: python
:suppress:
plt.close("all")
The keyword ``c`` may be given as the name of a column to provide colors for
each point:
.. ipython:: python
@savefig scatter_plot_colored.png
df.plot.scatter(x="a", y="b", c="c", s=50);
.. ipython:: python
:suppress:
plt.close("all")
.. versionadded:: 1.3.0
.. ipython:: python
@savefig scatter_plot_categorical.png
df.plot.scatter(x="a", y="b", c="species", cmap="viridis", s=50);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
@savefig scatter_plot_bubble.png
df.plot.scatter(x="a", y="b", s=df["c"] * 200);
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.hexbin:
.. ipython:: python
:suppress:
plt.figure()
np.random.seed(123456)
.. ipython:: python
@savefig hexbin_plot.png
df.plot.hexbin(x="a", y="b", gridsize=25);
By default, a histogram of the counts around each ``(x, y)`` point is computed.
You can specify alternative aggregations by passing values to the ``C`` and
``reduce_C_function`` arguments. ``C`` specifies the value at each ``(x, y)`` point
and ``reduce_C_function`` is a function of one argument that reduces all the
values in a bin to a single number (e.g. ``mean``, ``max``, ``sum``, ``std``). In
this
example the positions are given by columns ``a`` and ``b``, while the value is
given by column ``z``. The bins are aggregated with NumPy's ``max`` function.
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
np.random.seed(123456)
.. ipython:: python
@savefig hexbin_plot_agg.png
df.plot.hexbin(x="a", y="b", C="z", reduce_C_function=np.max, gridsize=25);
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.pie:
Pie plot
~~~~~~~~
.. ipython:: python
:suppress:
np.random.seed(123456)
plt.figure()
.. ipython:: python
:okwarning:
@savefig series_pie_plot.png
series.plot.pie(figsize=(6, 6));
.. ipython:: python
:suppress:
plt.close("all")
For pie plots it's best to use square figures, i.e. a figure aspect ratio 1.
You can create the figure with equal width and height, or force the aspect ratio
to be equal after plotting by calling ``ax.set_aspect('equal')`` on the returned
``axes`` object.
Note that pie plot with :class:`DataFrame` requires that you either specify a
target column by the ``y`` argument or ``subplots=True``. When ``y`` is
specified, pie plot of selected column will be drawn. If ``subplots=True`` is
specified, pie plots for each column are drawn as subplots. A legend will be
drawn in each pie plots by default; specify ``legend=False`` to hide it.
.. ipython:: python
:suppress:
np.random.seed(123456)
plt.figure()
.. ipython:: python
df = pd.DataFrame(
3 * np.random.rand(4, 2), index=["a", "b", "c", "d"], columns=["x", "y"]
)
@savefig df_pie_plot.png
df.plot.pie(subplots=True, figsize=(8, 4));
.. ipython:: python
:suppress:
plt.close("all")
You can use the ``labels`` and ``colors`` keywords to specify the labels and colors
of each wedge.
.. warning::
Most pandas plots use the ``label`` and ``color`` arguments (note the lack of
"s" on those).
To be consistent with :func:`matplotlib.pyplot.pie` you must use ``labels`` and
``colors``.
.. ipython:: python
:suppress:
plt.figure()
.. ipython:: python
@savefig series_pie_plot_options.png
series.plot.pie(
labels=["AA", "BB", "CC", "DD"],
colors=["r", "g", "b", "c"],
autopct="%.2f",
fontsize=20,
figsize=(6, 6),
);
If you pass values whose sum total is less than 1.0 they will be rescaled so that
they sum to 1.
.. ipython:: python
:suppress:
plt.close("all")
plt.figure()
.. ipython:: python
:okwarning:
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.missing_data:
+----------------+--------------------------------------+
| Plot Type | NaN Handling |
+================+======================================+
| Line | Leave gaps at NaNs |
+----------------+--------------------------------------+
| Line (stacked) | Fill 0's |
+----------------+--------------------------------------+
| Bar | Fill 0's |
+----------------+--------------------------------------+
| Scatter | Drop NaNs |
+----------------+--------------------------------------+
| Histogram | Drop NaNs (column-wise) |
+----------------+--------------------------------------+
| Box | Drop NaNs (column-wise) |
+----------------+--------------------------------------+
| Area | Fill 0's |
+----------------+--------------------------------------+
| KDE | Drop NaNs (column-wise) |
+----------------+--------------------------------------+
| Hexbin | Drop NaNs |
+----------------+--------------------------------------+
| Pie | Fill 0's |
+----------------+--------------------------------------+
If any of these defaults are not what you want, or if you want to be
explicit about how missing values are handled, consider using
:meth:`~pandas.DataFrame.fillna` or :meth:`~pandas.DataFrame.dropna`
before plotting.
.. _visualization.tools:
Plotting tools
--------------
.. _visualization.scatter_matrix:
Scatter matrix plot
~~~~~~~~~~~~~~~~~~~
.. ipython:: python
:suppress:
np.random.seed(123456)
.. ipython:: python
@savefig scatter_matrix_kde.png
scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal="kde");
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.kde:
Density plot
~~~~~~~~~~~~
.. ipython:: python
:suppress:
plt.figure()
np.random.seed(123456)
.. ipython:: python
ser = pd.Series(np.random.randn(1000))
@savefig kde_plot.png
ser.plot.kde();
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.andrews_curves:
Andrews curves
~~~~~~~~~~~~~~
.. ipython:: python
data = pd.read_csv("data/iris.data")
plt.figure();
@savefig andrews_curves.png
andrews_curves(data, "Name");
.. _visualization.parallel_coordinates:
Parallel coordinates
~~~~~~~~~~~~~~~~~~~~
.. ipython:: python
data = pd.read_csv("data/iris.data")
plt.figure();
@savefig parallel_coordinates.png
parallel_coordinates(data, "Name");
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.lag:
Lag plot
~~~~~~~~
Lag plots are used to check if a data set or time series is random. Random
data should not exhibit any structure in the lag plot. Non-random structure
implies that the underlying data are not random. The ``lag`` argument may
be passed, and when ``lag=1`` the plot is essentially ``data[:-1]`` vs.
``data[1:]``.
.. ipython:: python
:suppress:
np.random.seed(123456)
.. ipython:: python
plt.figure();
@savefig lag_plot.png
lag_plot(data);
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.autocorrelation:
Autocorrelation plot
~~~~~~~~~~~~~~~~~~~~
Autocorrelation plots are often used for checking randomness in time series.
This is done by computing autocorrelations for data values at varying time lags.
If time series is random, such autocorrelations should be near zero for any and
all time-lag separations. If time series is non-random then one or more of the
autocorrelations will be significantly non-zero. The horizontal lines displayed
in the plot correspond to 95% and 99% confidence bands. The dashed line is 99%
confidence band. See the
`Wikipedia entry <https://en.wikipedia.org/wiki/Correlogram>`__ for more about
autocorrelation plots.
.. ipython:: python
:suppress:
np.random.seed(123456)
.. ipython:: python
plt.figure();
@savefig autocorrelation_plot.png
autocorrelation_plot(data);
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.bootstrap:
Bootstrap plot
~~~~~~~~~~~~~~
Bootstrap plots are used to visually assess the uncertainty of a statistic, such
as mean, median, midrange, etc. A random subset of a specified size is selected
from a data set, the statistic in question is computed for this subset and the
process is repeated a specified number of times. Resulting plots and histograms
are what constitutes the bootstrap plot.
.. ipython:: python
:suppress:
np.random.seed(123456)
.. ipython:: python
data = pd.Series(np.random.rand(1000))
@savefig bootstrap_plot.png
bootstrap_plot(data, size=50, samples=500, color="grey");
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.radviz:
RadViz
~~~~~~
.. ipython:: python
plt.figure();
@savefig radviz.png
radviz(data, "Name");
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.formatting:
Plot formatting
---------------
From version 1.5 and up, matplotlib offers a range of pre-configured plotting
styles. Setting the
style can be used to easily give plots the general look that you want.
Setting the style is as easy as calling ``matplotlib.style.use(my_plot_style)``
before
creating your plot. For example you could write ``matplotlib.style.use('ggplot')``
for ggplot-style
plots.
You can see the various available style names at ``matplotlib.style.available`` and
it's very
easy to try them out.
Most plotting methods have a set of keyword arguments that control the
layout and formatting of the returned plot:
.. ipython:: python
plt.figure();
@savefig series_plot_basic2.png
ts.plot(style="k--", label="Series");
.. ipython:: python
:suppress:
plt.close("all")
For each kind of plot (e.g. ``line``, ``bar``, ``scatter``) any additional
arguments
keywords are passed along to the corresponding matplotlib function
(:meth:`ax.plot() <matplotlib.axes.Axes.plot>`,
:meth:`ax.bar() <matplotlib.axes.Axes.bar>`,
:meth:`ax.scatter() <matplotlib.axes.Axes.scatter>`). These can be used
to control additional styling, beyond what pandas provides.
You may set the ``legend`` argument to ``False`` to hide the legend, which is
shown by default.
.. ipython:: python
:suppress:
np.random.seed(123456)
.. ipython:: python
@savefig frame_plot_basic_noleg.png
df.plot(legend=False);
.. ipython:: python
:suppress:
plt.close("all")
You may set the ``xlabel`` and ``ylabel`` arguments to give the plot custom labels
for x and y axis. By default, pandas will pick up index name as xlabel, while
leaving
it empty for ylabel.
.. ipython:: python
:suppress:
plt.figure();
.. ipython:: python
df.plot();
@savefig plot_xlabel_ylabel.png
df.plot(xlabel="new x", ylabel="new y");
.. ipython:: python
:suppress:
plt.close("all")
Scales
~~~~~~
.. ipython:: python
:suppress:
plt.figure()
np.random.seed(123456)
.. ipython:: python
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000",
periods=1000))
ts = np.exp(ts.cumsum())
@savefig series_plot_logy.png
ts.plot(logy=True);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
:suppress:
plt.figure()
.. ipython:: python
df["A"].plot();
@savefig series_plot_secondary_y.png
df["B"].plot(secondary_y=True, style="g");
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
ax = df.plot(secondary_y=["A", "B"])
ax.set_ylabel("CD scale");
@savefig frame_plot_secondary_y.png
ax.right_ax.set_ylabel("AB scale");
.. ipython:: python
:suppress:
plt.close("all")
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_no_right.png
df.plot(secondary_y=["A", "B"], mark_right=False);
.. ipython:: python
:suppress:
plt.close("all")
.. _plotting.formatters:
pandas provides custom formatters for timeseries plots. These change the
formatting of the axis labels for dates and times. By default,
the custom formatters are applied only to plots created by pandas with
:meth:`DataFrame.plot` or :meth:`Series.plot`. To have them apply to all
plots, including those made by matplotlib, set the option
``pd.options.plotting.matplotlib.register_converters = True`` or use
:meth:`pandas.plotting.register_matplotlib_converters`.
Here is the default behavior, notice how the x-axis tick labeling is performed:
.. ipython:: python
plt.figure();
@savefig ser_plot_suppress.png
df["A"].plot();
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig ser_plot_suppress_parm.png
df["A"].plot(x_compat=True);
.. ipython:: python
:suppress:
plt.close("all")
If you have more than one plot that needs to be suppressed, the ``use`` method
in ``pandas.plotting.plot_params`` can be used in a ``with`` statement:
.. ipython:: python
plt.figure();
@savefig ser_plot_suppress_context.png
with pd.plotting.plot_params.use("x_compat", True):
df["A"].plot(color="r")
df["B"].plot(color="g")
df["C"].plot(color="b")
.. ipython:: python
:suppress:
plt.close("all")
Subplots
~~~~~~~~
.. ipython:: python
@savefig frame_plot_subplots.png
df.plot(subplots=True, figsize=(6, 6));
.. ipython:: python
:suppress:
plt.close("all")
The layout of subplots can be specified by the ``layout`` keyword. It can accept
``(rows, columns)``. The ``layout`` keyword can be used in
``hist`` and ``boxplot`` also. If the input is invalid, a ``ValueError`` will be
raised.
The number of axes which can be contained by rows x columns specified by ``layout``
must be
larger than the number of required subplots. If layout can contain more axes than
required,
blank axes are not drawn. Similar to a NumPy array's ``reshape`` method, you
can use ``-1`` for one dimension to automatically calculate the number of rows
or columns needed, given the other.
.. ipython:: python
@savefig frame_plot_subplots_layout.png
df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
.. ipython:: python
:suppress:
plt.close("all")
The required number of columns (3) is inferred from the number of series to plot
and the given number of rows (2).
You can pass multiple axes created beforehand as list-like via ``ax`` keyword.
This allows more complicated layouts.
The passed axes must be the same number as the subplots being drawn.
When multiple axes are passed via the ``ax`` keyword, ``layout``, ``sharex`` and
``sharey`` keywords
don't affect to the output. You should explicitly pass ``sharex=False`` and
``sharey=False``,
otherwise you will see a warning.
.. ipython:: python
.. ipython:: python
:suppress:
plt.close("all")
np.random.seed(123456)
ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000",
periods=1000))
ts = ts.cumsum()
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.errorbars:
Horizontal and vertical error bars can be supplied to the ``xerr`` and ``yerr``
keyword arguments to :meth:`~DataFrame.plot()`. The error values can be specified
using a variety of formats:
Here is an example of one way to easily plot group means with standard deviations
from the raw data.
.. ipython:: python
df3 = pd.DataFrame(
{
"data1": [9, 3, 2, 4, 3, 2, 4, 6, 3, 2],
"data2": [9, 6, 5, 7, 5, 4, 5, 6, 5, 1],
},
index=ix3,
)
# Group by index labels and take the means and standard deviations
# for each group
gp3 = df3.groupby(level=("letter", "word"))
means = gp3.mean()
errors = gp3.std()
means
errors
# Plot
fig, ax = plt.subplots()
@savefig errorbar_example.png
means.plot.bar(yerr=errors, ax=ax, capsize=4, rot=0);
.. ipython:: python
:suppress:
plt.close("all")
Asymmetrical error bars are also supported, however raw error values must be
provided in this case. For a ``N`` length :class:`Series`, a ``2xN`` array should
be provided indicating lower and upper (or left and right) errors. For a
``MxN`` :class:`DataFrame`, asymmetrical errors should be in a ``Mx2xN`` array.
Here is an example of one way to plot the min/max range using asymmetrical error
bars.
.. ipython:: python
mins = gp3.min()
maxs = gp3.max()
# Plot
fig, ax = plt.subplots()
@savefig errorbar_asymmetrical_example.png
means.plot.bar(yerr=errors, ax=ax, capsize=4, rot=0);
.. ipython:: python
:suppress:
plt.close("all")
.. _visualization.table:
Plotting tables
~~~~~~~~~~~~~~~
.. ipython:: python
np.random.seed(123456)
fig, ax = plt.subplots(1, 1, figsize=(7, 6.5))
df = pd.DataFrame(np.random.rand(5, 3), columns=["a", "b", "c"])
ax.xaxis.tick_top() # Display x-axis ticks on top.
@savefig line_plot_table_true.png
df.plot(table=True, ax=ax);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
@savefig line_plot_table_data.png
df.plot(table=np.round(df.T, 2), ax=ax);
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
fig, ax = plt.subplots(1, 1)
table(ax, np.round(df.describe(), 2), loc="upper right", colWidths=[0.2, 0.2,
0.2]);
@savefig line_plot_table_describe.png
df.plot(ax=ax, ylim=(0, 2), legend=None);
.. ipython:: python
:suppress:
plt.close("all")
**Note**: You can get table instances on the axes using ``axes.tables`` property
for further decorations. See the `matplotlib table documentation
<https://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.table>`__ for more.
.. _visualization.colormaps:
Colormaps
~~~~~~~~~
As matplotlib does not directly support colormaps for line-based plots, the
colors are selected based on an even spacing determined by the number of columns
in the ``DataFrame``. There is no consideration made for background color, so some
colormaps will produce lines that are not easily visible.
.. ipython:: python
np.random.seed(123456)
df = pd.DataFrame(np.random.randn(1000, 10), index=ts.index)
df = df.cumsum()
plt.figure();
@savefig cubehelix.png
df.plot(colormap="cubehelix");
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
@savefig cubehelix_cm.png
df.plot(colormap=cm.cubehelix);
.. ipython:: python
:suppress:
plt.close("all")
Colormaps can also be used other plot types, like bar charts:
.. ipython:: python
np.random.seed(123456)
dd = pd.DataFrame(np.random.randn(10, 10)).map(abs)
dd = dd.cumsum()
plt.figure();
@savefig greens.png
dd.plot.bar(colormap="Greens");
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig parallel_gist_rainbow.png
parallel_coordinates(data, "Name", colormap="gist_rainbow");
.. ipython:: python
:suppress:
plt.close("all")
.. ipython:: python
plt.figure();
@savefig andrews_curve_winter.png
andrews_curves(data, "Name", colormap="winter");
.. ipython:: python
:suppress:
plt.close("all")
pandas also automatically registers formatters and locators that recognize date
indices, thereby extending date and time support to practically all plot types
available in matplotlib. Although this formatting does not provide the same
level of refinement you would get when plotting via pandas, it can be faster
when plotting a large number of points.
.. ipython:: python
np.random.seed(123456)
price = pd.Series(
np.random.randn(150).cumsum(),
index=pd.date_range("2000-1-1", periods=150, freq="B"),
)
ma = price.rolling(20).mean()
mstd = price.rolling(20).std()
plt.figure();
.. ipython:: python
:suppress:
plt.close("all")
Plotting backends
-----------------
.. code-block:: python
Alternatively, you can also set this option globally, do you don't need to specify
the keyword in each ``plot`` call. For example:
.. code-block:: python
Or:
.. code-block:: python
.. code-block:: python
The backend module can then use other visualization tools (Bokeh, Altair,
hvplot,...)
to generate the plots. Some libraries implementing a backend for pandas are listed
on `the ecosystem page <https://pandas.pydata.org/community/ecosystem.html>`_.