Skip to content

Legends cut off in Jupyter Notebooks when plotting with %matplotlib notebook command #9163

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

Closed
mtd91429 opened this issue Sep 6, 2017 · 3 comments
Milestone

Comments

@mtd91429
Copy link

mtd91429 commented Sep 6, 2017

Bug report

When using matplotlib in a Jupyter Notebook with the magic %matplotlib notebook, legends placed outside of the axis are cut off.

Code for reproduction

import matplotlib.pyplot as plt
# probably unnecessary/overkill, but reset all parameters to default
plt.rcParams.update(plt.rcParamsDefault)
%matplotlib notebook

fig, ax = plt.subplots(ncols=1, nrows=1)
ax.plot(range(10), label="Plot 1")
ax.plot(range(10, 0, -1), label="Plot 2")
ax.legend(loc='center right', bbox_to_anchor=(1.20, 0.5))

Actual outcome

image

Expected outcome

The legend would be fully displayed as in the following example. This plot obtained by switching to %matplotlib inline
image

Matplotlib version

  • Operating System: Windows 7, x64
  • Matplotlib Version: 2.0.2
  • Python Version: 3.6.2
  • Jupyter Server: 5.0.0
  • Other Libraries:

Matplotlib and python were installed using Anaconda

Additional Information
This seems similar to #2530

@jklymak
Copy link
Member

jklymak commented Sep 7, 2017

The legend is cut off if you do fig.savefig('Test.pdf') as well.

%matplotlib inline is just letting you see off the edge of the figure, whereas %matplotlib notebook is giving you a more what-you-see-is-what-you-get view. If you want yout legend over there, you can make the axis smaller (fig.subplots_adjust=0.85).

An automatic soultion could be offered by fig.tight_layout(), but tight_layout() doesn't take into account legends (yet. see #9130).

@jklymak
Copy link
Member

jklymak commented Sep 7, 2017

OK, I made a pull request: #9164

@timhoffm
Copy link
Member

Closed by #9164.

@QuLogic QuLogic added this to the v2.2.0 milestone Jul 16, 2020
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