Skip to content

Commit

Permalink
Merge pull request #42 from nvaytet/cleanup_wrappers
Browse files Browse the repository at this point in the history
Cleanup wrappers
  • Loading branch information
nvaytet authored Nov 14, 2021
2 parents 91de71f + 2a0276f commit ac892c2
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 121 deletions.
81 changes: 54 additions & 27 deletions docs/plotting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. A 2D histogram of density vs magnetic field\n",
"## A 2D histogram of density vs magnetic field\n",
"\n",
"By default, the `histogram2d` function will show a binned count of cells"
]
Expand Down Expand Up @@ -71,7 +71,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Simple cut plane\n",
"## Simple cut plane\n",
"\n",
"Create a 2D gas density slice 2000 au wide through the plane normal to `z`,\n",
"with velocity vectors overlayed as arrows, once agains using `layers`:"
Expand All @@ -80,9 +80,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"}, # layer 1\n",
Expand All @@ -96,7 +94,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Cut plane at an arbitrary angle\n",
"## Cut plane at an arbitrary angle\n",
"\n",
"The `plane` function will also accept a vector to define the normal direction to the plane.\n",
"In this example, we also change the colormap."
Expand All @@ -105,9 +103,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
Expand All @@ -122,7 +118,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Automatic “top/side” slice orientation according to angular momentum\n",
"## Automatic “top/side” slice orientation according to angular momentum\n",
"\n",
"Create a 2D slice of the logarithm of density 500 au wide using automatic orientation based on the angular momentum in the data.\n",
"This is useful for looking at disks.\n",
Expand Down Expand Up @@ -166,7 +162,40 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Embedding plots in existing Matplotlib axes\n",
"## Coloring vectors and streamlines\n",
"\n",
"In this example, we represent the velocity field as vectors (left) and as streamlines (right).\n",
"The colours represent the magnitude of the velocity."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"fig, ax = plt.subplots(1, 2, figsize=(10, 3.5))\n",
"plt.subplots_adjust(wspace=0.35)\n",
"\n",
"dx = 2000.0 * osyris.units(\"au\")\n",
"\n",
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" {\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"vec\",\n",
" \"color\": data[\"hydro\"][\"velocity\"], \"cmap\": \"spring_r\",\n",
" \"cbar\": False},\n",
" dx=dx, origin=center, direction=\"z\", ax=ax[0])\n",
"osyris.plane({\"data\": data[\"hydro\"][\"density\"], \"norm\": \"log\"},\n",
" {\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"stream\",\n",
" \"color\": data[\"hydro\"][\"velocity\"], \"cmap\": \"jet\",\n",
" \"cbar\": False},\n",
" dx=dx, origin=center, direction=\"z\", ax=ax[1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Embedding plots in existing Matplotlib axes\n",
"\n",
"In this example, we create two subplot axes with Matplotlib.\n",
"\n",
Expand All @@ -182,9 +211,7 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [],
"source": [
"# Create figure\n",
Expand Down Expand Up @@ -224,7 +251,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Plot only a subset of cells belonging to a disk\n",
"## Plot only a subset of cells belonging to a disk\n",
"\n",
"In this example, we select cells according to their density and plot only those.\n",
"This is done by creating a new field and using Numpy's `masked_where` function.\n",
Expand Down Expand Up @@ -271,7 +298,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Difference between two snapshots\n",
"## Difference between two snapshots\n",
"\n",
"Here, we want to make a map of the difference in density between two snapshots.\n",
"Because we do not necessarily have the same number of cells at the same place,\n",
Expand Down Expand Up @@ -330,7 +357,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. Slice above the origin\n",
"## Slice above the origin\n",
"\n",
"We want to plot a slice of density but through a point which is 20 AU above the centre of the domain,\n",
"defined as the cell with the highest density."
Expand All @@ -355,7 +382,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 9. Scatter plot\n",
"## Scatter plot\n",
"\n",
"Make a scatter plot of density vs temperature, with only 1 in 1000 cells.\n",
"Colour the dots according to the magnitude of the gas velocity."
Expand All @@ -378,7 +405,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 10. Sink particles\n",
"## Sink particles\n",
"\n",
"Some star formation simulations contain sink particles,\n",
"and these can be represented as a scatter layer on a plane plot. "
Expand All @@ -401,7 +428,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 11. 2D data\n",
"## 2D data\n",
"\n",
"2D data can be loaded and viewed just like 3D data."
]
Expand All @@ -420,7 +447,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 12. 1D histograms\n",
"## 1D histograms\n",
"\n",
"The `histogram1d` function provides a simple way to quickly make 1d histogram plots:"
]
Expand Down Expand Up @@ -477,7 +504,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 13. Line integral convolution vector field visualizations\n",
"## Line integral convolution vector field visualizations\n",
"\n",
"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",
"\n",
Expand All @@ -493,7 +520,7 @@
"outputs": [],
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"lic\"},\n",
" dx=dx * osyris.units(\"au\"),\n",
" dx=2000 * osyris.units(\"au\"),\n",
" origin=center,\n",
" direction=\"z\", resolution=1000)"
]
Expand All @@ -513,7 +540,7 @@
"source": [
"osyris.plane({\"data\": data[\"hydro\"][\"velocity\"], \"mode\": \"lic\",\n",
" \"color\": data[\"hydro\"][\"density\"], \"norm\": \"log\", \"length\":30},\n",
" dx=dx * osyris.units(\"au\"),\n",
" dx=2000 * osyris.units(\"au\"),\n",
" origin=center,\n",
" direction=\"z\", resolution=1000)"
]
Expand All @@ -528,7 +555,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -542,9 +569,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = osyris
version = 2.3.0
version = 2.3.1
author = Neil Vaytet
author_email = [email protected]
description = A package to visualize AMR data from the RAMSES code
Expand Down
38 changes: 13 additions & 25 deletions src/osyris/plot/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,20 @@ def render(x=None, y=None, data=None, logx=False, logy=False, ax=None):
else:
cbar = True

mpl_objects.extend(
getattr(wrappers, func)(ax, x, y, item["data"], **item["params"]))

need_cbar = False

ind_render = -1
if func != "scatter":
name = item["name"]
unit = item["unit"]

if func == "line_integral_convolution" and "color" in item["params"]:
need_cbar = True
ind_render = -2
name = item["params"]["color"].name
unit = item["params"]["color"].unit.units
cblabel = make_label(name=item["params"]["color"].name,
unit=item["params"]["color"].unit.units)
else:
cblabel = make_label(name=item.get("name", ""), unit=item.get("unit", ""))

mpl_objects.append(
getattr(wrappers, func)(ax=ax,
x=x,
y=y,
z=item["data"],
cbar=cbar,
cblabel=cblabel,
**item["params"]))

if func in ["contourf", "pcolormesh"]:
need_cbar = True
if (func == "scatter") and ("c" in item["params"]):
if not isinstance(item["params"]["c"], str):
need_cbar = True
name = item["name"]
unit = item["unit"]
if need_cbar and cbar:
cb = plt.colorbar(mpl_objects[ind_render], ax=ax, cax=None)
cb.set_label(make_label(name=name, unit=unit))
cb.ax.yaxis.set_label_coords(-1.1, 0.5)
out["objects"] = mpl_objects
return out
Loading

0 comments on commit ac892c2

Please sign in to comment.