Comparison with Pandas API#

This page recreates the pandas chart visualization guide. It shows how to configure and control the output of pd.DataFrame.plot and pd.Series.plot to generate hvPlot plots. For comparison, the page includes both the Pandas & Matplotlib and Pandas & hvPlot plot versions.

Note

You will find many notes in this page that are used to highlight differences between Pandas and hvPlot.

Tip

We generally recommend using hvPlot by installing the hvplot namespace on Pandas objects with import hvplot.pandas.

import numpy as np
import pandas as pd

pd.options.plotting.backend = 'hvplot'

Basic Plotting: plot#

The plot method on Series and DataFrame is just a simple wrapper around hvPlot():

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot(backend='matplotlib');
../../../_images/2a1fae660a0e993958997b36c4ca41b6c59935daeb9da841675f9039b73ab951.png
ts.plot()

If the index consists of dates, they will be formatted nicely along the x-axis as per above.

Note

Dates will be sorted as long as sort_date=True.

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

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))
df = df.cumsum()
df.plot(backend='matplotlib');
../../../_images/6b46c4a479ecb5b4a8ba26b3eb52f7d7d3d7f9bbc05b08bc8e318d2c8a91bcaa.png
df.plot()

Note

The default colors used in hvPlot are different from pandas but you can pass your preferred colors as a list via the color keyword.

df.plot(color=['#1F77B4', '#FF7F0D', '#B5DDB5', '#D93A3B'])

You can plot one column versus another using the x and y keywords in plot():

df3 = pd.DataFrame(np.random.randn(1000, 2), columns=['B', 'C']).cumsum()
df3['A'] = pd.Series(list(range(len(df))))
df3.plot(x='A', y='B', backend='matplotlib');
../../../_images/6c03a6179104099c1a52af4e581cb47140ad801120036787f0dd0837cf09d678.png
df3.plot(x='A', y='B')

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 plot(). These include:

  • bar or barh for bar plots

  • hist for histogram

  • box for boxplot

  • kde or density for density plots

  • area for area plots

  • scatter for scatter plots

  • hexbin for hexagonal bin plots

  • [NOT IMPLEMENTED]: ‘pie’ for pie plots

For example, a bar plot can be created the following way:

df.iloc[5].plot(kind='bar', backend='matplotlib');
../../../_images/0d685b6dae2573fe38dd1fdccf232db61e3882257d889e7d4119a76b217cac3c.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.

In addition to these kinds, there are the DataFrame.hist(), and DataFrame.boxplot() methods, which use a separate interface.

Finally, there are several plotting functions in hvplot.plotting that take a Series or DataFrame as an argument. These include:

  • Scatter Matrix

  • Andrews Curves

  • Parallel Coordinates

  • Lag Plot

Note

The following plots are not currently supported in hvPlot:

  • Autocorrelation Plot

  • Bootstrap Plot

  • RadViz

Plots may also be adorned with errorbars or tables.

Bar plots#

For labeled, non-time series data, you may wish to produce a bar plot:

df.iloc[5].plot.bar(backend='matplotlib');
../../../_images/0d685b6dae2573fe38dd1fdccf232db61e3882257d889e7d4119a76b217cac3c.png
df.iloc[5].plot.bar()

Calling a DataFrame’s plot.bar() method produces a multiple bar plot:

df2 = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df2.plot.bar(backend='matplotlib');
../../../_images/fc65c76edbd0bf01fcc536b84c4411eddb8f2e66c12a19e66dc132f727aa1739.png
df2.plot.bar()

To produce a stacked bar plot, pass stacked=True:

df2.plot.bar(stacked=True, backend='matplotlib');
../../../_images/32664bfd5e0694218b29ae69e82975747a46e51c4883fbbdf2467ca20abd5639.png
df2.plot.bar(stacked=True)

To get horizontal bar plots, use the barh method:

df2.plot.barh(stacked=True, backend='matplotlib');
../../../_images/065dba655185aae48ba06d4cf3d38f76461772652bd08d662440820a7d63af7b.png
df2.plot.barh(stacked=True)

Histograms#

Histogram can be drawn by using the DataFrame.plot.hist() and Series.plot.hist() methods.

df4 = pd.DataFrame({'a': np.random.randn(1000) + 1, 'b': np.random.randn(1000),
                    'c': np.random.randn(1000) - 1}, columns=['a', 'b', 'c'])

