<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">I'm fairly new to matplotlib and I was trying to create a confusion matrix following the tutorial here: <a href="https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html" rel="nofollow">https://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html</a>. I downloaded the example code and when I ran it, the plots looked slightly cropped on the top and bottom edges.</p>
<p dir="auto"><strong>Code for reproduction</strong></p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="print(__doc__)

import numpy as np
import matplotlib.pyplot as plt

from sklearn import svm, datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.utils.multiclass import unique_labels

# import some data to play with
iris = datasets.load_iris()
X = iris.data
y = iris.target
class_names = iris.target_names

# Split the data into a training set and a test set
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Run classifier, using a model that is too regularized (C too low) to see
# the impact on the results
classifier = svm.SVC(kernel='linear', C=0.01)
y_pred = classifier.fit(X_train, y_train).predict(X_test)


def plot_confusion_matrix(y_true, y_pred, classes,
                          normalize=False,
                          title=None,
                          cmap=plt.cm.Blues):
    &quot;&quot;&quot;
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    &quot;&quot;&quot;
    if not title:
        if normalize:
            title = 'Normalized confusion matrix'
        else:
            title = 'Confusion matrix, without normalization'

    # Compute confusion matrix
    cm = confusion_matrix(y_true, y_pred)
    # Only use the labels that appear in the data
    classes = classes[unique_labels(y_true, y_pred)]
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print(&quot;Normalized confusion matrix&quot;)
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    fig, ax = plt.subplots()
    im = ax.imshow(cm, interpolation='nearest', cmap=cmap)
    ax.figure.colorbar(im, ax=ax)
    # We want to show all ticks...
    ax.set(xticks=np.arange(cm.shape[1]),
           yticks=np.arange(cm.shape[0]),
           # ... and label them with the respective list entries
           xticklabels=classes, yticklabels=classes,
           title=title,
           ylabel='True label',
           xlabel='Predicted label')

    # Rotate the tick labels and set their alignment.
    plt.setp(ax.get_xticklabels(), rotation=45, ha=&quot;right&quot;,
             rotation_mode=&quot;anchor&quot;)

    # Loop over data dimensions and create text annotations.
    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha=&quot;center&quot;, va=&quot;center&quot;,
                    color=&quot;white&quot; if cm[i, j] &gt; thresh else &quot;black&quot;)
    fig.tight_layout()
    return ax


np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plot_confusion_matrix(y_test, y_pred, classes=class_names,
                      title='Confusion matrix, without normalization')

# Plot normalized confusion matrix
plot_confusion_matrix(y_test, y_pred, classes=class_names, normalize=True,
                      title='Normalized confusion matrix')

plt.show()"><pre class="notranslate"><span class="pl-en">print</span>(<span class="pl-s1">__doc__</span>)

<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>.<span class="pl-s1">pyplot</span> <span class="pl-k">as</span> <span class="pl-s1">plt</span>

<span class="pl-k">from</span> <span class="pl-s1">sklearn</span> <span class="pl-k">import</span> <span class="pl-s1">svm</span>, <span class="pl-s1">datasets</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">model_selection</span> <span class="pl-k">import</span> <span class="pl-s1">train_test_split</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">metrics</span> <span class="pl-k">import</span> <span class="pl-s1">confusion_matrix</span>
<span class="pl-k">from</span> <span class="pl-s1">sklearn</span>.<span class="pl-s1">utils</span>.<span class="pl-s1">multiclass</span> <span class="pl-k">import</span> <span class="pl-s1">unique_labels</span>

<span class="pl-c"># import some data to play with</span>
<span class="pl-s1">iris</span> <span class="pl-c1">=</span> <span class="pl-s1">datasets</span>.<span class="pl-en">load_iris</span>()
<span class="pl-v">X</span> <span class="pl-c1">=</span> <span class="pl-s1">iris</span>.<span class="pl-s1">data</span>
<span class="pl-s1">y</span> <span class="pl-c1">=</span> <span class="pl-s1">iris</span>.<span class="pl-s1">target</span>
<span class="pl-s1">class_names</span> <span class="pl-c1">=</span> <span class="pl-s1">iris</span>.<span class="pl-s1">target_names</span>

<span class="pl-c"># Split the data into a training set and a test set</span>
<span class="pl-v">X_train</span>, <span class="pl-v">X_test</span>, <span class="pl-s1">y_train</span>, <span class="pl-s1">y_test</span> <span class="pl-c1">=</span> <span class="pl-en">train_test_split</span>(<span class="pl-v">X</span>, <span class="pl-s1">y</span>, <span class="pl-s1">random_state</span><span class="pl-c1">=</span><span class="pl-c1">0</span>)

