Skip to content

Commit ac892c2

Browse files
authored
Merge pull request #42 from nvaytet/cleanup_wrappers
Cleanup wrappers
2 parents 91de71f + 2a0276f commit ac892c2

File tree

4 files changed

+184
-121
lines changed

4 files changed

+184
-121
lines changed

docs/plotting.ipynb

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"cell_type": "markdown",
3333
"metadata": {},
3434
"source": [
35-
"## 1. A 2D histogram of density vs magnetic field\n",
35+
"## A 2D histogram of density vs magnetic field\n",
3636
"\n",
3737
"By default, the `histogram2d` function will show a binned count of cells"
3838
]
@@ -71,7 +71,7 @@
7171
"cell_type": "markdown",
7272
"metadata": {},
7373
"source": [
74-
"## 2. Simple cut plane\n",
74+
"## Simple cut plane\n",
7575
"\n",
7676
"Create a 2D gas density slice 2000 au wide through the plane normal to `z`,\n",
7777
"with velocity vectors overlayed as arrows, once agains using `layers`:"
@@ -80,9 +80,7 @@
8080
{
8181
"cell_type": "code",
8282
"execution_count": null,
83-
"metadata": {
84-
"scrolled": false
85-
},
83+
"metadata": {},
8684
"outputs": [],
8785
"source": [
8886
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"}, # layer 1\n",
@@ -96,7 +94,7 @@
9694
"cell_type": "markdown",
9795
"metadata": {},
9896
"source": [
99-
"## 3. Cut plane at an arbitrary angle\n",
97+
"## Cut plane at an arbitrary angle\n",
10098
"\n",
10199
"The `plane` function will also accept a vector to define the normal direction to the plane.\n",
102100
"In this example, we also change the colormap."
@@ -105,9 +103,7 @@
105103
{
106104
"cell_type": "code",
107105
"execution_count": null,
108-
"metadata": {
109-
"scrolled": false
110-
},
106+
"metadata": {},
111107
"outputs": [],
112108
"source": [
113109
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
@@ -122,7 +118,7 @@
122118
"cell_type": "markdown",
123119
"metadata": {},
124120
"source": [
125-
"## 4. Automatic “top/side” slice orientation according to angular momentum\n",
121+
"## Automatic “top/side” slice orientation according to angular momentum\n",
126122
"\n",
127123
"Create a 2D slice of the logarithm of density 500 au wide using automatic orientation based on the angular momentum in the data.\n",
128124
"This is useful for looking at disks.\n",
@@ -166,7 +162,40 @@
166162
"cell_type": "markdown",
167163
"metadata": {},
168164
"source": [
169-
"## 5. Embedding plots in existing Matplotlib axes\n",
165+
"## Coloring vectors and streamlines\n",
166+
"\n",
167+
"In this example, we represent the velocity field as vectors (left) and as streamlines (right).\n",
168+
"The colours represent the magnitude of the velocity."
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"metadata": {},
175+
"outputs": [],
176+
"source": [
177+
"fig, ax = plt.subplots(1, 2, figsize=(10, 3.5))\n",
178+
"plt.subplots_adjust(wspace=0.35)\n",
179+
"\n",
180+
"dx = 2000.0 * osyris.units(\"au\")\n",
181+
"\n",
182+
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
183+
" {\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"vec\",\n",
184+
" \"color\": data[\"hydro\"][\"velocity\"], \"cmap\": \"spring_r\",\n",
185+
" \"cbar\": False},\n",
186+
" dx=dx, origin=center, direction=\"z\", ax=ax[0])\n",
187+
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
188+
" {\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"stream\",\n",
189+
" \"color\": data[\"hydro\"][\"velocity\"], \"cmap\": \"jet\",\n",
190+
" \"cbar\": False},\n",
191+
" dx=dx, origin=center, direction=\"z\", ax=ax[1])"
192+
]
193+
},
194+
{
195+
"cell_type": "markdown",
196+
"metadata": {},
197+
"source": [
198+
"## Embedding plots in existing Matplotlib axes\n",
170199
"\n",
171200
"In this example, we create two subplot axes with Matplotlib.\n",
172201
"\n",
@@ -182,9 +211,7 @@
182211
{
183212
"cell_type": "code",
184213
"execution_count": null,
185-
"metadata": {
186-
"scrolled": false
187-
},
214+
"metadata": {},
188215
"outputs": [],
189216
"source": [
190217
"# Create figure\n",
@@ -224,7 +251,7 @@
224251
"cell_type": "markdown",
225252
"metadata": {},
226253
"source": [
227-
"## 6. Plot only a subset of cells belonging to a disk\n",
254+
"## Plot only a subset of cells belonging to a disk\n",
228255
"\n",
229256
"In this example, we select cells according to their density and plot only those.\n",
230257
"This is done by creating a new field and using Numpy's `masked_where` function.\n",
@@ -271,7 +298,7 @@
271298
"cell_type": "markdown",
272299
"metadata": {},
273300
"source": [
274-
"## 7. Difference between two snapshots\n",
301+
"## Difference between two snapshots\n",
275302
"\n",
276303
"Here, we want to make a map of the difference in density between two snapshots.\n",
277304
"Because we do not necessarily have the same number of cells at the same place,\n",
@@ -330,7 +357,7 @@
330357
"cell_type": "markdown",
331358
"metadata": {},
332359
"source": [
333-
"## 8. Slice above the origin\n",
360+
"## Slice above the origin\n",
334361
"\n",
335362
"We want to plot a slice of density but through a point which is 20 AU above the centre of the domain,\n",
336363
"defined as the cell with the highest density."
@@ -355,7 +382,7 @@
355382
"cell_type": "markdown",
356383
"metadata": {},
357384
"source": [
358-
"## 9. Scatter plot\n",
385+
"## Scatter plot\n",
359386
"\n",
360387
"Make a scatter plot of density vs temperature, with only 1 in 1000 cells.\n",
361388
"Colour the dots according to the magnitude of the gas velocity."
@@ -378,7 +405,7 @@
378405
"cell_type": "markdown",
379406
"metadata": {},
380407
"source": [
381-
"## 10. Sink particles\n",
408+
"## Sink particles\n",
382409
"\n",
383410
"Some star formation simulations contain sink particles,\n",
384411
"and these can be represented as a scatter layer on a plane plot. "
@@ -401,7 +428,7 @@
401428
"cell_type": "markdown",
402429
"metadata": {},
403430
"source": [
404-
"## 11. 2D data\n",
431+
"## 2D data\n",
405432
"\n",
406433
"2D data can be loaded and viewed just like 3D data."
407434
]
@@ -420,7 +447,7 @@
420447
"cell_type": "markdown",
421448
"metadata": {},
422449
"source": [
423-
"## 12. 1D histograms\n",
450+
"## 1D histograms\n",
424451
"\n",
425452
"The `histogram1d` function provides a simple way to quickly make 1d histogram plots:"
426453
]
@@ -477,7 +504,7 @@
477504
"cell_type": "markdown",
478505
"metadata": {},
479506
"source": [
480-
"## 13. Line integral convolution vector field visualizations\n",
507+
"## Line integral convolution vector field visualizations\n",
481508
"\n",
482509
"Osyris now supports line integral convolution (LIC) visualizations of vector fields. This visualization method, although computationally intensive, offers an excellent representation of the vector field (especially near discontinuities) provided we have enough resolution.\n",
483510
"\n",
@@ -493,7 +520,7 @@
493520
"outputs": [],
494521
"source": [
495522
"osyris.plane({\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"lic\"},\n",
496-
" dx=dx * osyris.units(\"au\"),\n",
523+
" dx=2000 * osyris.units(\"au\"),\n",
497524
" origin=center,\n",
498525
" direction=\"z\", resolution=1000)"
499526
]
@@ -513,7 +540,7 @@
513540
"source": [
514541
"osyris.plane({\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"lic\",\n",
515542
" \"color\": data[\"hydro\"][\"density\"], \"norm\": \"log\", \"length\":30},\n",
516-
" dx=dx * osyris.units(\"au\"),\n",
543+
" dx=2000 * osyris.units(\"au\"),\n",
517544
" origin=center,\n",
518545
" direction=\"z\", resolution=1000)"
519546
]
@@ -528,7 +555,7 @@
528555
],
529556
"metadata": {
530557
"kernelspec": {
531-
"display_name": "Python 3",
558+
"display_name": "Python 3 (ipykernel)",
532559
"language": "python",
533560
"name": "python3"
534561
},
@@ -542,9 +569,9 @@
542569
"name": "python",
543570
"nbconvert_exporter": "python",
544571
"pygments_lexer": "ipython3",
545-
"version": "3.8.8"
572+
"version": "3.9.5"
546573
}
547574
},
548575
"nbformat": 4,
549-
"nbformat_minor": 2
576+
"nbformat_minor": 4
550577
}

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = osyris
3-
version = 2.3.0
3+
version = 2.3.1
44
author = Neil Vaytet
55
author_email = [email protected]
66
description = A package to visualize AMR data from the RAMSES code

src/osyris/plot/render.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,20 @@ def render(x=None, y=None, data=None, logx=False, logy=False, ax=None):
4949
else:
5050
cbar = True
5151

52-
mpl_objects.extend(
53-
getattr(wrappers, func)(ax, x, y, item["data"], **item["params"]))
54-
55-
need_cbar = False
56-
57-
ind_render = -1
58-
if func != "scatter":
59-
name = item["name"]
60-
unit = item["unit"]
61-
6252
if func == "line_integral_convolution" and "color" in item["params"]:
63-
need_cbar = True
64-
ind_render = -2
65-
name = item["params"]["color"].name
66-
unit = item["params"]["color"].unit.units
53+
cblabel = make_label(name=item["params"]["color"].name,
54+
unit=item["params"]["color"].unit.units)
55+
else:
56+
cblabel = make_label(name=item.get("name", ""), unit=item.get("unit", ""))
57+
58+
mpl_objects.append(
59+
getattr(wrappers, func)(ax=ax,
60+
x=x,
61+
y=y,
62+
z=item["data"],
63+
cbar=cbar,
64+
cblabel=cblabel,
65+
**item["params"]))
6766

68-
if func in ["contourf", "pcolormesh"]:
69-
need_cbar = True
70-
if (func == "scatter") and ("c" in item["params"]):
71-
if not isinstance(item["params"]["c"], str):
72-
need_cbar = True
73-
name = item["name"]
74-
unit = item["unit"]
75-
if need_cbar and cbar:
76-
cb = plt.colorbar(mpl_objects[ind_render], ax=ax, cax=None)
77-
cb.set_label(make_label(name=name, unit=unit))
78-
cb.ax.yaxis.set_label_coords(-1.1, 0.5)
7967
out["objects"] = mpl_objects
8068
return out

0 commit comments

Comments
 (0)