df4.plot.hist(alpha=0.5, backend='matplotlib');
../../../_images/f09b2d61d89797e267864d97fb5acde6b51289682667aef9bd1e3b9b81e19547.png
df4.plot.hist(alpha=0.5)

Note

Stacking for histograms is not yet supported in hvPlot

# df4.plot.hist(stacked=True, bins=20);

You can pass other keywords. For example, horizontal and cumulative histogram can be drawn by invert=True and cumulative=True.

df4['a'].plot.hist(orientation='horizontal', cumulative=True, backend='matplotlib');
../../../_images/7ae592708f34cc918089c3371d0d3abfe2685a8e1e0972387eef75058c3d57c3.png

Note

hvPlot uses invert=True instead of orientation='horizontal'.

df4['a'].plot.hist(invert=True, cumulative=True)

The existing interface DataFrame.hist to plot histogram still can be used.

df['A'].diff().hist(backend='matplotlib');
../../../_images/099de84091be8847dc9da314ac7c70a2e13358400fee45cff39da278219acd66.png
df['A'].diff().hist()

Note

Pandas’ DataFrame.hist() plots the histograms of the columns on multiple subplots. hvPlot creates instead an overlay of histogram plots. To reproduce Pandas’ behavior, you can set subplots=True to create a layout of plots (1 per column in this case), and additionally call .cols(2) on the object returned to lay the plots in a layout with a maximum number of 2 columns.

df.diff().hist(color='k', alpha=0.5, bins=50, backend='matplotlib');
../../../_images/e23f6d0d3cc7d3a65be4ba158910214046bbf2fa5b0b1895433444dc9e1ef858.png
df.diff().hist(color='k', alpha=0.5, bins=50, subplots=True, width=300).cols(2)

The by keyword can be specified to plot grouped histograms:

data = pd.Series(np.random.randn(1000))
df5 = pd.DataFrame({'data': data, 'by_column': np.random.randint(0, 4, 1000)})

data.hist(by=np.random.randint(0, 4, 1000), figsize=(6, 4), backend='matplotlib');
../../../_images/d669d7c2996b43ffc5ce74c322b0d1b36520b15669b81959212c74f400557828.png

Note

Pandas’ hist method accepts a Numpy NdArray for by but hvPlot does not, which is why in the example above an extra column 'by_column' was created.

df5.hist(by='by_column', width=300, subplots=True).cols(2)

In Pandas, the by keyword can also be specified in DataFrame.plot.hist().

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,
    },
)
data.plot.hist(by=["a", "b"], figsize=(10, 5), backend='matplotlib');
../../../_images/c77d3bea936e58b6f8ae26039839e7cbc5c7b673bb5ab2a3ab84996628d7e2d8.png

Note

This is not yet supported in hvPlot but a similar plot can be generated using HoloViews directly.

import holoviews as hv
from holoviews.operation import histogram

ds = hv.Dataset(data, kdims=['a', 'b'], vdims=['c', 'd'])
dsg = ds.groupby(['a', 'b'])
(
    hv.NdLayout(histogram(dsg, dimension='c'))
    * hv.NdLayout(histogram(dsg, dimension='d'))
).opts(
    hv.opts.Histogram(height=100, width=800)
).cols(1)

Box Plots#

Boxplot can be drawn by calling Series.plot.box() and DataFrame.plot.box(), or DataFrame.boxplot() to visualize the distribution of values within each column.

For instance, here is a boxplot representing five trials of 10 observations of a uniform random variable on [0,1).

df = pd.DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
df.plot.box(backend='matplotlib');
../../../_images/49e2d25fa81e79462dcd3c6ffaf662d4042133bed74ae808127e541f2938b86b.png
df.plot.box()

Using the matplotlib backend, 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.

color = {
    "boxes": "DarkGreen",
    "whiskers": "DarkOrange",
    "medians": "DarkBlue",
    "caps": "Gray",
}

df.plot.box(color=color, sym='r+', backend='matplotlib');
../../../_images/007f2f1df3f7f718164c8b321a9c62cc7f0e6a2fcc6a1f4d61d857734baaf825.png

Note

Setting color this way or sym isn’t supported in hvPlot. However hvPlot supports passing down backend-specific style options that allow some level of customization.

df.plot.box(
    box_fill_color='white',
    box_line_color='DarkGreen',
    whisker_line_color='DarkOrange',
    outlier_color='red',
)

