Skip to content

Commit

Permalink
Consolidate plotting capabilities in Model.plot (#3282)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonathan Shimwell <[email protected]>
  • Loading branch information
paulromano and shimwell authored Jan 31, 2025
1 parent d9c8e59 commit 59c398b
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 424 deletions.
56 changes: 2 additions & 54 deletions openmc/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import openmc.checkvalue as cv
from ._xml import get_text
from .mixin import IDManagerMixin
from .plots import add_plot_params
from .region import Region, Complement
from .surface import Halfspace
from .bounding_box import BoundingBox
Expand Down Expand Up @@ -559,64 +560,11 @@ def clone(self, clone_materials=True, clone_regions=True, memo=None):

return memo[self]

@add_plot_params
def plot(self, *args, **kwargs):
"""Display a slice plot of the cell.
.. versionadded:: 0.14.0
Parameters
----------
origin : iterable of float
Coordinates at the origin of the plot. If left as None then the
bounding box center will be used to attempt to ascertain the origin.
Defaults to (0, 0, 0) if the bounding box is not finite
width : iterable of float
Width of the plot in each basis direction. If left as none then the
bounding box width will be used to attempt to ascertain the plot
width. Defaults to (10, 10) if the bounding box is not finite
pixels : Iterable of int or int
If iterable of ints provided, then this directly sets the number of
pixels to use in each basis direction. If int provided, then this
sets the total number of pixels in the plot and the number of pixels
in each basis direction is calculated from this total and the image
aspect ratio.
basis : {'xy', 'xz', 'yz'}
The basis directions for the plot
color_by : {'cell', 'material'}
Indicate whether the plot should be colored by cell or by material
colors : dict
Assigns colors to specific materials or cells. Keys are instances of
:class:`Cell` or :class:`Material` and values are RGB 3-tuples, RGBA
4-tuples, or strings indicating SVG color names. Red, green, blue,
and alpha should all be floats in the range [0.0, 1.0], for example:
.. code-block:: python
# Make water blue
water = openmc.Cell(fill=h2o)
water.plot(colors={water: (0., 0., 1.)})
seed : int
Seed for the random number generator
openmc_exec : str
Path to OpenMC executable.
axes : matplotlib.Axes
Axes to draw to
legend : bool
Whether a legend showing material or cell names should be drawn
legend_kwargs : dict
Keyword arguments passed to :func:`matplotlib.pyplot.legend`.
outline : bool
Whether outlines between color boundaries should be drawn
axis_units : {'km', 'm', 'cm', 'mm'}
Units used on the plot axis
**kwargs
Keyword arguments passed to :func:`matplotlib.pyplot.imshow`
Returns
-------
matplotlib.axes.Axes
Axes containing resulting image
"""
# Create dummy universe but preserve used_ids
next_id = openmc.Universe.next_id
Expand Down
55 changes: 2 additions & 53 deletions openmc/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import openmc
import openmc._xml as xml
from .plots import add_plot_params
from .checkvalue import check_type, check_less_than, check_greater_than, PathLike


Expand Down Expand Up @@ -747,62 +748,10 @@ def clone(self) -> Geometry:
clone.root_universe = self.root_universe.clone()
return clone

@add_plot_params
def plot(self, *args, **kwargs):
"""Display a slice plot of the geometry.
.. versionadded:: 0.14.0
Parameters
----------
origin : iterable of float
Coordinates at the origin of the plot. If left as None then the
bounding box center will be used to attempt to ascertain the origin.
Defaults to (0, 0, 0) if the bounding box is not finite
width : iterable of float
Width of the plot in each basis direction. If left as none then the
bounding box width will be used to attempt to ascertain the plot
width. Defaults to (10, 10) if the bounding box is not finite
pixels : Iterable of int or int
If iterable of ints provided, then this directly sets the number of
pixels to use in each basis direction. If int provided, then this
sets the total number of pixels in the plot and the number of pixels
in each basis direction is calculated from this total and the image
aspect ratio.
basis : {'xy', 'xz', 'yz'}
The basis directions for the plot
color_by : {'cell', 'material'}
Indicate whether the plot should be colored by cell or by material
colors : dict
Assigns colors to specific materials or cells. Keys are instances of
:class:`Cell` or :class:`Material` and values are RGB 3-tuples, RGBA
4-tuples, or strings indicating SVG color names. Red, green, blue,
and alpha should all be floats in the range [0.0, 1.0], for
example::
# Make water blue
water = openmc.Cell(fill=h2o)
universe.plot(..., colors={water: (0., 0., 1.))
seed : int
Seed for the random number generator
openmc_exec : str
Path to OpenMC executable.
axes : matplotlib.Axes
Axes to draw to
legend : bool
Whether a legend showing material or cell names should be drawn
legend_kwargs : dict
Keyword arguments passed to :func:`matplotlib.pyplot.legend`.
outline : bool
Whether outlines between color boundaries should be drawn
axis_units : {'km', 'm', 'cm', 'mm'}
Units used on the plot axis
**kwargs
Keyword arguments passed to :func:`matplotlib.pyplot.imshow`
Returns
-------
matplotlib.axes.Axes
Axes containing resulting image
"""

return self.root_universe.plot(*args, **kwargs)
Loading

0 comments on commit 59c398b

Please sign in to comment.