<span class="pl-c"># Run classifier, using a model that is too regularized (C too low) to see</span>
<span class="pl-c"># the impact on the results</span>
<span class="pl-s1">classifier</span> <span class="pl-c1">=</span> <span class="pl-s1">svm</span>.<span class="pl-v">SVC</span>(<span class="pl-s1">kernel</span><span class="pl-c1">=</span><span class="pl-s">'linear'</span>, <span class="pl-v">C</span><span class="pl-c1">=</span><span class="pl-c1">0.01</span>)
<span class="pl-s1">y_pred</span> <span class="pl-c1">=</span> <span class="pl-s1">classifier</span>.<span class="pl-en">fit</span>(<span class="pl-v">X_train</span>, <span class="pl-s1">y_train</span>).<span class="pl-en">predict</span>(<span class="pl-v">X_test</span>)


<span class="pl-k">def</span> <span class="pl-en">plot_confusion_matrix</span>(<span class="pl-s1">y_true</span>, <span class="pl-s1">y_pred</span>, <span class="pl-s1">classes</span>,
                          <span class="pl-s1">normalize</span><span class="pl-c1">=</span><span class="pl-c1">False</span>,
                          <span class="pl-s1">title</span><span class="pl-c1">=</span><span class="pl-c1">None</span>,
                          <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">plt</span>.<span class="pl-s1">cm</span>.<span class="pl-v">Blues</span>):
    <span class="pl-s">"""</span>
<span class="pl-s">    This function prints and plots the confusion matrix.</span>
<span class="pl-s">    Normalization can be applied by setting `normalize=True`.</span>
<span class="pl-s">    """</span>
    <span class="pl-k">if</span> <span class="pl-c1">not</span> <span class="pl-s1">title</span>:
        <span class="pl-k">if</span> <span class="pl-s1">normalize</span>:
            <span class="pl-s1">title</span> <span class="pl-c1">=</span> <span class="pl-s">'Normalized confusion matrix'</span>
        <span class="pl-k">else</span>:
            <span class="pl-s1">title</span> <span class="pl-c1">=</span> <span class="pl-s">'Confusion matrix, without normalization'</span>

    <span class="pl-c"># Compute confusion matrix</span>
    <span class="pl-s1">cm</span> <span class="pl-c1">=</span> <span class="pl-en">confusion_matrix</span>(<span class="pl-s1">y_true</span>, <span class="pl-s1">y_pred</span>)
    <span class="pl-c"># Only use the labels that appear in the data</span>
    <span class="pl-s1">classes</span> <span class="pl-c1">=</span> <span class="pl-s1">classes</span>[<span class="pl-en">unique_labels</span>(<span class="pl-s1">y_true</span>, <span class="pl-s1">y_pred</span>)]
    <span class="pl-k">if</span> <span class="pl-s1">normalize</span>:
        <span class="pl-s1">cm</span> <span class="pl-c1">=</span> <span class="pl-s1">cm</span>.<span class="pl-en">astype</span>(<span class="pl-s">'float'</span>) <span class="pl-c1">/</span> <span class="pl-s1">cm</span>.<span class="pl-en">sum</span>(<span class="pl-s1">axis</span><span class="pl-c1">=</span><span class="pl-c1">1</span>)[:, <span class="pl-s1">np</span>.<span class="pl-s1">newaxis</span>]
        <span class="pl-en">print</span>(<span class="pl-s">"Normalized confusion matrix"</span>)
    <span class="pl-k">else</span>:
        <span class="pl-en">print</span>(<span class="pl-s">'Confusion matrix, without normalization'</span>)

    <span class="pl-en">print</span>(<span class="pl-s1">cm</span>)

    <span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-s1">plt</span>.<span class="pl-en">subplots</span>()
    <span class="pl-s1">im</span> <span class="pl-c1">=</span> <span class="pl-s1">ax</span>.<span class="pl-en">imshow</span>(<span class="pl-s1">cm</span>, <span class="pl-s1">interpolation</span><span class="pl-c1">=</span><span class="pl-s">'nearest'</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s1">cmap</span>)
    <span class="pl-s1">ax</span>.<span class="pl-s1">figure</span>.<span class="pl-en">colorbar</span>(<span class="pl-s1">im</span>, <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>)
    <span class="pl-c"># We want to show all ticks...</span>
    <span class="pl-s1">ax</span>.<span class="pl-en">set</span>(<span class="pl-s1">xticks</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-s1">cm</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>]),
           <span class="pl-s1">yticks</span><span class="pl-c1">=</span><span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-s1">cm</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">0</span>]),
           <span class="pl-c"># ... and label them with the respective list entries</span>
           <span class="pl-s1">xticklabels</span><span class="pl-c1">=</span><span class="pl-s1">classes</span>, <span class="pl-s1">yticklabels</span><span class="pl-c1">=</span><span class="pl-s1">classes</span>,
           <span class="pl-s1">title</span><span class="pl-c1">=</span><span class="pl-s1">title</span>,
           <span class="pl-s1">ylabel</span><span class="pl-c1">=</span><span class="pl-s">'True label'</span>,
           <span class="pl-s1">xlabel</span><span class="pl-c1">=</span><span class="pl-s">'Predicted label'</span>)

    <span class="pl-c"># Rotate the tick labels and set their alignment.</span>
    <span class="pl-s1">plt</span>.<span class="pl-en">setp</span>(<span class="pl-s1">ax</span>.<span class="pl-en">get_xticklabels</span>(), <span class="pl-s1">rotation</span><span class="pl-c1">=</span><span class="pl-c1">45</span>, <span class="pl-s1">ha</span><span class="pl-c1">=</span><span class="pl-s">"right"</span>,
             <span class="pl-s1">rotation_mode</span><span class="pl-c1">=</span><span class="pl-s">"anchor"</span>)

    <span class="pl-c"># Loop over data dimensions and create text annotations.</span>
    <span class="pl-s1">fmt</span> <span class="pl-c1">=</span> <span class="pl-s">'.2f'</span> <span class="pl-k">if</span> <span class="pl-s1">normalize</span> <span class="pl-k">else</span> <span class="pl-s">'d'</span>
    <span class="pl-s1">thresh</span> <span class="pl-c1">=</span> <span class="pl-s1">cm</span>.<span class="pl-en">max</span>() <span class="pl-c1">/</span> <span class="pl-c1">2.</span>
    <span class="pl-k">for</span> <span class="pl-s1">i</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">cm</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">0</span>]):
        <span class="pl-k">for</span> <span class="pl-s1">j</span> <span class="pl-c1">in</span> <span class="pl-en">range</span>(<span class="pl-s1">cm</span>.<span class="pl-s1">shape</span>[<span class="pl-c1">1</span>]):
            <span class="pl-s1">ax</span>.<span class="pl-en">text</span>(<span class="pl-s1">j</span>, <span class="pl-s1">i</span>, <span class="pl-en">format</span>(<span class="pl-s1">cm</span>[<span class="pl-s1">i</span>, <span class="pl-s1">j</span>], <span class="pl-s1">fmt</span>),
                    <span class="pl-s1">ha</span><span class="pl-c1">=</span><span class="pl-s">"center"</span>, <span class="pl-s1">va</span><span class="pl-c1">=</span><span class="pl-s">"center"</span>,
                    <span class="pl-s1">color</span><span class="pl-c1">=</span><span class="pl-s">"white"</span> <span class="pl-k">if</span> <span class="pl-s1">cm</span>[<span class="pl-s1">i</span>, <span class="pl-s1">j</span>] <span class="pl-c1">&gt;</span> <span class="pl-s1">thresh</span> <span class="pl-k">else</span> <span class="pl-s">"black"</span>)
    <span class="pl-s1">fig</span>.<span class="pl-en">tight_layout</span>()
    <span class="pl-k">return</span> <span class="pl-s1">ax</span>