In Pandas, you can pass other keywords supported by matplotlib boxplot. For example, horizontal and custom-positioned boxplot can be drawn by vert=False and positions keywords.

df.plot.box(vert=False, positions=[1, 4, 5, 6, 8], backend='matplotlib');
../../../_images/7bd65a42b6092c9d548f22712885d2f4463a22fdece40fe22b5f252361ba07b6.png

Note

vert and positions are not supported by hvPlot and the Bokeh extension. Instead, and for example, horizontal boxplots can be drawn by invert=True.

df.plot.box(invert=True)

The existing interface DataFrame.boxplot to plot boxplot still can be used.

df = pd.DataFrame(np.random.rand(10, 5))
df.boxplot(backend='matplotlib')
<Axes: >
../../../_images/f39e87ac85974cf49fae528281d198ca649703bfd353e254eedce4fa24c6cc31.png
df.boxplot()

In Pandas, you can create a stratified boxplot using the by keyword argument to create groupings.

df = pd.DataFrame(np.random.rand(10,2), columns=['Col1', 'Col2'])
df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
df.boxplot(by='X', backend='matplotlib');
../../../_images/62c259333d83420ff015532096b4946d350ab830de014408fb17f767708c3228.png

Note

In hvPlot this plot can be generated differently by transforming first the data to long format with Pandas and then using by and col to facet the plot.

df_melted = df.melt(id_vars=['X'], value_vars=['Col1', 'Col2'], var_name='Col')
df_melted.boxplot(col='Col', by='X')

You can also pass a subset of columns to plot, as well as group by multiple columns:

df = pd.DataFrame(np.random.rand(10,3), columns=['Col1', 'Col2', 'Col3'])

df['X'] = pd.Series(['A','A','A','A','A','B','B','B','B','B'])
df['Y'] = pd.Series(['A','B','A','B','A','B','A','B','A','B'])

bp = df.boxplot(column=["Col1", "Col2"], by=["X", "Y"], backend='matplotlib')
../../../_images/38a52486b5def7d49371db58daa81b269f008fc8b59b8644ef6605b2f81bd1a7.png

Note

In hvPlot this plot can be generated differently by transforming first the data to long format with Pandas and then using by and col to facet the plot.

df_melted = df.melt(id_vars=['X', 'Y'], value_vars=['Col1', 'Col2'], var_name='Col')
df_melted.boxplot(col='Col', by=['X', 'Y'])
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
df_box.boxplot(row='g')

For more control over the ordering of the levels, we can perform a groupby on the data before plotting.

df_box.groupby('g').boxplot()

Area Plot#

You can create area plots with Series.plot.area() and DataFrame.plot.area(). Area plots are stacked by default. To produce stacked area plot, each column must be either all positive or all negative values.

When input data contains NaN, it will be automatically filled by 0. If you want to drop or fill by different values, use dataframe.dropna() or dataframe.fillna() before calling plot.

df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])
df.plot.area(backend='matplotlib');
../../../_images/707a8e189e92bc4585c30feb3be600b381ca7c40dd30a4650871356cb41a5560.png
df.plot.area()

To produce an unstacked plot, pass stacked=False. In Pandas, alpha value is set to 0.5 unless otherwise specified:

df.plot.area(stacked=False, backend='matplotlib');
../../../_images/b7b1d6860bee35afb7cc467e3fbcd16ea2a58e829ccedd871a48705da0d6da6a.png
df.plot.area(stacked=False, alpha=0.5)

Scatter Plot#

Scatter plots can be drawn by using the DataFrame.plot.scatter() method. Scatter plots require that x and y be specified using the x and y keywords.

df = pd.DataFrame(np.random.rand(50, 4), columns=['a', 'b', 'c', 'd'])
df["species"] = pd.Categorical(

    ["setosa"] * 20 + ["versicolor"] * 20 + ["virginica"] * 10

)
df.plot.scatter(x='a', y='b', backend='matplotlib');
../../../_images/419c6622f3134640dbf049d573d2360c6e4667abea49397fc906c978f8157438.png
df.plot.scatter(x='a', y='b')

In Pandas, 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.

Note

In hvPlot, to plot multiple column groups in a single axes, repeat plot method and then use the overlay operator (*) to combine the plots.

