Skip to content

Default legend behavior (loc='best') very slow for large amounts of data. #12120

Closed
@kbrose

Description

@kbrose

Bug report

Plotting large amounts of data with legend is slow when legend is using best location.

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

# Setup, and create the data to plot
y = np.random.rand(10000000)
# Adapted from https://matplotlib.org/tutorials/introductory/usage.html#performance
mpl.rcParams['path.simplify'] = True
mpl.rcParams['path.simplify_threshold'] = 1.0

plt.plot(y, label='data')
plt.legend(loc='best')
plt.show()

plt.plot(y, label='data')
plt.legend(loc='upper right')
plt.show()

Actual outcome

The first plot is much slower (loc='best') than the second.

Matplotlib version

  • Operating system: Ubuntu 18.04
  • Matplotlib version: 2.1.x and 2.2.x
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg, TkAgg
  • Python version: 3.7
  • Jupyter version (if applicable): N/A
  • Other libraries: numpy 1.15.1

Installed via conda.


I think it'd be fine to just note this in the performance section of the User Guide (https://matplotlib.org/tutorials/introductory/usage.html#performance). I could also try looking into speeding up the best location finder but I get the feeling that'd be a fairly slow project.

Curious to hear what others think.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions