Skip to content

Commit

Permalink
Updates to matplotlib element references to use opts (#3270)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr authored and jlstevens committed Dec 11, 2018
1 parent b226242 commit d21c1d7
Show file tree
Hide file tree
Showing 37 changed files with 228 additions and 142 deletions.
7 changes: 5 additions & 2 deletions examples/reference/elements/matplotlib/Arrow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Curve (color='#D3D3D3')\n",
"xs = np.linspace(-5,5,100)\n",
"ys = -(xs-2)**3\n",
"hv.Curve((xs,ys)) * hv.Arrow(0, 5, 'Inflection', 'v')"
"\n",
"curve = hv.Curve((xs,ys))\n",
"arrow = hv.Arrow(0, 5, 'Inflection', 'v')\n",
"\n",
"curve.opts(color='#D3D3D3') * arrow"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions examples/reference/elements/matplotlib/Bars.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Bars.Grouped [category_index=0 group_index=1 stack_index=5] \n",
"%%opts Bars.Stacked [color_by=['stack'] stack_index=1 category_index=5]\n",
"from itertools import product\n",
"np.random.seed(3)\n",
"index, groups = ['A', 'B'], ['a', 'b']\n",
"keys = product(index, groups)\n",
"bars = hv.Bars([k+(np.random.rand()*100.,) for k in keys],\n",
" ['Index', 'Group'], 'Count')\n",
"bars.relabel(group='Grouped') + bars.relabel(group='Stacked')"
"\n",
"grouped = bars.relabel('Grouped').opts(category_index=5, color_by=['stack'], stack_index=1)\n",
"stacked = bars.relabel('Stacked').opts(stack_index=5)\n",
"\n",
"grouped + stacked"
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions examples/reference/elements/matplotlib/Bivariate.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand Down Expand Up @@ -58,8 +60,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Bivariate [filled=True colorbar=True] (cmap='Blues')\n",
"hv.Bivariate(normal)"
"hv.Bivariate(normal).opts(colorbar=True, cmap='Blues', filled=True)"
]
},
{
Expand All @@ -75,8 +76,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Bivariate {+axiswise}\n",
"hv.NdLayout({bw: hv.Bivariate(normal).options(bandwidth=bw)\n",
"hv.NdLayout({bw: hv.Bivariate(normal).options(axiswise=True, bandwidth=bw)\n",
" for bw in [0.05, 0.1, 0.5, 1]}, 'Bandwidth')"
]
},
Expand Down
4 changes: 2 additions & 2 deletions examples/reference/elements/matplotlib/Bounds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Bounds (color='orange' linewidth=6)\n",
"penguins = hv.RGB.load_image('../assets/penguins.png')\n",
"penguins * hv.Bounds((-0.15, -0.4, 0.2, 0))"
"bounds = hv.Bounds((-0.15, -0.4, 0.2, 0))\n",
"penguins * bounds.opts(color='orange', linewidth=6)"
]
},
{
Expand Down
14 changes: 10 additions & 4 deletions examples/reference/elements/matplotlib/Box.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand All @@ -37,11 +39,12 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Box (linewidth=5 color='red') Image (cmap='gray')\n",
"data = np.sin(np.mgrid[0:100,0:100][1]/10.0)\n",
"data[np.arange(40, 60), np.arange(20, 40)] = -1\n",
"data[np.arange(40, 50), np.arange(70, 80)] = -3 \n",
"hv.Image(data) * hv.Box(-0.2, 0, 0.25 ) * hv.Box(-0, 0, (0.4,0.9) )"
"(hv.Image(data) * hv.Box(-0.2, 0, 0.25 ) * hv.Box(-0, 0, (0.4,0.9))).opts(\n",
" opts.Box(color='red', linewidth=5),\n",
" opts.Image(cmap='gray'))"
]
},
{
Expand All @@ -59,10 +62,13 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Box (linewidth=5 color='purple') Image (cmap='gray')\n",
"data = np.sin(np.mgrid[0:100,0:100][1]/10.0)\n",
"data[np.arange(30, 70), np.arange(30, 70)] = -3\n",
"hv.Image(data) * hv.Box(-0, 0, 0.25, aspect=3, orientation=-np.pi/4)"
"box = hv.Box(-0, 0, 0.25, aspect=3, orientation=-np.pi/4)\n",
"\n",
"(hv.Image(data) * box).opts(\n",
" opts.Box(color='purple', linewidth=5),\n",
" opts.Image(cmap='gray'))"
]
},
{
Expand Down
9 changes: 6 additions & 3 deletions examples/reference/elements/matplotlib/BoxWhisker.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand Down Expand Up @@ -62,10 +64,11 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts BoxWhisker [aspect=2 fig_size=200 show_legend=False] (whiskerprops={'color': 'gray'})\n",
"groups = [chr(65+g) for g in np.random.randint(0, 3, 200)]\n",
"hv.BoxWhisker((groups, np.random.randint(0, 5, 200), np.random.randn(200)),\n",
" ['Group', 'Category'], 'Value').sort()"
"box = hv.BoxWhisker((groups, np.random.randint(0, 5, 200), np.random.randn(200)),\n",
" ['Group', 'Category'], 'Value').sort()\n",
"\n",
"box.opts(opts.BoxWhisker(aspect=2, fig_size=200, whiskerprops={'color': 'gray'}))"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions examples/reference/elements/matplotlib/Chord.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
"source": [
"import pandas as pd\n",
"import holoviews as hv\n",
"\n",
"from holoviews import dim, opts\n",
"from bokeh.sampledata.les_mis import data\n",
"\n",
"hv.extension('matplotlib')\n",
"%output size=200 fig='svg'"
"hv.output(fig='svg', size=200)"
]
},
{
Expand Down Expand Up @@ -96,8 +98,8 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Chord [labels='name'] (node_color=dim('index').astype(str) edge_color=dim('source').astype(str) cmap='Category20' edge_cmap='Category20')\n",
"hv.Chord((links, nodes)).select(value=(5, None))"
"hv.Chord((links, nodes)).select(value=(5, None)).opts(\n",
" opts.Chord(cmap='Category20', edge_color=dim('source').astype(str), labels='name', node_color=dim('index').astype(str)))"
]
}
],
Expand Down
5 changes: 2 additions & 3 deletions examples/reference/elements/matplotlib/Contours.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,11 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Contours [colorbar=True] (cmap='fire')\n",
"x,y = np.mgrid[-50:51, -50:51] * 0.05\n",
"img = hv.Image(np.sin(x**2+y**3))\n",
"contours = hv.operation.contours(img, levels=5)\n",
"\n",
"z0, z1 = img.range('z')\n",
"img + hv.operation.contours(img, levels=5)"
"img + contours.opts(colorbar=True, cmap='fire')"
]
},
{
Expand Down
8 changes: 5 additions & 3 deletions examples/reference/elements/matplotlib/Curve.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts NdOverlay [legend_position='right']\n",
"hv.NdOverlay({interp: hv.Curve(points[::8]).options(interpolation=interp)\n",
" for interp in ['linear', 'steps-mid', 'steps-pre', 'steps-post']})"
"overlay = hv.NdOverlay({\n",
" interp: hv.Curve(points[::8]).options(interpolation=interp)\n",
" for interp in ['linear', 'steps-mid', 'steps-pre', 'steps-post']})\n",
"\n",
"overlay.opts(legend_position='right')"
]
},
{
Expand Down
3 changes: 1 addition & 2 deletions examples/reference/elements/matplotlib/Distribution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Distribution [filled=False] (alpha=1)\n",
"hv.NdOverlay({bw: hv.Distribution(normal).options(bandwidth=bw)\n",
"hv.NdOverlay({bw: hv.Distribution(normal).opts(alpha=1, bandwidth=bw, filled=False)\n",
" for bw in [0.05, 0.1, 0.5, 1]}, 'Bandwidth')"
]
},
Expand Down
6 changes: 4 additions & 2 deletions examples/reference/elements/matplotlib/Ellipse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand All @@ -37,7 +39,8 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Ellipse (linewidth=6)\n",
"opts.defaults(opts.Ellipse(linewidth=6))\n",
"\n",
"# Generate some data\n",
"c1 = np.random.normal(loc=2, scale=0.2, size=(200,200))\n",
"c2x = np.random.normal(loc=-2, scale=0.6, size=200)\n",
Expand All @@ -61,7 +64,6 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts Ellipse (linewidth=6)\n",
"clusters = hv.Points(c1) * hv.Points((c2x, c2y)) * hv.Points(c3)\n",
"clusters * hv.Ellipse(0,0, 4, orientation=np.pi/5, aspect=2) "
]
Expand Down
13 changes: 7 additions & 6 deletions examples/reference/elements/matplotlib/Graph.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
Expand Down Expand Up @@ -60,8 +61,8 @@
"\n",
"padding = dict(x=(-1.2, 1.2), y=(-1.2, 1.2))\n",
"\n",
"simple_graph = hv.Graph(((source, target),)).options(padding=0.1)\n",
"simple_graph"
"simple_graph = hv.Graph(((source, target),))\n",
"simple_graph.opts(padding=0.1)"
]
},
{
Expand All @@ -79,7 +80,7 @@
"metadata": {},
"outputs": [],
"source": [
"simple_graph.options(directed=True, arrowhead_length=0.08)"
"simple_graph.opts(directed=True, arrowhead_length=0.08)"
]
},
{
Expand Down Expand Up @@ -107,7 +108,7 @@
"#### Additional features\n",
"\n",
"\n",
"Next we will extend this example by supplying explicit edges, node information and edge weights. By constructing the ``Nodes`` explicitly we can declare an additional value dimensions, which are revealed when hovering and/or can be mapped to the color by specifying the ``color_index``. We can also associate additional information with each edge by supplying a value dimension to the ``Graph`` itself, which we can map to a color using the ``edge_color_index``."
"Next we will extend this example by supplying explicit edges, node information and edge weights. By constructing the ``Nodes`` explicitly we can declare an additional value dimensions, which are revealed when hovering and/or can be mapped to the color by specifying the ``color``. We can also associate additional information with each edge by supplying a value dimension to the ``Graph`` itself, which we can map to a color using the ``edge_color``."
]
},
{
Expand Down Expand Up @@ -135,8 +136,8 @@
"nodes = hv.Nodes((x, y, node_indices, node_labels), vdims='Type')\n",
"graph = hv.Graph(((source, target, edge_weights), nodes, paths), vdims='Weight')\n",
"\n",
"graph.redim.range(**padding).options(color_index='Type', edge_color_index='Weight',\n",
" cmap=['blue', 'red'], edge_cmap='viridis')"
"graph.opts(\n",
" opts.Graph(cmap=['blue', 'red'], edge_cmap='viridis', edge_color='Weight', node_color='Type', padding=0.1))"
]
},
{
Expand Down
9 changes: 7 additions & 2 deletions examples/reference/elements/matplotlib/HLine.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand All @@ -37,10 +39,13 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts HLine (color='blue' linewidth=6) Points (color='#D3D3D3')\n",
"xs = np.random.normal(size=100)\n",
"ys = np.random.normal(size=100) * xs\n",
"hv.Points((xs,ys)) * hv.HLine(ys.mean())"
"points = hv.Points((xs,ys))\n",
"\n",
"(points * hv.HLine(ys.mean())).opts(\n",
" opts.HLine(color='#D3D3D3')\n",
" opts.Points(color='blue', linewidth=6))"
]
},
{
Expand Down
5 changes: 4 additions & 1 deletion examples/reference/elements/matplotlib/HSV.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')"
]
},
Expand Down Expand Up @@ -67,7 +69,8 @@
"metadata": {},
"outputs": [],
"source": [
"%opts Image (cmap='gray')\n",
"opts.defaults(opts.Image(cmap='gray'))\n",
"\n",
"hsv[..., 'H'].relabel('H') + hsv[..., 'S'].relabel('S') + hsv[..., 'V'].relabel('V')"
]
},
Expand Down
7 changes: 4 additions & 3 deletions examples/reference/elements/matplotlib/HeatMap.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@
"metadata": {},
"outputs": [],
"source": [
"%%opts HeatMap [ colorbar=True fig_size=250]\n",
"hv.HeatMap((np.random.randint(0, 10, 100), np.random.randint(0, 10, 100),\n",
" np.random.randn(100), np.random.randn(100)), vdims=['z', 'z2']).redim.range(z=(-2, 2))"
"heatmap = hv.HeatMap((np.random.randint(0, 10, 100), np.random.randint(0, 10, 100),\n",
" np.random.randn(100), np.random.randn(100)), vdims=['z', 'z2']).redim.range(z=(-2, 2))\n",
"\n",
"heatmap.opts(colorbar=True, fig_size=250)"
]
},
{
Expand Down
19 changes: 13 additions & 6 deletions examples/reference/elements/matplotlib/HexTiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
"source": [
"import numpy as np\n",
"import holoviews as hv\n",
"from holoviews import opts\n",
"\n",
"hv.extension('matplotlib')\n",
"%output fig='svg' size=200"
"hv.output(fig='svg', size=200)"
]
},
{
Expand All @@ -40,7 +42,7 @@
"source": [
"np.random.seed(44)\n",
"hex_tiles = hv.HexTiles(np.random.randn(100000, 2))\n",
"hex_tiles.options(colorbar=True)"
"hex_tiles.opts(colorbar=True)"
]
},
{
Expand All @@ -60,9 +62,11 @@
"source": [
"xs, ys = np.random.randn(2, 1000)\n",
"hex_with_values = hv.HexTiles((xs, ys, xs*(ys/2.), (xs**2)*ys), vdims=['Values', 'Hover Values'])\n",
"points = hv.Points(hex_with_values)\n",
"\n",
"hex_with_values.options(aggregator=np.sum) *\\\n",
"hv.Points(hex_with_values).options(s=3, color='black')"
"(hex_with_values * points).opts(\n",
" opts.HexTiles(aggregator=np.sum),\n",
" opts.Points(s=3, color='black'))"
]
},
{
Expand All @@ -82,8 +86,11 @@
"source": [
"x, y = np.hstack([np.random.randn(2, 10000), np.random.randn(2, 10000)*0.8+2])\n",
"hex_two_distributions = hv.HexTiles((x, y))\n",
"hex_two_distributions.options(min_count=0) *\\\n",
"hv.Bivariate(hex_two_distributions).options(show_legend=False, linewidth=3)"
"bivariate = hv.Bivariate((x, y))\n",
"\n",
"(hex_two_distributions * bivariate).opts(\n",
" opts.Bivariate(linewidth=3, show_legend=False),\n",
" opts.HexTiles(min_count=0))"
]
}
],
Expand Down

0 comments on commit d21c1d7

Please sign in to comment.