ax = df.plot.scatter(x="a", y="b", color="DarkBlue", label="Group 1", backend='matplotlib')
df.plot.scatter(x="c", y="d", color="DarkGreen", label="Group 2", ax=ax, backend='matplotlib');
../../../_images/8aa783a974f91e954f124cee6dab4f12684435742032874e3f36f3f9fba0f99a.png
plot_1 = df.plot.scatter(x='a', y='b', color='DarkBlue', label='Group 1')
plot_2 = df.plot.scatter(x='c', y='d', color='DarkGreen', label='Group 2')
plot_1 * plot_2

The keyword c may be given as the name of a column to provide colors for each point:

df.plot.scatter(x='a', y='b', c='c', s=50, backend='matplotlib');
../../../_images/2b397b2220ebef3d249ae7e805e8d58b9795629fa25842fcdf1fc6495af0c5c3.png
df.plot.scatter(x='a', y='b', c='c', s=50)

Note

The default colors for continuous labelling in hvPlot is the kbc_r cmap. You can change the colormap to the pandas default by using the cmap keyword.

df.plot.scatter(x='a', y='b', c='c', s=50, cmap='viridis')

If a categorical column is passed to c, then, in Pandas, a discrete colorbar will be produced:

df.plot.scatter(x="a", y="b", c="species", s=50, cmap="viridis", backend='matplotlib');
../../../_images/86b439c572e744ef075b0f080a7c3955857b0e3ffa454fbff5ba479f19032d5a.png

Note

In hvPlot, setting c does not display a colorbar but a legend. The default categorical colormap used is glasbey_category10.

df.plot.scatter(x="a", y="b", c="species", s=50)

You can pass other keywords supported by matplotlib scatter. The example below shows a bubble chart using a column of the DataFrame as the bubble size.

df.plot.scatter(x="a", y="b", s=df["c"] * 200, backend='matplotlib');
../../../_images/0783f9510f91bb486c9e0633e1212aafe6e46169ebbd4759174dc4abd8971273.png

Note

In hvPlot you may need to increase s to obtain markers with a size similar to the ones generated with Pandas and Matplotlib.

df.plot.scatter(x='a', y='b', s=df['c'] * 500)

Note

In hvPlot, the same effect can be accomplished using the scale option.

df.plot.scatter(x='a', y='b', s='c', scale=25)

Hexagonal Bin Plot#

You can create hexagonal bin plots with DataFrame.plot.hexbin(). Hexbin plots can be a useful alternative to scatter plots if your data are too dense to plot each point individually.

df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
df.plot.hexbin(x="a", y="b", gridsize=25, backend='matplotlib');
../../../_images/5610c27b424dfca67d951186269ab75ae7624d5628f0c7d4a2d0f579e5ea1105.png

Note

We pass aspect=1 to hvPlot to generate a plot with dimensions similar to the one generated with Pandas and Matplotlib

df.plot.hexbin(x='a', y='b', gridsize=25, aspect=1)

A useful keyword argument is gridsize; it controls the number of hexagons in the x-direction, and defaults to 100. A larger gridsize means more, smaller bins.

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.

df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
df['b'] = df['b'] + np.arange(1000)
df['z'] = np.random.uniform(0, 3, 1000)
df.plot.hexbin(x='a', y='b', C='z', reduce_C_function=np.max, gridsize=25, backend='matplotlib');
../../../_images/ccddce3668248794a5b6149b257c76b4770453d8a86c6aa6960be0b55f615303.png

Note

In hvPlot, reduce_function is used instead of reduce_C_function.

df.plot.hexbin(x='a', y='b', C='z', reduce_function=np.max, gridsize=25, aspect=1)

Pie plot#

Note

Not yet implemented in hvPlot/HoloViews.

Density Plot#

You can create density plots using the Series.plot.kde() and DataFrame.plot.kde() methods.

ser = pd.Series(np.random.randn(1000))
ser.plot.kde(backend='matplotlib');
../../../_images/7c1561dc5d71c430443ebd703753325bec27d95c4666e7d8f16534d22c6b788a.png
ser.plot.kde()

Plotting with missing data#

hvPlot tries to be pragmatic about plotting DataFrames or Series that contain missing data. Missing values are dropped, left out, or filled depending on the plot type.

(To be confirmed)

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

Fill 0’s

Pie

Not supported

Plotting Tools#

These functions can be imported from hvplot.plotting and take a Series or DataFrame as an argument.

Scatter Matrix Plot#

You can create a scatter plot matrix using the scatter_matrix function:

from pandas.plotting import scatter_matrix as pd_scatter_matrix

