# KDE plot does not show up on Matplotlib axis

**URL:** <https://discourse.pymc.io/t/kde-plot-does-not-show-up-on-matplotlib-axis/14668>\
**Category:** v5\
**Created:** [June 25, 2024, 3:44pm UTC](https://discourse.pymc.io/t/kde-plot-does-not-show-up-on-matplotlib-axis/14668 "2024-06-25T15:44:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sequoia99](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sequoia99](https://discourse.pymc.io/u/sequoia99)\
**Post date:** [June 25, 2024, 3:44pm UTC](https://discourse.pymc.io/t/kde-plot-does-not-show-up-on-matplotlib-axis/14668/1 "2024-06-25T15:44:12Z")

</div>

Hello,

I am attempting to use the KDE plot on an axis pre-defined with a Matplotlib subplots object. However, the KDE figure does not show up on the axis:

```auto
rng = np.random.default_rng()
data = rng.multivariate_normal([2, 2], [[1, 0.4], [0.4, 0.8]], 1000000)
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
axs[0] = az.plot_kde(data[:, 0], data[:, 1], ax=axs[0], hdi_probs=[0.393, 0.865, 0.989], contourf_kwargs={"cmap": "Blues"})

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/2/2a2f6258dbf4b12a67ab1cf0c94909463399925a.png)

The plot works fine without attempting to put it on a pre-defined axis:

```auto
rng = np.random.default_rng()
data = rng.multivariate_normal([2, 2], [[1, 0.4], [0.4, 0.8]], 1000000)
ax = az.plot_kde(data[:, 0], data[:, 1], hdi_probs=[0.393, 0.865, 0.989], contourf_kwargs={"cmap": "Blues"})

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/1/14133f5d5528c81ac44e7b9746925eaf5c19c301.png)

I know that this may seem like a duplicate of [this question](https://discourse.pymc.io/t/arviz-wont-apply-to-matplotlib-ax-plot-forest-plot-kde/6935). However, for that person the issue seemed to be that they had `az.rcParams["plot.matplotlib.show"] = True` (not sure why this would cause an issue). However, my issue persists whether or not I have `az.rcParams["plot.matplotlib.show"]` set to `True`, or `False`. For context, I am running this in a VSCode notebook.

Thanks so much for any insight you can offer!

---

<div class="post-metadata">

**Author:** ![sequoia99](https://avatars.discourse-cdn.com/v4/letter/s/ad7895/32.png) [@sequoia99](https://discourse.pymc.io/u/sequoia99)\
**Post date:** [June 25, 2024, 5:19pm UTC](https://discourse.pymc.io/t/kde-plot-does-not-show-up-on-matplotlib-axis/14668/2 "2024-06-25T17:19:27Z")

</div>

In case this is useful for anyone: I found the solution, which is to add the `show=True` argument to `plot_kde()`. I am not sure why this is different than setting `show` in rcParams to `True`, but it works.

---

<div class="post-metadata">

**Author:** ![iavicenna](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/iavicenna/32/7923_2.png) [@iavicenna](https://discourse.pymc.io/u/iavicenna)\
**Post date:** [June 26, 2024, 7:45am UTC](https://discourse.pymc.io/t/kde-plot-does-not-show-up-on-matplotlib-axis/14668/3 "2024-06-26T07:45:47Z")

</div>

> [@sequoia99](#):
>
> `fig, ax = plt.subplots(1, 1, figsize=(3, 3))`

I suspect your problem might also have been related to the fact that you are creating a single axis and trying to index into it here:

```auto
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
axs[0] = az.plot_kde(data[:, 0], data[:, 1], ax=axs[0], hdi_probs=[0.393, 0.865, 0.989], contourf_kwargs={"cmap": "Blues"})

```

This will first create an empty axis and then throw an error

```auto
TypeError: 'Axes' object is not subscriptable

```

and if you do not notice the error, all you will be seeing is an empty axis. When I changed this to

```auto
fig, ax = plt.subplots(1, 1, figsize=(3, 3))
ax = az.plot_kde(data[:, 0], data[:, 1], ax=ax, hdi_probs=[0.393, 0.865, 0.989], 
                 contourf_kwargs={"cmap": "Blues"})

```

it works fine without having to supply show=True. What you did in the second piece of code also works because you are not indexing into the axis.
