Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practices with plot_kde #1157

Closed
rpgoldman opened this issue Apr 20, 2020 · 2 comments
Closed

Best practices with plot_kde #1157

rpgoldman opened this issue Apr 20, 2020 · 2 comments

Comments

@rpgoldman
Copy link
Contributor

rpgoldman commented Apr 20, 2020

Short Description

I was trying to redo some of the plot_kde plots for the "rat tumor" example of Chapter 5 BDA3, building on the notebooks in the PyMC3 resources repo. I had a couple of problems, that may be more documentation issues than anything else:

  1. It seems that one must label the x and y axes in a separate call -- there doesn't seem to be any way to specify the x and y axis labels in the call to plot_kde. This seems normal enough to want to do that it should be supported. Perhaps it's supported by one of the keywords arguments, but I couldn't figure out how and the examples in the docs weren't sufficient to figure it out.

  2. I tried to use the contour_kwds argument to set the number of contours, but that didn't seem to work. Again, an example would have helped.

I had two less specific issues, that may be related:

  1. In general I couldn't easily figure out how to use the extra keyword arguments, and I couldn't find examples.

  2. The ArviZ plots I did ended up looking significantly uglier than the Seaborn plots that were in the old version of the notebook. I could use some help figuring out how to style the ArviZ plots.

Code Example or link

From the diagram, here is the seaborn contour plot:

image

and here is the best ArviZ equivalent I could come up with. It's not very impressive:

image

The seaborn code is as follows:

fig, ax = plt.subplots(figsize=(8, 7))
sns.kdeplot(np.log(alpha_trunc /  beta_trunc), np.log(alpha_trunc + beta_trunc),
            cmap=plt.cm.viridis, n_levels=11, ax=ax)
# ax.set_xlim(1.5, 2.1)
ax.set_ylim(1.7, 3.75)
ax.set_xlabel('log(alpha / beta)')
ax.set_ylabel('log(alpha + beta)');

and here's what I came up with as an ArviZ alternative:

fig, ax = plt.subplots(figsize=(8, 7))

az.plot_kde(values2=idata_10.posterior['log(alpha + beta)'], values=idata_10.posterior['log(alpha / beta)'], ax=ax,
                contour=True, 
                contour_kwargs={'levels': 10,}) # Setting different levels here does not have any effect.
    
# ax.set_xlim(1.5, 2.1)
ax.set_ylim(1.7, 3.75)
ax.set_xlabel('log(α / β)')
_ = ax.set_ylabel('log(α + β)')

I'm not sure why it looks so bad. Possibly seaborn's style sheet is messing up the ArviZ plot but... I don't know how styling works with ArviZ. There's an example in the gallery that uses az.style, but az.style does not appear in the online API docs page.

I cargo-culted in az.style.use("arviz-darkgrid") from the ArviZ gallery, but it didn't change my grayscale contour plot.

For full details of what I was trying to do, see my fork of BDA3.

ArviZ version: master at 8f1677cfd985b2b553dc52571943a830adc29d1c

@aloctavodia
Copy link
Contributor

aloctavodia commented Apr 20, 2020

Hi @rpgoldman , thanks for the report.

The grayscale is weird because Matplotlib's default cmap is viridis, I guess you change it somewhere and thus you are seeing a gray scale.

Below there are a few examples of customizing the plots. Probably we should add a few to the gallery and/or docstring.

az.style.use("arviz-darkgrid")
data = {"a":np.random.normal(0, 1, size=1000), "b":np.random.normal(0, 2, size=1000)}
az.plot_kde(values2=data["a"], values=data["b"], 
                    contour=True, fill_last=False) 

00

az.plot_kde(values2=data["a"], values=data["b"],
                contour=True, fill_last=False, contour_kwargs={"alpha":0})

01

az.plot_kde(values2=data["a"], values=data["b"],
            contour=True, fill_last=False, 
            contour_kwargs={"colors":None, "cmap":plt.cm.viridis},
            contourf_kwargs={"alpha":0});

02

az.plot_kde(values2=data["a"], values=data["b"],
            contour=True, fill_last=False, 
            contour_kwargs={"colors":None,"cmap":plt.cm.viridis,"levels":2},
            contourf_kwargs={"alpha":0.5, "levels":2});

03

@canyon289
Copy link
Member

@rpgoldman is this closed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants