From 2d86aac9a99b548e31fd2c74aaebd31a77f65842 Mon Sep 17 00:00:00 2001 From: Isaiah Akorita Date: Thu, 30 Jan 2025 13:40:11 +0100 Subject: [PATCH 1/3] remove test cell from output --- doc/user_guide/Viewing.ipynb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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", From 3c69ddbf15b75df9a8d70c877266ae6f53b0d45d Mon Sep 17 00:00:00 2001 From: Isaiah Akorita Date: Thu, 30 Jan 2025 13:45:52 +0100 Subject: [PATCH 2/3] update notebook to match current pandas API --- doc/user_guide/Pandas_API.ipynb | 203 ++++++++++++++++++-------------- 1 file changed, 113 insertions(+), 90 deletions(-) diff --git a/doc/user_guide/Pandas_API.ipynb b/doc/user_guide/Pandas_API.ipynb index 31af30e17..512155cb2 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 is meant to recreate the [pandas visualization docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html).\n", + "\n", + "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`. " ] }, { @@ -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:" ] @@ -104,7 +119,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 +136,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 +150,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 +182,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.iloc[5].plot.bar() * hv.HLine(0).opts(color='k')" + "df.iloc[5].plot.bar()" ] }, { @@ -254,16 +267,6 @@ "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)" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -300,7 +303,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 +319,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 +328,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 +340,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 +361,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 +379,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 +405,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 +476,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." ] @@ -491,7 +495,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To produce an stacked plot, pass `stacked=True`. " + "To produce an unstacked plot, pass `stacked=False`. " ] }, { @@ -500,7 +504,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.plot.area(stacked=True)" + "df.plot.area(stacked=False, alpha=0.5)" ] }, { @@ -514,7 +518,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 +566,25 @@ "df.plot.scatter(x='a', y='b', c='c', s=50)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If a categorical column is passed to `c`, then discrete color labels will be produced." + ] + }, + { + "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": {}, @@ -636,7 +659,7 @@ "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)" @@ -666,9 +689,22 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "### Pie plot\n", + "## Plotting with missing data\n", + "\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", - "**NOT SUPPORTED**\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 +720,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 +791,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 +825,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 +865,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 +896,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 +921,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 +957,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 +987,7 @@ "metadata": {}, "outputs": [], "source": [ - "df.A.plot(yformatter='$%.2f', xformatter=formatter)" + "df['A'].plot(yformatter='$%.2f', xformatter=formatter)" ] }, { @@ -973,7 +996,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 +1005,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 +1026,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 +1126,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 +1190,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)" ] } From 55f11bb43eefa9accec3048d203488036aa5daef Mon Sep 17 00:00:00 2001 From: Isaiah Akorita Date: Mon, 3 Feb 2025 18:03:42 +0100 Subject: [PATCH 3/3] added explanations for some color differences --- doc/user_guide/Pandas_API.ipynb | 50 ++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/doc/user_guide/Pandas_API.ipynb b/doc/user_guide/Pandas_API.ipynb index 512155cb2..351d9f68d 100644 --- a/doc/user_guide/Pandas_API.ipynb +++ b/doc/user_guide/Pandas_API.ipynb @@ -11,9 +11,9 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This notebook is meant to recreate the [pandas visualization docs](https://pandas.pydata.org/pandas-docs/stable/user_guide/visualization.html).\n", + "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", - "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`. " + "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`." ] }, { @@ -86,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": {}, @@ -264,7 +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." + "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", + ":::" ] }, { @@ -488,7 +508,7 @@ "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()" ] }, { @@ -570,7 +590,23 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "If a categorical column is passed to `c`, then discrete color labels will be produced." + "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." ] }, { @@ -640,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')" ] }, { @@ -662,7 +698,7 @@ "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')" ] }, {