# \[Docs\] RuntimeError: Bad RGB in plot

**URL:** <https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925>\
**Category:** User Support\
**Created:** [April 17, 2023, 7:29am UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925 "2023-04-17T07:29:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingyue](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/kingyue/32/507_2.png) [@kingyue](https://openmc.discourse.group/u/kingyue)\
**Post date:** [April 17, 2023, 7:29am UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/1 "2023-04-17T07:29:22Z")

</div>

According to the documentation [openmc.Universe — OpenMC Documentation](https://docs.openmc.org/en/stable/pythonapi/generated/openmc.Universe.html#openmc.Universe.plot), for parameter `colors` of method `openmc.Universe.plot`, red, green, blue, and alpha should all be floats in the range [0.0, 1.0]. But the following code results into a runtime error `Bad RGB in plot`:

```python
geometry.root_universe.plot(
    width=(200, 200),
    pixels=(800, 800),
    origin=(0, 0, 250),
    color_by="material",
    colors={water:(0.,0.,1.)},
)

```

It seems that rgb should be int in the range [0,255]. I don’t know whether it originates from a breaking change of matplotlib or openmc. Maybe we should update the docs?

---

<div class="post-metadata">

**Author:** ![kingyue](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/kingyue/32/507_2.png) [@kingyue](https://openmc.discourse.group/u/kingyue)\
**Post date:** [April 17, 2023, 7:36am UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/2 "2023-04-17T07:36:55Z")

</div>

Furthermore, does this API really support rgba? It seems that a RGBA 4-tuples will always raise runtime error

---

<div class="post-metadata">

**Author:** ![paulromano](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/paulromano/32/486_2.png) [@paulromano](https://openmc.discourse.group/u/paulromano)\
**Post date:** [April 17, 2023, 2:39pm UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/3 "2023-04-17T14:39:45Z")

</div>

Thanks for pointing this out @kingyue. We recently changed what happens under the hood when you call `Universe.plot`. Before it used to be generated/colored by matplotlib, but now it relies on the `openmc` executable to produce a png and then simply shows that png and the coloring options are slightly more restrictive as you’ve found. I’ll get the documentation updated. You are correct that alpha channel is not supported.

---

<div class="post-metadata">

**Author:** ![yrrepy](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/yrrepy/32/2365_2.png) [@yrrepy](https://openmc.discourse.group/u/yrrepy)\
**Post date:** [April 21, 2023, 3:49pm UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/4 "2023-04-21T15:49:36Z")

</div>

Piggybacking on to this discussion as it involves issues with RGB etc.

I’ve just been looking into the Python universe.plot plotting.

@Shimwell, them outlines are rad  
@gridley, auto-legend is nice and handy, but I can’t seem to get it working with RGB e.g.: (10, 255, 20), it complains that it’s not RGBA, then I make it RGBA and it doesn’t like that either. I can only get it working with color names e.g.:‘blue’  
A consequence/constraint of the patches routine?

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [April 21, 2023, 7:39pm UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/5 "2023-04-21T19:39:46Z")

</div>

I did have a method of getting openmc to generate matplotlib plots using RGB values that I coded up and put into a [prototyping package](https://github.com/fusion-energy/openmc_geometry_plot).

Recently I’ve been porting functionality across from that package to openmc and I was planning to tackle this RGB topic at some point.

---

<div class="post-metadata">

**Author:** ![gridley](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/gridley/32/1064_2.png) [@gridley](https://openmc.discourse.group/u/gridley)\
**Post date:** [April 21, 2023, 8:46pm UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/6 "2023-04-21T20:46:20Z")

</div>

Thanks for the heads up! This PR fixes that behavior. I didn’t realize matplotlib expected RGB values to range from 0-1 rather than 0-255.

> <https://github.com/openmc-dev/openmc/pull/2487>
>
> Addresses the problem brought up here:
> 
> https://openmc.discourse.group/t/docs-…runtimeerror-bad-rgb-in-plot/2925/4
> 
> Apparently matplotlib expects RGB values to range from 0-1 rather than 0-255, so this fixes that. The message was confusing because it seemed to be originating from OpenMC and talks about RGBA values, but we've never included an alpha when specifying colors in plots. Now, the standard plot color runtime checks will detect incorrectly formatted color inputs, and only valid colors get passed to matplotlib for universe plot legend generation.

---

<div class="post-metadata">

**Author:** ![Shimwell](https://yyz2.discourse-cdn.com/free1/user_avatar/openmc.discourse.group/shimwell/32/3876_2.png) [@Shimwell](https://openmc.discourse.group/u/Shimwell)\
**Post date:** [April 27, 2023, 8:37am UTC](https://openmc.discourse.group/t/docs-runtimeerror-bad-rgb-in-plot/2925/7 "2023-04-27T08:37:51Z")

</div>

The RGB legend fix made by @gridley has now been merged into the develop branch
