Print out the corresponding cell in the geometry plot

Hi,

I am wondering how I can print the corresponding cell number in the geometry plot when I plot the geometry with “color by cell”.

Thanks,
Fahima

1 Like

Hi Fahima,

I started thinking about this and ended up with a minimal example for the PWR pincell example model using the Python API. The easiest way I could see to do this is to apply known material/cell colors to your plot, then manually generate the legend you want.

import openmc
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches


pincell = openmc.examples.pwr_pin_cell()
materials = pincell.materials
mat_colors = {materials[0] : 'green',
              materials[1] : 'gray',
              materials[2] : 'blue'}

a = pincell.geometry.root_universe.plot(colors=mat_colors, color_by='material')

patches = []
for mat, color in mat_colors.items():
    mat_patch = mpatches.Patch(color=color, label=mat.name)
    patches.append(mat_patch)

plt.legend(handles=patches)
plt.show()

A legend=True feature like this might be nice to add to our plot methods at some point, but for now this is what I’d recommend.

Unless you were looking for labels placed on top of the colored regions? That could be done as well with matplotlib text boxes, but you’d have to know where to place them ahead of time. Just means some iteration on plot and text placement.

Hope this helps!

-Patrick

Edit: Adding example image from that script

1 Like