You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(plot): add functions to plot distributions of particle data
Add `ozzy.plot.hist` and `ozzy.plot.hist_proj` to easily plot density distributions (histograms) of particle data, taking advantage of the seaborn functions [`seaborn.histplot`](https://seaborn.pydata.org/generated/seaborn.histplot.html) and [`seaborn.jointplot`](https://seaborn.pydata.org/generated/seaborn.jointplot.html).
Previously it would have been necessary to bin the data first, and then plot, e.g.:
```python
import ozzy as oz
import ozzy.plot as oplt
# A particle data Dataset
ds = oz.Dataset(..., pic_data_type="part")
ds_ps = ds.ozzy.get_phase_space(["p2", "x2"])
ds_ps["rho"].plot()
```
While now the following code is enough:
```python
import ozzy as oz
import ozzy.plot as oplt
ds = oz.Dataset(..., pic_data_type='part')
oplt.hist(ds, x="x2", y="p2")
```
"xtick.bottom": True, # draw ticks on the bottom side
213
+
"ytick.left": True, # draw ticks on the left side
214
+
"axes.edgecolor": "black",
213
215
}
214
216
215
217
sns.set_theme(
216
-
style="ticks",
218
+
style="whitegrid",
217
219
font="serif",
218
220
rc=ozparams,
219
221
)
@@ -1093,3 +1095,215 @@ def imovie(
1093
1095
)
1094
1096
1095
1097
returnhvobj
1098
+
1099
+
1100
+
defhist(
1101
+
do: xr.Dataset|xr.DataArray,
1102
+
x: str|None=None,
1103
+
y: str|None=None,
1104
+
weight_var: str|None="q",
1105
+
bins: str|int|Iterable="auto",
1106
+
cmap: str|None="cmc.bamako",
1107
+
cbar: bool=False,
1108
+
**kwargs,
1109
+
) ->mpl.axes.Axes:
1110
+
"""Create a weighted histogram plot using [`seaborn.histplot`][seaborn.histplot].
1111
+
1112
+
Parameters
1113
+
----------
1114
+
do : xarray.Dataset | xarray.DataArray
1115
+
Input Dataset or DataArray to plot
1116
+
x : str | None
1117
+
Variable name for x-axis
1118
+
y : str | None
1119
+
Variable name for y-axis
1120
+
weight_var : str | None
1121
+
Variable name to use as weights
1122
+
bins : str | int | Iterable
1123
+
Generic bin parameter passed to [`seaborn.histplot`][seaborn.histplot]. It can be `'auto'`, the number of bins, or the breaks of the bins. Defaults to `200` for weighted data or to an automatically calculated number for unweighted data.
1124
+
cmap : str | None
1125
+
Colormap name. Uses `'cmc.bamako'` or the `ozzy.plot` sequential default
1126
+
cbar : bool
1127
+
Whether to display colorbar
1128
+
**kwargs
1129
+
Additional keyword arguments passed to [`seaborn.histplot()`][seaborn.histplot]
"""Create a 2D histogram plot with projected distributions using [`seaborn.jointplot(kind="hist")`][seaborn.jointplot].
1217
+
1218
+
Parameters
1219
+
----------
1220
+
do : xarray.Dataset | xarray.DataArray
1221
+
Input Dataset or DataArray to plot
1222
+
x : str
1223
+
Variable name for x-axis
1224
+
y : str
1225
+
Variable name for y-axis
1226
+
weight_var : str | None
1227
+
Variable name to use as weights
1228
+
bins : str | int | Iterable
1229
+
Generic bin parameter passed to [`seaborn.histplot`][seaborn.histplot]. It can be `'auto'`, the number of bins, or the breaks of the bins. Defaults to `200` for weighted data or to an automatically calculated number for unweighted data.
1230
+
cmap : str | None
1231
+
Colormap name. Uses `'cmc.bamako'` or the `ozzy.plot` sequential default
1232
+
space : float
1233
+
Space between 2D plot and marginal projection plots
1234
+
refline : bool
1235
+
Whether to add reference lines (see [`seaborn.JointGrid.refline`][seaborn.JointGrid.refline])
1236
+
refline_kwargs : dict
1237
+
Keyword arguments for reference lines (see [`seaborn.JointGrid.refline`][seaborn.JointGrid.refline])
1238
+
**kwargs
1239
+
Additional keyword arguments passed to [`seaborn.jointplot()`][seaborn.jointplot]
1240
+
1241
+
Returns
1242
+
-------
1243
+
seaborn.JointGrid
1244
+
The joint grid plot object
1245
+
1246
+
Examples
1247
+
--------
1248
+
???+ example "2D histogram with projected distributions"
1249
+
```python
1250
+
import ozzy as oz
1251
+
import ozzy.plot as oplt
1252
+
ds = oz.Dataset(...)
1253
+
jg = oplt.hist_proj(ds, x='x2', y='p2')
1254
+
```
1255
+
1256
+
???+ example "2D histogram with projected distributions and reference lines"
1257
+
```python
1258
+
import ozzy as oz
1259
+
import ozzy.plot as oplt
1260
+
ds = oz.Dataset(...)
1261
+
jg = oplt.hist_proj(ds, x='x2', y='p2',
1262
+
refline=True,
1263
+
refline_kwargs={'x': 0, 'y': 0})
1264
+
```
1265
+
"""
1266
+
ifcmapisNone:
1267
+
cmap=xr.get_options()["cmap_sequential"]
1268
+
1269
+
if (weight_varisnotNone) and (bins=="auto"):
1270
+
bins=200
1271
+
1272
+
jg=sns.jointplot(
1273
+
do.to_dataframe(),
1274
+
x=x,
1275
+
y=y,
1276
+
weights=weight_var,
1277
+
bins=bins,
1278
+
space=space,
1279
+
cmap=cmap,
1280
+
kind="hist",
1281
+
color=mpl.colormaps[cmap](
1282
+
0.0
1283
+
), # choose the lower bound of the color scale as the color for the projected bins
0 commit comments