diff --git a/doc/user_guide/Pandas_API.ipynb b/doc/user_guide/Pandas_API.ipynb index 31af30e17..351d9f68d 100644 --- a/doc/user_guide/Pandas_API.ipynb +++ b/doc/user_guide/Pandas_API.ipynb @@ -11,7 +11,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If hvplot and pandas are both installed, then we can use the pandas.options.plotting.backend to control the output of `pd.DataFrame.plot` and `pd.Series.plot`. This notebook is meant to recreate the [pandas visualization docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html)." + "This notebook recreates the [pandas visualization examples](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html). It shows how to configure and control the output of `pd.DataFrame.plot` and `pd.Series.plot`.\n", + "\n", + "When hvPlot and pandas are both installed, you can enable hvPlot by setting `pandas.options.plotting.backend` to `\"hvplot\"` or `\"holoviews\"` or by importing `hvplot.pandas`." ] }, { @@ -40,6 +42,19 @@ "The plot method on Series and DataFrame is just a simple wrapper around `hvplot()`:" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "tags": [ + "remove-cell" + ] + }, + "outputs": [], + "source": [ + "np.random.seed(123456)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -55,7 +70,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If the index consists of dates, they will be sorted (as long as `sort_date=True`) and formatted the x-axis nicely as per above.\n", + "If the index consists of dates, they will be sorted (as long as `sort_date=True`) and formatted nicely along the x-axis as per above.\n", "\n", "On DataFrame, `plot()` is a convenience to plot all of the columns with labels:" ] @@ -71,6 +86,22 @@ "df.plot()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The default colors used in hvPlot are different from pandas but you can pass your preferred colors as a list via the `color` keyword." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.plot(color=['#1F77B4', '#FF7F0D', '#B5DDB5', '#D93A3B'])" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -104,7 +135,6 @@ "* `area` for area plots\n", "* `scatter` for scatter plots\n", "* `hexbin` for hexagonal bin plots\n", - "* ~~`pie` for pie plots~~\n", "\n", "For example, a bar plot can be created the following way:" ] @@ -122,7 +152,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can also create these other plots using the methods DataFrame.plot. instead of providing the kind keyword argument. This makes it easier to discover plot methods and the specific arguments they use:" + "You can also create these other plots using the methods `DataFrame.plot.` instead of providing the `kind` keyword argument. This makes it easier to discover plot methods and the specific arguments they use:" ] }, { @@ -136,31 +166,30 @@ "* `Scatter Matrix`\n", "* `Andrews Curves`\n", "* `Parallel Coordinates`\n", - "* `Lag Plot`\n", - "* ~~`Autocorrelation Plot`~~\n", - "* ~~`Bootstrap Plot`~~\n", - "* ~~`RadViz`~~\n", - "\n", - "Plots may also be adorned with errorbars or tables." + "* `Lag Plot`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Bar plots\n", + ":::{note}\n", + "The following plots are not currently supported in hvPlot:\n", + "* `Autocorrelation Plot`\n", + "* `Bootstrap Plot`\n", + "* `RadViz`\n", + ":::\n", "\n", - "For labeled, non-time series data, you may wish to produce a bar plot:\n", - "\n" + "Plots may also be adorned with errorbars or tables." ] }, { - "cell_type": "code", - "execution_count": null, + "cell_type": "markdown", "metadata": {}, - "outputs": [], "source": [ - "import holoviews as hv" + "### Bar plots\n", + "\n", + "For labeled, non-time series data, you may wish to produce a bar plot:" ] }, { @@ -169,7 +198,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.iloc[5].plot.bar() * hv.HLine(0).opts(color='k')" + "df.iloc[5].plot.bar()" ] }, { @@ -251,17 +280,11 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Using the matplotlib backend, histograms can be stacked using `stacked=True`; **stacking is not yet supported in the holoviews backend**. Bin size can be changed by `bins` keyword." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Stacked not supported\n", - "df4.plot.hist(stacked=True, bins=20)" + "Using the matplotlib backend, histograms can be stacked using `stacked=True`; Bin size can be changed by `bins` keyword.\n", + "\n", + ":::{note}\n", + "Stacking is not yet supported in the holoviews backend\n", + ":::" ] }, { @@ -300,7 +323,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "`DataFrame.hist()` plots the histograms of the columns on multiple subplots:" + "You can use the `subplots` keyword to plot `DataFrame.hist()` columns on multiple subplots:" ] }, { @@ -316,7 +339,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "The by keyword can be specified to plot grouped histograms:" + "The `by` keyword can be specified to plot grouped histograms:" ] }, { @@ -325,10 +348,10 @@ "metadata": {}, "outputs": [], "source": [ - "# by does not support arrays, instead the array should be added as a column\n", "data = pd.Series(np.random.randn(1000))\n", - "data = pd.DataFrame({'data': data, 'by_column': np.random.randint(0, 4, 1000)})\n", - "data.hist(by='by_column', width=300, subplots=True).cols(2)" + "df5 = pd.DataFrame({'data': data, 'by_column': np.random.randint(0, 4, 1000)})\n", + "\n", + "df5.hist(by='by_column', width=300, subplots=True).cols(2)" ] }, { @@ -337,7 +360,7 @@ "source": [ "### Box Plots\n", "\n", - "Boxplot can be drawn calling `Series.plot.box()` and `DataFrame.plot.box()`, or `DataFrame.boxplot()` to visualize the distribution of values within each column.\n", + "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.\n", "\n", "For instance, here is a boxplot representing five trials of 10 observations of a uniform random variable on [0,1).\n" ] @@ -358,14 +381,16 @@ "source": [ "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`. \n", "\n", - "**This behavior is not supported in the holoviews backend.**" + ":::{note}\n", + "This behavior is not supported in the holoviews backend\n", + ":::" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "You can pass other keywords supported by holoviews boxplot. For example, horizontal and custom-positioned boxplot can be drawn by `invert=True` and positions keywords." + "You can pass other keywords supported by holoviews boxplot. For example, horizontal boxplots can be drawn by `invert=True`." ] }, { @@ -374,15 +399,14 @@ "metadata": {}, "outputs": [], "source": [ - "# positions not supported\n", - "df.plot.box(invert=True, positions=[1, 4, 5, 6, 8])" + "df.plot.box(invert=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "See the boxplot method and the matplotlib boxplot documentation for more.\n", + "See the boxplot method and the [matplotlib boxplot documentation](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.boxplot.html) for more.\n", "\n", "The existing interface `DataFrame.boxplot` to plot boxplot still can be used." ] @@ -401,7 +425,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "You can create a stratified boxplot using the `groupby` keyword argument to create groupings. For instance," + "You can create a stratified boxplot using the `groupby` keyword argument to create groupings." ] }, { @@ -472,7 +496,7 @@ "source": [ "### Area Plot\n", "\n", - "You can create area plots with `Series.plot.area()` and `DataFrame.plot.area()`. Area plots are *not* stacked by default. To produce stacked area plot, each column must be either all positive or all negative values.\n", + "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.\n", "\n", "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." ] @@ -484,14 +508,14 @@ "outputs": [], "source": [ "df = pd.DataFrame(np.random.rand(10, 4), columns=['a', 'b', 'c', 'd'])\n", - "df.plot.area(alpha=0.5)" + "df.plot.area()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "To produce an stacked plot, pass `stacked=True`. " + "To produce an unstacked plot, pass `stacked=False`. " ] }, { @@ -500,7 +524,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.plot.area(stacked=True)" + "df.plot.area(stacked=False, alpha=0.5)" ] }, { @@ -514,7 +538,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Scatter plot can be drawn by using the `DataFrame.plot.scatter()` method. Scatter plot require that x and y be specified using the `x` and `y` keywords." + "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." ] }, { @@ -562,6 +586,41 @@ "df.plot.scatter(x='a', y='b', c='c', s=50)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df.plot.scatter(x='a', y='b', c='c', s=50, cmap='viridis')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If a categorical column is passed to `c`, then discrete color labels will be produced using [`glasbey_category10`](https://colorcet.holoviz.org/user_guide/Categorical.html#starting-colors) as the default colors for categorical color mapping." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df[\"species\"] = pd.Categorical(\n", + " [\"setosa\"] * 20 + [\"versicolor\"] * 20 + [\"virginica\"] * 10)\n", + "\n", + "df.plot.scatter(x=\"a\", y=\"b\", c=\"species\", s=50)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -617,7 +676,7 @@ "df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])\n", "df['b'] = df['b'] + np.arange(1000)\n", "\n", - "df.plot.hexbin(x='a', y='b', gridsize=25, width=500, height=400)" + "df.plot.hexbin(x='a', y='b', gridsize=25, width=500, height=400, cmap='Greens')" ] }, { @@ -636,10 +695,10 @@ "outputs": [], "source": [ "df = pd.DataFrame(np.random.randn(1000, 2), columns=['a', 'b'])\n", - "df['b'] = df['b'] = df['b'] + np.arange(1000)\n", + "df['b'] = df['b'] + np.arange(1000)\n", "df['z'] = np.random.uniform(0, 3, 1000)\n", "\n", - "df.plot.hexbin(x='a', y='b', C='z', reduce_function=np.max, gridsize=25, width=500, height=400)" + "df.plot.hexbin(x='a', y='b', C='z', reduce_function=np.max, gridsize=25, width=500, height=400, cmap='Greens')" ] }, { @@ -666,9 +725,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Pie plot\n", + "## Plotting with missing data\n", "\n", - "**NOT SUPPORTED**\n" + "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.\n", + "\n", + "| Plot Type | Nan Handling |\n", + "| --- | --- |\n", + "| Line | Leave gaps at NaNs |\n", + "| Line (stacked) | Fill 0's\n", + "| Bar | Fill 0's |\n", + "| Scatter | Drop NaNs\n", + "| Histogram | Drop NaNs (column-wise)\n", + "| Box | Drop NaNs (column-wise)\n", + "| Area |Fill 0's\n", + "| KDE | Drop NaNs (column-wise)\n", + "| Hexbin | Fill 0's\n", + "| Pie | *Not supported*" ] }, { @@ -684,16 +756,6 @@ "You can create a scatter plot matrix using the `scatter_matrix` function:" ] }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "import numpy as np" - ] - }, { "cell_type": "code", "execution_count": null, @@ -765,7 +827,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Parallel coordinates is a plotting technique for plotting multivariate data. It 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." + "Parallel coordinates is a plotting technique for plotting multivariate data. It 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." ] }, { @@ -799,27 +861,7 @@ "data = pd.Series(0.1 * np.random.rand(1000) +\n", " 0.9 * np.sin(np.linspace(-99 * np.pi, 99 * np.pi, num=1000)))\n", "\n", - "lag_plot(data, width=400, height=400)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "### Autocorrelation Plot\n", - "\n", - "**NOT SUPPORTED**\n", - "\n", - "### Bootstrap Plot\n", - "\n", - "**NOT SUPPORTED**\n", - "\n", - "### RadViz\n", - "\n", - "**NOT SUPPORTED**\n", - "\n", - "\n" + "lag_plot(data, width=500, height=400)" ] }, { @@ -859,7 +901,7 @@ "metadata": {}, "outputs": [], "source": [ - "df = pd.DataFrame(np.random.randn(1000, 4), \n", + "df = pd.DataFrame(np.random.randn(1000, 4),\n", " index=ts.index, columns=list('ABCD'))\n", "df = df.cumsum()\n", "\n", @@ -890,7 +932,7 @@ "metadata": {}, "outputs": [], "source": [ - "ts = pd.Series(np.random.randn(1000), \n", + "ts = pd.Series(np.random.randn(1000),\n", " index=pd.date_range('1/1/2000', periods=1000))\n", "\n", "ts = np.exp(ts.cumsum())\n", @@ -915,7 +957,24 @@ "source": [ "### Plotting on a Secondary Y-axis\n", "\n", - "**NOT SUPPORTED**" + "hvPlot does not currently have built-in support for multiple y-axes, but you can achieve this effect by overlaying multiple plots and using the `multi_y` option in `.opts`. This allows each y-variable to be displayed on a separate axis." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "df = pd.DataFrame(np.random.randn(1000, 4), index=ts.index, columns=list('ABCD'))\n", + "df = df.cumsum()\n", + "\n", + "p1 = df.plot(y='A')\n", + "p2 = df.plot(y='B')\n", + "p3 = df.plot(y='C')\n", + "\n", + "multi_y_plot = (p1 * p2 * p3).opts(multi_y=True)\n", + "multi_y_plot" ] }, { @@ -934,10 +993,10 @@ "metadata": {}, "outputs": [], "source": [ - "df = pd.DataFrame(np.random.randn(1000, 4), \n", + "df = pd.DataFrame(np.random.randn(1000, 4),\n", " index=ts.index, columns=list('ABCD'))\n", "df = df.cumsum()\n", - "df.A.plot()" + "df['A'].plot()" ] }, { @@ -964,7 +1023,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.A.plot(yformatter='$%.2f', xformatter=formatter)" + "df['A'].plot(yformatter='$%.2f', xformatter=formatter)" ] }, { @@ -973,7 +1032,7 @@ "source": [ "### Subplots\n", "\n", - "Each `Series` in a `DataFrame` can be plotted on a different axis with the `subplots` keyword:" + "Each `Series` in a `DataFrame` can be plotted on a different axis with the `subplots` keyword." ] }, { @@ -982,7 +1041,7 @@ "metadata": {}, "outputs": [], "source": [ - "df = pd.DataFrame(np.random.randn(1000, 4), \n", + "df = pd.DataFrame(np.random.randn(1000, 4),\n", " index=ts.index, columns=list('ABCD'))\n", "df = df.cumsum()\n", "df.plot(subplots=True, height=150).cols(1)" @@ -1003,7 +1062,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.plot(subplots=True, shared_axes=False, width=150).cols(3)" + "df.plot(subplots=True, shared_axes=False, width=300).cols(2)" ] }, { @@ -1103,7 +1162,7 @@ "metadata": {}, "outputs": [], "source": [ - "dd = pd.DataFrame(np.random.randn(10, 10)).applymap(abs)\n", + "dd = pd.DataFrame(np.random.randn(10, 10)).map(abs)\n", "dd = dd.cumsum()\n", "dd.plot.bar(colormap='Greens')" ] @@ -1167,7 +1226,7 @@ "mstd = price.rolling(20).std()\n", "\n", "price.plot(c='k') * ma.plot(c='b', label='mean') * \\\n", - "hv.Area((mstd.index, ma - 2 * mstd, ma + 2 * mstd), \n", + "hv.Area((mstd.index, ma - 2 * mstd, ma + 2 * mstd),\n", " vdims=['y', 'y2']).opts(color='b', alpha=0.2)" ] } diff --git a/doc/user_guide/Viewing.ipynb b/doc/user_guide/Viewing.ipynb index 5c8b7d28f..085fed95d 100644 --- a/doc/user_guide/Viewing.ipynb +++ b/doc/user_guide/Viewing.ipynb @@ -183,7 +183,11 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "tags": [ + "remove-cell" + ] + }, "outputs": [], "source": [ "from pathlib import Path\n",