Skip to content

Doc: Adding annotated heatmap example - #11017

Merged
jklymak merged 1 commit into
matplotlib:masterfrom
ImportanceOfBeingErnest:doc-heatmap-example
Apr 12, 2018
Merged

jklymak merged 1 commit into
matplotlib:masterfrom
ImportanceOfBeingErnest:doc-heatmap-example

Conversation

@ImportanceOfBeingErnest

@ImportanceOfBeingErnest ImportanceOfBeingErnest commented Apr 10, 2018 •

Copy link
Copy Markdown
Member

PR Summary

PR #10956 suggested to provide a function to draw annotated categorical heatmaps in matplotlib. The discussion to this concluded that such a function is not necessary given that those are easily produced from imshow plots. It would however be useful to have an example in the gallery showing how to do this.

This PR creates such example.

PR Checklist

  • This is an example
  • Code is PEP 8 compliant
  • Documentation is sphinx and numpydoc compliant

@ImportanceOfBeingErnest
ImportanceOfBeingErnest force-pushed the doc-heatmap-example branch 3 times, most recently from 0e012a4 to e0b81c4 Compare April 10, 2018 14:27
@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member Author

@jklymak

jklymak commented Apr 10, 2018

Copy link
Copy Markdown
Member

Wow! Very nice!

I’m personally not opposed to adding a method like this to the core axes class so long as the API is thought out. I think these sorts of plots get used a fair bit in some fields.

@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member Author

The line

# sphinx_gallery_thumbnail_number = 2

renders like this:

image

Does anyone have an idea how to set the thumbnail number without it appearing in the output?


I'm not convinced that this should be a built-in function. This is just one possible implementation. I think it's better to show users how to adjust stuff to their liking instead of providing a closed function and let them fight with all the possible arguments etc.

# which defines the data to color code. We then also need two lists or arrays
# of categories; of course the number of elements in those lists
# need to match the data along the respective axes.
# The heatmap itself is an :meth:`imshow <matplotlib.axes.Axes.imshow>` plot

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

`~.Axes.imshow`

Will give you the same thing I am 98% sure as this long string (including the :meth:)

# As discussed in the :ref:`Coding styles <coding_styles>`
# one might want to reuse such code to create some kind of heatmap
# for different input data and/or on different axes.
# We might hence create a function which takes the data and the row and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We might hence" is pretty verbose. -> "we create a..."

Usually only use "which" after a comma. "which"->"that"

Suggest: "We create a function that takes the data and the row and column labels as input, and allows arguments that are used to customize the plot"

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Some comments in the text that you can take or leave as they are just english suggestions.

@QuLogic QuLogic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some typos. A nice set of examples, otherwise.

Creating annotated heatmaps
===========================

It is often desireable to show data which depends on two independent

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

desirable

===========================

It is often desireable to show data which depends on two independent
variables as a colorcoded image plot. This is often referred to as a

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

color coded

heatmap. If the data is categorical, this would be called a categorical
heatmap.
Matplotlib's :meth:`imshow <matplotlib.axes.Axes.imshow>` function makes
production of such plot particularly easy.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"such a plot" / "such plots"

#
# Here, in addition to the above we also want to create a colorbar and
# position the labels above of the heatmap instead of below it.
# The annotations shall

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Early line break.

data = im.get_array()

# Normalize the threshold to the images color range.
if not threshold and threshold != 0.0:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if threshold is not None: seems to be the same intent?

texts = []
for i in range(data.shape[0]):
for j in range(data.shape[1]):
kw.update(dict(color=textcolors[im.norm(data[i, j]) > threshold]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kw['color'] = ...; no need for the extra dict.

# Replicate the above example with a different font size and colormap.

im, _ = heatmap(harvest, vegetables, farmers, ax=ax,
cmap="Wistia", cbarlabel="harvest [t/year]")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Misaligned.

@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member Author

Thanks @jklymak and @QuLogic for your input. That is very useful.

I'm still stuggeling with where to put the line # sphinx_gallery_thumbnail_number = 2. I tried several places by now; in the current version it would just appear as text:

image

although it does take effect and the thumbnail image is indeed the second one.

@jklymak

jklymak commented Apr 11, 2018

Copy link
Copy Markdown
Member

I’d just have it as a comment up in the import section. Yes a naive user may include it but it doesn’t hurt anything.

@ImportanceOfBeingErnest

Copy link
Copy Markdown
Member Author

Ok, that's what I did now. Apart, it looks all good, doesn't it?

@jklymak jklymak added this to the v2.2-doc milestone Apr 12, 2018
@jklymak
jklymak merged commit 2825fe2 into matplotlib:master Apr 12, 2018
jklymak added a commit that referenced this pull request Apr 12, 2018
@ImportanceOfBeingErnest
ImportanceOfBeingErnest deleted the doc-heatmap-example branch April 14, 2018 17:51
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

Successfully merging this pull request may close these issues.

3 participants