<span class="pl-s1">np</span>.<span class="pl-en">set_printoptions</span>(<span class="pl-s1">precision</span><span class="pl-c1">=</span><span class="pl-c1">2</span>)

<span class="pl-c"># Plot non-normalized confusion matrix</span>
<span class="pl-en">plot_confusion_matrix</span>(<span class="pl-s1">y_test</span>, <span class="pl-s1">y_pred</span>, <span class="pl-s1">classes</span><span class="pl-c1">=</span><span class="pl-s1">class_names</span>,
                      <span class="pl-s1">title</span><span class="pl-c1">=</span><span class="pl-s">'Confusion matrix, without normalization'</span>)

<span class="pl-c"># Plot normalized confusion matrix</span>
<span class="pl-en">plot_confusion_matrix</span>(<span class="pl-s1">y_test</span>, <span class="pl-s1">y_pred</span>, <span class="pl-s1">classes</span><span class="pl-c1">=</span><span class="pl-s1">class_names</span>, <span class="pl-s1">normalize</span><span class="pl-c1">=</span><span class="pl-c1">True</span>,
                      <span class="pl-s1">title</span><span class="pl-c1">=</span><span class="pl-s">'Normalized confusion matrix'</span>)

<span class="pl-s1">plt</span>.<span class="pl-en">show</span>()</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/56123251/66134690-b2f1ff00-e5c6-11e9-9c34-f96644eaf220.png"><img src="https://user-images.githubusercontent.com/56123251/66134690-b2f1ff00-e5c6-11e9-9c34-f96644eaf220.png" alt="Screenshot from 2019-10-03 10-12-56" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Expected outcome</strong></p>
<p dir="auto">The plots were expected to not look cropped.</p>
<p dir="auto"><strong>Matplotlib version</strong></p>
<ul dir="auto">
<li>Operating system: Ubunto 18.04</li>
<li>Matplotlib version: 3.1.1</li>
<li>Matplotlib backend (<code class="notranslate">print(matplotlib.get_backend())</code>):</li>
<li>Python version: 3.7</li>
<li>Jupyter version (if applicable):</li>
<li>Other libraries:</li>
</ul>
<p dir="auto">matplotlib installed via pip.</p>
<h3 dir="auto">Bug report</h3>
<p dir="auto"><strong>Bug summary</strong></p>
<p dir="auto">The very top and bottom of the heatmaps are getting truncated to 1/2 height in version <code class="notranslate">3.1.1</code>. This does not happen for version <code class="notranslate">3.0.3</code>.</p>
<p dir="auto">This is the code from a Jupyter Notebook</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import matplotlib
import pandas as pd
import numpy as np
import seaborn as sb
%pylab inline