df = pd.DataFrame(np.random.randn(1000, 4), columns=['a', 'b', 'c', 'd'])
pd_scatter_matrix(df, alpha=0.2, figsize=(6, 6), diagonal='kde');
../../../_images/022c5e5ddd00cf0f0eaa60f2a5a068ecd9c92b7b76f5c8b23c3c35cb32cea08a.png
from hvplot import scatter_matrix

scatter_matrix(df, alpha=0.2, diagonal='kde')

Note

In hvPlot, since scatter matrix plots may generate a large number of points (the one above renders 120,000 points!), you may want to take advantage of the power provided by Datashader to rasterize the off-diagonal plots into a fixed-resolution representation.

df = pd.DataFrame(np.random.randn(10000, 4), columns=['a', 'b', 'c', 'd'])

scatter_matrix(df, rasterize=True, dynspread=True)

Andrews Curves#

Andrews curves allow one to plot multivariate data as a large number of curves that are created using the attributes of samples as coefficients for Fourier series, see the Wikipedia entry for more information. By coloring these curves differently for each class it is possible to visualize data clustering. Curves belonging to samples of the same class will usually be closer together and form larger structures.

import hvsampledata

dfp = hvsampledata.penguins("pandas")
df_data = dfp[['bill_length_mm', 'bill_depth_mm', 'flipper_length_mm', 'body_mass_g']].sample(frac=0.3)
df_data = (df_data - df_data.min()) / (df_data.max() - df_data.min())
df_data['species'] = dfp['species']
from pandas.plotting import andrews_curves

andrews_curves(df_data, 'species');
../../../_images/dfe66e4e6e658e791b8172ab78b0870f2c845cd6c867d161f8f3a8a95d25886e.png
from hvplot.plotting import andrews_curves

andrews_curves(df_data, 'species')

Parallel Coordinates#

Parallel coordinates is a plotting technique for plotting multivariate data, see the Wikipedia entry for an introduction. Parallel coordinates allows one to see clusters in data and to estimate other statistics visually. Using parallel coordinates points are represented as connected line segments. Each vertical line represents one attribute. One set of connected line segments represents one data point. Points that tend to cluster will appear closer together.

from pandas.plotting import parallel_coordinates

parallel_coordinates(df_data, 'species');
../../../_images/4dfd360675ee8777b100f9a5e85ce80934cd3b4d7e50b6640cfc39de9c177024.png
from hvplot.plotting import parallel_coordinates

parallel_coordinates(df_data, 'species')

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:].

from pandas.plotting import lag_plot

spacing = np.linspace(-99 * np.pi, 99 * np.pi, num=1000)
data = pd.Series(0.1 * np.random.rand(1000) + 0.9 * np.sin(spacing))

lag_plot(data);
../../../_images/2792f3c70f2ef95560fb9a8ce91dd374deab512a2203220afe8f7911bb4ffc6b.png
from hvplot.plotting import lag_plot

lag_plot(data)

Autocorrelation plot#

Note

Not yet implemented in hvPlot.

Bootstrap plot#

Note

Not yet implemented in hvPlot.

RadViz#

Note

Not yet implemented in hvPlot.

Plot Formatting#

Setting the plot style#

TBD

General plot style arguments#

Most plotting methods have a set of keyword arguments that control the layout and formatting of the returned plot:

ts.plot(style='k--', label='Series', backend='matplotlib');
../../../_images/f9575e1d97d6ba573027cc50a85ed9d73810a81a1cb1aaef83fc2f9aba17c5e0.png

Note

The style keyword to customize line plots isn’t supported in hvPlot. Instead, hvPlot offers some generic styling options like c (alias for color), and backend-specific styling options like line_dash.

ts.plot(c='k', line_dash='dashed', label='Series')

For each kind of plot (e.g. line, bar, scatter) any additional arguments keywords are passed along to the corresponding HoloViews object (hv.Curve, hv.Bar, hv.Scatter). These can be used to control additional styling, beyond what hvPlot provides.

Controlling the legend#

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

df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list("ABCD"))
df = df.cumsum()

df.plot(legend=False, backend='matplotlib');
../../../_images/753af0c966149c0e60a44e113957c3cb591d42e2b837557a871b5c5434b2df62.png
df.plot(legend=False)

Note

In hvPlot, you can also control the placement of the legend using the same legend argument set to one of: 'top_right', 'top_left', 'bottom_left', 'bottom_right', 'right', 'left', 'top', or 'bottom'.

