2

I'm struggling with this strange behavior in the matplotlib pyplot library.

The actual plot is more complex but I've reduced it to these few lines of python code:

from matplotlib import pyplot as plt
import numpy as np

def plot(w, h):

    num_samples = int(w * h)
    x = w * np.random.rand(num_samples)
    y = h * np.random.rand(num_samples)
    plt.figure(figsize=(w, h))
    plt.xticks(range(w))
    plt.xlim(0, w)
    plt.ylim(0, h)
    plt.scatter(x, y)
    plt.grid()

It basically creates a scatter plot given width and height parameters.

If I invoke this function like plot(5, 3), it will produce this figure: short plot

I can use the function with greater width values, generating long plots like plot(100, 3): long plot

But if I use it with a greater width it creates a "broken plot" plot(450, 3): longer plot

Note that

  1. the lines of the containing frame are kind of dashed?
  2. there are scatter dots outside the containing frame. broken plot

why? Is it a bug in the library?

6
  • This might be a bug that should be reported through the GitHub issues page. Commented Apr 25, 2024 at 15:51
  • Before reporting, verify this is an issue on the newest version of the library. Commented Apr 25, 2024 at 15:53
  • Ok! I've tested the same code in google colab and the problem is present. So if colab has the last version of the library I think it is not related to the version. Commented Apr 25, 2024 at 16:41
  • thanks @jared for your advice, I've just added the issue to the official library repository. github.com/matplotlib/matplotlib/issues/28137 Commented Apr 25, 2024 at 17:10
  • Depending on the backend used to run matplotlib, plt.figure(figsize=(450, 3)) would be much too large, especially when running in interactive mode. See e.g. Setting figure size to be larger than screen size in matplotlib. In this post, one of matplotlib maintainers mentions a maximum figsize of 50 inches. Commented Apr 25, 2024 at 17:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.