print(matplotlib.__version__)
print(sb.__version__)

grid = pd.DataFrame(np.arange(9).reshape((3,3))/10)
fig, ax = subplots(1, 1, figsize=(5, 5))

sb.heatmap(grid, annot=True, fmt=&quot;.0f&quot;, ax=ax, cmap='RdBu',  vmin=0, vmax=1, cbar=True);"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">matplotlib</span>
<span class="pl-k">import</span> <span class="pl-s1">pandas</span> <span class="pl-k">as</span> <span class="pl-s1">pd</span>
<span class="pl-k">import</span> <span class="pl-s1">numpy</span> <span class="pl-k">as</span> <span class="pl-s1">np</span>
<span class="pl-k">import</span> <span class="pl-s1">seaborn</span> <span class="pl-k">as</span> <span class="pl-s1">sb</span>
<span class="pl-c1">%</span><span class="pl-s1">pylab</span> <span class="pl-s1">inline</span>

<span class="pl-en">print</span>(<span class="pl-s1">matplotlib</span>.<span class="pl-s1">__version__</span>)
<span class="pl-en">print</span>(<span class="pl-s1">sb</span>.<span class="pl-s1">__version__</span>)

<span class="pl-s1">grid</span> <span class="pl-c1">=</span> <span class="pl-s1">pd</span>.<span class="pl-v">DataFrame</span>(<span class="pl-s1">np</span>.<span class="pl-en">arange</span>(<span class="pl-c1">9</span>).<span class="pl-en">reshape</span>((<span class="pl-c1">3</span>,<span class="pl-c1">3</span>))<span class="pl-c1">/</span><span class="pl-c1">10</span>)
<span class="pl-s1">fig</span>, <span class="pl-s1">ax</span> <span class="pl-c1">=</span> <span class="pl-en">subplots</span>(<span class="pl-c1">1</span>, <span class="pl-c1">1</span>, <span class="pl-s1">figsize</span><span class="pl-c1">=</span>(<span class="pl-c1">5</span>, <span class="pl-c1">5</span>))

<span class="pl-s1">sb</span>.<span class="pl-en">heatmap</span>(<span class="pl-s1">grid</span>, <span class="pl-s1">annot</span><span class="pl-c1">=</span><span class="pl-c1">True</span>, <span class="pl-s1">fmt</span><span class="pl-c1">=</span><span class="pl-s">".0f"</span>, <span class="pl-s1">ax</span><span class="pl-c1">=</span><span class="pl-s1">ax</span>, <span class="pl-s1">cmap</span><span class="pl-c1">=</span><span class="pl-s">'RdBu'</span>,  <span class="pl-s1">vmin</span><span class="pl-c1">=</span><span class="pl-c1">0</span>, <span class="pl-s1">vmax</span><span class="pl-c1">=</span><span class="pl-c1">1</span>, <span class="pl-s1">cbar</span><span class="pl-c1">=</span><span class="pl-c1">True</span>);</pre></div>
<p dir="auto"><strong>Actual outcome</strong></p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Populating the interactive namespace from numpy and matplotlib
3.1.1
0.9.0"><pre class="notranslate"><code class="notranslate">Populating the interactive namespace from numpy and matplotlib
3.1.1
0.9.0
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/1696066/60530345-87f5c780-9cc6-11e9-9e31-a251283f3f7e.png"><img src="https://user-images.githubusercontent.com/1696066/60530345-87f5c780-9cc6-11e9-9e31-a251283f3f7e.png" alt="download" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Matplotlib version</strong></p>

<ul dir="auto">
<li>Operating system: Ubuntu Linux</li>
<li>Matplotlib version: 3.1.1</li>
<li>Matplotlib backend: module://ipykernel.pylab.backend_inline</li>
<li>Python version: 3.6</li>
<li>Jupyter version (if applicable): 4.4.0</li>
<li>Other libraries:</li>
</ul>