df.plot(legend='top_left')

Controlling the labels#

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.

df.plot(xlabel="new x", ylabel="new y", backend='matplotlib');
../../../_images/3237515cb83d613973bb50cff682f45a24a4f02d79c503ac4f6b589ad7ab86d3.png
df.plot(xlabel="new x", ylabel="new y")

Scales#

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

ts = pd.Series(np.random.randn(1000), index=pd.date_range("1/1/2000", periods=1000))
ts = np.exp(ts.cumsum())
ts.plot(logy=True, backend='matplotlib');
../../../_images/959f2c4cab452171c68d2e1de5003043ca186faafa3466820999bbddd6d8cd4d.png
ts.plot(logy=True)

See also the logx and loglog keyword arguments.

Note

In hvPlot, logz=True allows to get a log-scale on the color axis.

dfh = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])
dfh['b'] = dfh['b'] + np.arange(1000)
dfh.plot.hexbin(x='a', y='b', gridsize=25, width=500, height=400, logz=True)

Plotting on a Secondary Y-axis#

In Pandas, to plot data on a secondary y-axis, use the secondary_y keyword:

df["A"].plot(backend='matplotlib')
df["B"].plot(secondary_y=True, style="g", backend='matplotlib');
../../../_images/ff5976da38c53055de0d6bb841daa4001a1224dd3f21f93d929dfcfb9a815d08.png

Note

hvPlot does not currently have built-in support for multiple y-axes, but you can achieve this effect by overlaying multiple plots and setting the multi_y option in .opts(). This allows each y-variable to be displayed on a separate axis.

(df.plot(y='A') * df.plot(y='B') * df.plot(y='C')).opts(multi_y=True)

Suppressing tick resolution adjustment#

pandas includes automatic 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 labeling is performed:

df['A'].plot(backend='matplotlib');
../../../_images/417dba0ceef9b0ede76bbab777065b133fd8f31386cc3a3d7d501d0ea655b26c.png
df['A'].plot()

Using the x_compat parameter, you can suppress this behavior:

df["A"].plot(x_compat=True, backend='matplotlib');
../../../_images/38b9d2ab22a6dfb9c92770bd54c5e6af7a36bd4f4337ed38c0f52cbf49ed07b1.png

Note

In hvPlot, formatters can be set using format strings, by declaring bokeh TickFormatters, or using custom functions. See HoloViews Tick Docs for more information.

from bokeh.models.formatters import DatetimeTickFormatter

formatter = DatetimeTickFormatter(months='%b %Y')
df['A'].plot(yformatter='$%.2f', xformatter=formatter)

Subplots#

Each Series in a DataFrame can be plotted on a different axis with the subplots keyword.

df.plot(subplots=True, figsize=(6, 6), backend='matplotlib');
../../../_images/e981decdb58e10ec2f00844d37a63e760c9ff473b6310bea8aa34bc4fd9d6c59.png

Note

In hvPlot, you can control the maximum number N of plots displayed on a row by calling .cols(N) on the HoloViews layout returned by the .plot() call.

df.plot(subplots=True, height=120).cols(1)

Controlling layout and targeting multiple axes#

In Pandas, 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.

df.plot(subplots=True, layout=(2, 3), figsize=(6, 6), sharex=False, backend='matplotlib');
../../../_images/8ae01f584b007a78b5e1ae81c46ec2512dc8ae1ae042be8d7c31f6bc14b08805.png

Note

In hvPlot, layout is not supported, use .cols(N) instead.

df.plot(subplots=True, sharex=False, width=160).cols(3)

Note

In hvPlot, setting sharex and/or sharey to True is equivalent to setting shared_axes=True.

Plotting with error bars#

In Pandas, plotting with error bars is supported in DataFrame.plot() and Series.plot(). Horizontal and vertical error bars can be supplied to the xerr and yerr keyword arguments to plot().

df3 = pd.DataFrame({'data1': [3, 2, 4, 3, 2, 4, 3, 2],
                    'data2': [6, 5, 7, 5, 4, 5, 6, 5]})
mean_std = pd.DataFrame({'mean': df3.mean(), 'std': df3.std()})
mean_std.plot.bar(y='mean', yerr='std', capsize=4, rot=0, backend='matplotlib');
../../../_images/21f7f90904a48af79481889e8fca2848379478b9f937b3a5d81cc01db9ac9e78.png

Note

