-
-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reimplement Path, Contours and Polygons plots #1991
Changes from 38 commits
1321c4b
8e7e682
68d3605
25c1e22
d1c9c01
10b6dc8
8000134
a5e7411
9adf459
8db534a
17ea673
115ac00
ea5d64e
d0e2f2d
a69a622
4d9f5ee
2cc0e42
8cdb15b
c7493af
712d04b
7debba1
343b49a
39f7a82
a237e85
9a4c335
5d88475
c2cd257
93088bb
13fb334
4027f06
b1b95f2
96de1b0
1e75ffa
14a91b1
1711099
1b2602d
0a73811
bf217c5
6e9ec29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,20 +38,11 @@ | |
"from bokeh.sampledata.us_counties import data as counties\n", | ||
"from bokeh.sampledata.unemployment import data as unemployment\n", | ||
"\n", | ||
"counties = {\n", | ||
" code: county for code, county in counties.items() if county[\"state\"] == \"tx\"\n", | ||
"}\n", | ||
"counties = [dict(county, Unemployment=unemployment[cid])\n", | ||
" for cid, county in counties.items()\n", | ||
" if county[\"state\"] == \"tx\"]\n", | ||
"\n", | ||
"county_xs = [county[\"lons\"] for county in counties.values()]\n", | ||
"county_ys = [county[\"lats\"] for county in counties.values()]\n", | ||
"\n", | ||
"county_names = [county['name'] for county in counties.values()]\n", | ||
"county_rates = [unemployment[county_id] for county_id in counties]\n", | ||
"\n", | ||
"county_polys = {name: hv.Polygons((xs, ys), level=rate, vdims=['Unemployment'])\n", | ||
" for name, xs, ys, rate in zip(county_names, county_xs, county_ys, county_rates)}\n", | ||
"\n", | ||
"choropleth = hv.NdOverlay(county_polys, kdims=['County'])" | ||
"choropleth = hv.Polygons(counties, ['lons', 'lats'], [('detailed name', 'County'), 'Unemployment'])" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume the old code still works and the new approach is just more succinct/flexible/efficient. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. |
||
] | ||
}, | ||
{ | ||
|
@@ -68,10 +59,11 @@ | |
"outputs": [], | ||
"source": [ | ||
"plot_opts = dict(logz=True, tools=['hover'], xaxis=None, yaxis=None,\n", | ||
" show_grid=False, show_frame=False, width=500, height=500)\n", | ||
" show_grid=False, show_frame=False, width=500, height=500,\n", | ||
" color_index='Unemployment', colorbar=True, toolbar='above')\n", | ||
"style = dict(line_color='white')\n", | ||
"\n", | ||
"choropleth({'Polygons': {'style': style, 'plot': plot_opts}})" | ||
"choropleth.opts(style=style, plot=plot_opts)" | ||
] | ||
} | ||
], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,11 @@ | |
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"A ``Polygons`` object is similar to a ``Contours`` object except that each supplied path is closed and filled. Just like ``Contours``, an optional ``level`` value may be supplied; the Polygons will then be colored according to the supplied ``cmap``. Non-finite values such as ``np.NaN`` or ``np.inf`` will default to the supplied ``facecolor``.\n", | ||
"A ``Polygons`` represents a contiguous filled area in a 2D space as a list of paths. Just like the ``Contours`` element additional scalar value dimensions maybe may be supplied, which can be used to color the ``Polygons`` with the defined ``cmap``. Like other ``Path`` types it accepts a list of arrays, dataframes, a dictionary of columns (or any of the other literal formats including tuples of columns and lists of tuples).\n", | ||
"\n", | ||
"Polygons with values can be used to build heatmaps with arbitrary shapes." | ||
"In order to efficiently represent the scalar values associated with each path the dictionary format is preferable since it can store the scalar values without expanding them into a whole column. Additionally it allows passing multiple columns as a single array by specifying the dimension names as a tuple.\n", | ||
"\n", | ||
"In this example we will createa list of random polygons each with an associated ``level`` value. Polygons will default to using the first value dimension as the ``color_index`` but for clarity we will define the ``color_index`` explicitly:" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. createa -> create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. |
||
] | ||
}, | ||
{ | ||
|
@@ -39,19 +41,21 @@ | |
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%%opts Polygons (cmap='hot' line_color='black' line_width=2)\n", | ||
"np.random.seed(35)\n", | ||
"hv.Polygons([np.random.rand(4,2)], level=0.5) *\\\n", | ||
"hv.Polygons([np.random.rand(4,2)], level=1.0) *\\\n", | ||
"hv.Polygons([np.random.rand(4,2)], level=1.5) *\\\n", | ||
"hv.Polygons([np.random.rand(4,2)], level=2.0)" | ||
"%%opts Polygons [color_index='level'] (line_color='black' line_width=1)\n", | ||
"np.random.seed(1)\n", | ||
"\n", | ||
"def rectangle(x=0, y=0, width=.05, height=.05):\n", | ||
" return np.array([(x,y), (x+width, y), (x+width, y+height), (x, y+height)])\n", | ||
"\n", | ||
"hv.Polygons([{('x', 'y'): rectangle(x, y), 'level': z}\n", | ||
" for x, y, z in np.random.rand(100, 3)], vdims=['level']).redim.range(x=(-.1,1.1), y=(-0.1, 1.1))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"``Polygons`` without a value are useful as annotation, but also allow us to draw arbitrary shapes." | ||
"``Polygons`` is a very versatile element which may be used to draw custom annotations, choropleth maps (as can be seen in the [texas_unemploment example](../../../gallery/demos/bokeh/texas_choropleth_example.ipynb)) among many other examples. We can also use some of the other path based annotations to quickly generate polygons, including ``Box``, ``Bounds`` and ``Ellipse`` elements. In the simple case we can simply pass a list of these elements:" | ||
] | ||
}, | ||
{ | ||
|
@@ -60,12 +64,24 @@ | |
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def rectangle(x=0, y=0, width=1, height=1):\n", | ||
" return np.array([(x,y), (x+width, y), (x+width, y+height), (x, y+height)])\n", | ||
"\n", | ||
"(hv.Polygons([rectangle(width=2), rectangle(x=6, width=2)]).opts(style={'fill_color': '#a50d0d'})\n", | ||
"* hv.Polygons([rectangle(x=2, height=2), rectangle(x=5, height=2)]).opts(style={'fill_color': '#ffcc00'})\n", | ||
"* hv.Polygons([rectangle(x=3, height=2, width=2)]).opts(style={'fill_color': 'cyan'}))" | ||
"hv.Polygons([hv.Box(i, i, i) for i in range(1, 10)])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Alternatively we can use the ``array`` method to return the x/y-coordinates of the annotations and define additional z-values by declaring a dictionary:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"hv.Polygons([{('x', 'y'): hv.Box(0, 0, i).array(), 'z': i} for i in range(1, 10)[::-1]], vdims=['z']) +\\\n", | ||
"hv.Polygons([{('x', 'y'): hv.Ellipse(0, 0, (i, i)).array(), 'z': i} for i in range(1, 10)[::-1]], vdims=['z'])" | ||
] | ||
} | ||
], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this have implications for backwards compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess it has some, it's no longer
overlaid
by default. I could have made it be overlaid again but I generally don't want to.