# MatPlotLib into WandB

**URL:** <https://community.wandb.ai/t/matplotlib-into-wandb/3212>\
**Category:** W&B Help\
**Tags:** wandb\
**Created:** [October 3, 2022, 4:23am UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212 "2022-10-03T04:23:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kevinashaw](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/kevinashaw/32/1016_2.png) [@kevinashaw](https://community.wandb.ai/u/kevinashaw)\
**Post date:** [October 3, 2022, 4:23am UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212/1 "2022-10-03T04:23:10Z")

</div>

I need to import MatPlotLib images into WandB. On the surface, this seems simple, since the documentation clearly shows how to ingest a `plt` or `fig` object. However, WandB is making a mess of the plots and I don’t want to recode them in plotly.  
So I next want to use MatPlotLib to save a PNG and ingest that. Again seems easy, but I would prefer to do it using an in-memory buffer object (this avoids messing with local paths and temp directories on various instances). Apparently I’m not the first one to do this either ([link](https://stackoverflow.com/questions/35999020/convert-pyplot-figure-into-wand-image-image)). The instructions are clear and show someone has already done this. But it fails when I try it:

```auto
fig, (ax1, ax2) = plt.subplots(2, 1, dpi=300, figsize=(10, 5))
...
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
wandb.log(({"chart": wandb.Image(file=buf)}))

```

The error seems to be with `wandb.Image()`. It returns:  
`{TypeError} __init__ () got an unexpected keyword argument 'file'`

I can remove the `file=` parameter so that the command is:

```auto
wandb.Image(buf)

```

And I get: `{AttributeError}'_io.BytesIO' object has no attribute 'ndim'`

Any recommendations?

---

<div class="post-metadata">

**Author:** ![ramit\_goolry](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@ramit\_goolry](https://community.wandb.ai/u/ramit_goolry)\
**Post date:** [October 3, 2022, 5:26pm UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212/2 "2022-10-03T17:26:35Z")

</div>

Hi @kevinashaw!

The `BytesIO` type is not supported by `wandb.Image` which is why you are running into this issue. Here are a few options that would work instead:

- `wandb.log({ 'chart' : wandb.Image(Image.open(buf)) })`
- `wandb.log({ 'chart' : wandb.Image(fig) })`
- `wandb.log({ 'chart' : fig })` (Please note that this does not actually save an image but an interactable Plotly chart on your workspace

Thanks,  
Ramit

---

<div class="post-metadata">

**Author:** ![kevinashaw](https://sea2.discourse-cdn.com/flex020/user_avatar/community.wandb.ai/kevinashaw/32/1016_2.png) [@kevinashaw](https://community.wandb.ai/u/kevinashaw)\
**Post date:** [October 5, 2022, 4:18am UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212/3 "2022-10-05T04:18:50Z")

</div>

Thank you, @ramit_goolry!  
The first line you provided worked perfectly!

---

<div class="post-metadata">

**Author:** ![ramit\_goolry](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@ramit\_goolry](https://community.wandb.ai/u/ramit_goolry)\
**Post date:** [October 5, 2022, 8:49pm UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212/4 "2022-10-05T20:49:59Z")

</div>

Glad to hear! I’ll close this request for now, but if any other issues seem to pop up here, feel free to reply to this thread!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/wandb/original/1X/366b649231631dbab896843020da0056074ac79d.png) [@system](https://community.wandb.ai/u/system)\
**Post date:** [December 4, 2022, 4:18am UTC](https://community.wandb.ai/t/matplotlib-into-wandb/3212/5 "2022-12-04T04:18:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
