Skip to content
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

[Bug]: Log scaled barplots in PDF result in infinite rectangles #28175

Open
eabila opened this issue May 6, 2024 · 3 comments
Open

[Bug]: Log scaled barplots in PDF result in infinite rectangles #28175

eabila opened this issue May 6, 2024 · 3 comments

Comments

@eabila
Copy link

eabila commented May 6, 2024

Bug summary

When saving log scaled barplots as PDFs, the bars get saved as rectangles with infinite size.
Log_scale_barplot

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

categories = ['A', 'B', 'C', 'D', 'E']
values = [800, 1200, 2000, 3000, 5000]

plt.bar(categories, values)
plt.yscale('log')
plt.savefig('Log_scale_barplot.pdf')

Actual outcome

Rectangles with infinite size

Expected outcome

Rectangles size capped at axis

Additional information

No response

Operating system

Linux, OS/X

Matplotlib Version

3.8.4

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

@oscargus
Copy link
Contributor

oscargus commented May 6, 2024

Which tool are you using? I imported the plot in Inkscape and in neither of the import options (poppler/internal) it happens.

Next step is to actually check the PDF content, but that will have to wait.

(I also tried SVG to try to get some idea of where the problem happens, and the SVG also looked OK.)

@eabila
Copy link
Author

eabila commented May 6, 2024

Sorry, I forgot to mention this: I am using Adobe Illustrator here.

@ksunden
Copy link
Member

ksunden commented May 7, 2024

As far as I can tell, it isn't actually rendering incorrectly, correct?

A bar plot on a log scale is an odd thing because there is an implied zero, and zero on log scale cannot exist. You CAN set a baseline e.g. plt.bar(..., bottom=1) which will set it to something that is bounded on the axis. Note, however, that adding bottom explicitly will change the automatic limits to include the bottom value.

I actually can see the path extending when I load the PDF into inkscape using the internal importer. The generated groups/top level thing that you click on is clipped, but if you go down to the thing labeled pathXX (the leaf of the object tree) it does show as extending beyond. The SVG directly created from MPL requires a little more finesse to see, but it does actually have a large (but finite) stop value (~300k pts) if you look into what is actually stored in the path in the xml, just the clip path is composed with it at a low level, so even in inkscape you don't see it as extending unless you know where to look (I was able to see it in the XML editor). The PDF imported path is quite close (though there does seem to be a difference in what is considered the origin here, so it is actually -300k pts, rather than +300k pts).

That said, it is correct as far as I'm concerned, and absent additional context as to why it is problematic (e.g. a common pdf program actually rendering it incorrectly or something that is explicitly out of PDF spec... here it seems to display fine, it is only in the editor that you see it is extended, which is the correct fundamental bar for what was plotted.), I'm inclined to say it is fine. A bar graph (by default) is from zero to a number, and zero on a log scale is at -inf... and it draws correctly.

I do not see a particular reason we should be clipping paths when the vector formats we support already have clipping provided (and we use it, and it renders correctly). I believe we do something for lines where we remove some points, but that is about not bloating file size with points that are outside of the frame (IIRC, we clip to the Figure, not the axes to make sure we don't miss anything). There is no such file size concern for these paths, though.

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

No branches or pull requests

3 participants