In hvPlot, plotting with error bars is not directly supported in DataFrame.plot() and Series.plot(). Instead, users should fall back to using the hvplot namespace directly and the errorbars() plotting method it offers.

import hvplot.pandas  # noqa

(
    mean_std.plot.bar(y='mean', alpha=0.7)
    * mean_std.hvplot.errorbars(x='index', y='mean', yerr1='std')
)

Plotting tables#

In Pandas, plotting with matplotlib table is now supported in DataFrame.plot() and Series.plot() with a table keyword. The table keyword can accept bool, DataFrame or Series. The simple way to draw a table is to specify table=True. Data will be transposed to meet matplotlib’s default layout.

import matplotlib.pyplot as plt

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.
df.plot(table=True, ax=ax, backend='matplotlib');
../../../_images/37fde312b031357d64c9ac3c9e8c8ce73422506f3fae0a527b47feccede94f79.png

Note

In hvPlot, this same effect can be accomplished by using hvplot.table() and creating a layout of the resulting plots with the + operator, arranged with .cols(1).

import hvplot.pandas  # noqa

(
    df.plot(xaxis=False, legend='top_right')
    + df.T.hvplot.table()
).cols(1)

See also

See Panel’s Tabulator widget for a more powerful table component.

Colormaps#

A potential issue when plotting a large number of columns is that it can be difficult to distinguish some series due to repetition in the default colors. To remedy this, DataFrame plotting supports the use of the colormap argument, which accepts either a colormap or a string that is a name of a colormap.

To use the cubehelix colormap, we can pass colormap='cubehelix'.

df = pd.DataFrame(np.random.randn(1000, 10), index=ts.index)
df = df.cumsum()
df.plot(colormap='cubehelix', backend='matplotlib');
../../../_images/8c20f01ac01a05002ef154485817223c1b791475428f349b3bdbf42f7ffc1479.png

Note

colormap is not yet supported in hvPlot for line plots. However, in this case color can be used instead when set to a HoloViews Palette object, that can sample a given colormap at a regularly spaced interval (in this case, the number of overlaid lines).

import matplotlib
import holoviews as hv

# cubehelix palette only added when the matplotlib extension is loaded
hv.Palette.colormaps['cubehelix'] = matplotlib.colormaps['cubehelix']

df.plot(color=hv.Palette("cubehelix"))

Colormaps can also be used with other plot types, like bar charts:

dd = pd.DataFrame(np.random.randn(10, 10)).map(abs)
dd = dd.cumsum()
dd.plot.bar(colormap='Greens', backend='matplotlib');
../../../_images/8b7259c6383982f76d559e5c9f3798121394fc72441912d92e4dc699751a3b4b.png
dd.plot.bar(colormap='Greens')

Parallel coordinates charts:

import hvsampledata
from pandas.plotting import parallel_coordinates

dfp = hvsampledata.penguins('pandas')[['species', 'bill_length_mm', 'bill_depth_mm', 'flipper_length_mm']].sample(n=50)

parallel_coordinates(dfp, 'species', colormap='gist_rainbow');
../../../_images/8dd35dfaca684d5181a75ecb2b2c5d43ac813e1e39ab0ba116e6372da5e50488.png
from hvplot import parallel_coordinates

parallel_coordinates(dfp, 'species', colormap='gist_rainbow')

Andrews curves charts:

from pandas.plotting import andrews_curves

andrews_curves(dfp, 'species', colormap='winter')
<Axes: >
../../../_images/4f0eb8ff81d7ad031370b6a0a5f865e9f44b2a2f8c058d36a6c2f0b7a263e6bb.png
from hvplot.plotting import andrews_curves

andrews_curves(dfp, 'species', colormap='winter')

Plotting directly with HoloViews#

In some situations it may still be preferable or necessary to prepare plots directly with hvPlot or HoloViews, for instance when a certain type of plot or customization is not (yet) supported by pandas. Series and DataFrame objects behave like arrays and can therefore be passed directly to HoloViews functions without explicit casts.

import holoviews as hv

price = pd.Series(np.random.randn(150).cumsum(),
                  index=pd.date_range('2000-1-1', periods=150, freq='B'), name='price')
ma = price.rolling(20).mean()
mstd = price.rolling(20).std()

price.plot(c='k') * ma.plot(c='b', label='mean') * \
hv.Area((mstd.index, ma - 2 * mstd, ma + 2 * mstd),
        vdims=['y', 'y2']).opts(color='b', alpha=0.2)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website.