Skip to content

[Bug]: matplotlib pyplot creates a broken plot when long #28137

Closed as not planned

Description

@estebanuri

Bug summary

Very long plots are broken
As explained here

The plot has a broken containing frame.

Code for reproduction

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()

plot(450, 3)

Actual outcome

The plot has a broken containing frame.

image

Note that

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

Expected outcome

A correct plot,
for example if the function is invoked with smaller width the result is correct.

plot(5, 3)

image

Additional information

No response

Operating system

OS/X, Ubuntu

Matplotlib Version

3.7.1

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.10.12

Jupyter version

6.5.5

Installation

pip

Activity

  1. ksunden commented on Apr 25, 2024

    @ksunden
    Member

    This is a fundamental limitation of agg (the renderer for the most commonly used backends)

    Assuming you have the default dpi (100), your long dimension is 450*100 = 45,000 pixels.

    This is actually under the limit that (at one point) we thought was relevant (2**16=65,536) though there is some thought that the limit may actually be in RGB(A) subpixels, rather than full pixels, which would lower the limit by at least a factor of 3. (45k is also greater than the 2**15=33k which I have also seen stated as the limit... I did not go digging into the code to figure out which one is the actual limit here)

    See also:

    (Probably among others, those are just the ones I found most readily.)

  2. estebanuri commented on Apr 26, 2024

    @estebanuri
    Author

    Thanks @ksunden for your reply.

    So are you referring to this module of the library?
    Do you know where exactly is this limitation defined?

    Can you figure out if there is any workaround to plot these kind of large plots?

    Thank you in advance

  3. jklymak commented on Apr 26, 2024

    @jklymak
    Member

    It's probably possible to rewrite agg to allow larger images, but someone would have to put in the work

    As for work arounds, it would be helpful to specify why you need a figure that is 12 m (42 feet) long. The reason this never gets fixed is because such a huge figure is usually not desired.

  4. estebanuri commented on Apr 26, 2024

    @estebanuri
    Author

    To give a little more context on the problem, I am plotting events on a timeline. It is somewhat similar to the problem they had in this other issue with the audio spectrogram.

    The idea is to create an image with a good resolution to be able to zoom in on the screen and analyze the events that occurred at different timestamps.

    This problem could be solved with other tools, but I'd like to do it with pyplot because it allows you to export the plot to an image that is portable and that anyone can analyze the results using an image viewer.

  5. jklymak commented on Apr 26, 2024

    @jklymak
    Member

    I don't see how the user is to find the timestamps to zoom into. The fonts in the displayed image will be unreadable. People could zoom and pan around but it's not going to be efficient.

    If I were doing this I'd use Matplotlib directly and allow the zoom tool to reveal detail (while updating the axes limits) or make a GUI that uses the prebuilt images. But if you do the latter, you can easily paste together multiple images in your GUI.

  6. estebanuri commented on Apr 29, 2024

    @estebanuri
    Author

    @jklymak here I provide an extract of a real plot.
    x axis represents the time, y axis represents speed in km/h of a vehicle.
    image
    Note that the displayed fonts are perfectly readable, that represents hour minutes and seconds, measured in local time.

    Sure, I could create a software approach by drawing the results by tiles, this will require to create a website or to bundle the results with a software to be installed (what I can't / wouldn't want to do). I usually send the zipped image by email, so anyone can analyze it using a standard image viewer.

  7. rcomer commented on Apr 29, 2024

    @rcomer
    Member

    What format do you send the images in? I tried saving a png from the example in the OP. It seemed to save OK but my image viewer (Eye of Gnome on Ubuntu) threw an error saying it could not display images that large.

  8. estebanuri commented on Apr 29, 2024

    @estebanuri
    Author

    @rcomer I save the images in .png.
    I'd have to test it on linux. Currently I'm using Mac, with the default image preview application.
    I have no problem visualizing with that, but I'd guess that other open source applications for image manipulation shouldn't have any problem to handle these kind of images.

  9. jklymak commented on Apr 29, 2024

    @jklymak
    Member

    The fonts are readable because you have zoomed in. If you look at the whole image at once it will not be readable.

    Again this is a limitation of Matplotlib Agg renderer. I don't think we would object to someone doing the work to relax this constraint. However it is not currently a high priority because it is usually not desired.

  10. scottshambaugh commented on Apr 29, 2024

    @scottshambaugh
    Contributor

    Closing as a duplicate as #26368, which has some more discussion in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions