Repository navigation
Doc: Adding annotated heatmap example - #11017
Conversation
0e012a4 to
e0b81c4
Compare
|
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. |
|
The line renders like this: 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 |
There was a problem hiding this comment.
`~.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 |
There was a problem hiding this comment.
"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
left a comment
There was a problem hiding this comment.
This is great. Some comments in the text that you can take or leave as they are just english suggestions.
QuLogic
left a comment
There was a problem hiding this comment.
Some typos. A nice set of examples, otherwise.
| Creating annotated heatmaps | ||
| =========================== | ||
|
|
||
| It is often desireable to show data which depends on two independent |
| =========================== | ||
|
|
||
| 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 |
| 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. |
| # | ||
| # 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 |
| data = im.get_array() | ||
|
|
||
| # Normalize the threshold to the images color range. | ||
| if not threshold and threshold != 0.0: |
There was a problem hiding this comment.
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])) |
There was a problem hiding this comment.
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]") |
e0b81c4 to
da9aee0
Compare
|
Thanks @jklymak and @QuLogic for your input. That is very useful. I'm still stuggeling with where to put the line although it does take effect and the thumbnail image is indeed the second one. |
|
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. |
da9aee0 to
c8b6231
Compare
|
Ok, that's what I did now. Apart, it looks all good, doesn't it? |
Backport PR #11017 on branch v2.2.2-doc


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
imshowplots. It would however be useful to have an example in the gallery showing how to do this.This PR creates such example.
PR Checklist