Skip to content

Legend positioning behavior change from matplotlib {2.1.0 -> 2.2.2} #11126

Closed
@deepak18

Description

@deepak18

Bug report

Bug summary
The behavior of positioning a legend outside the plot by using the figure transformation(fig.transFigure) has changed, by shortening the size of plot and leaving too much space between plot and legend.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

# Fixing random state for reproducibility
np.random.seed(19680801)

N = 50
x = 15*np.random.rand(N)
y = 15*np.random.rand(N)
colors = np.random.rand(N)

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

ax.scatter(x, y, c=colors, alpha=0.5, label='Scatter plot')
ax.plot([0, 15],[0, 15], color='r', linestyle='-', linewidth=2.5, label = 'Straight line')

ax.legend(bbox_to_anchor=(1, 0), loc="upper right",
           bbox_transform=fig.transFigure, columnspacing=0.5, ncol=2)

fig.tight_layout()
plt.savefig(_image_name_, bbox_inches="tight")

Actual outcome
output_2 2 2

Expected outcome
Notice the empty space between plot and the legend. This was not the case with Matplotlib version: 2.1.0
Below are the plots for the same stuff in both the versions:
Matplotlib 2.1.0:
matplotlib_2 1 0

Matplotb 2.2.2:
matplotlib_2 2 2

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: '2.2.2'
  • Matplotlib backend: Qt5Agg or module://ipykernel.pylab.backend_inline
  • Python version: Python 3.6.5 |Anaconda custom (64-bit)
  • Jupyter Notebook version: 6.3.1

I have the Python installed through Anaconda and I spotted the above change after I updated my packages through Anaconda.
image

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions