-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Transforms of BoundingBox objects wrong (when backend uses dpi != 72) #16641
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
Comments
One possible workaround (for the pdf backend) is to explicitly set the dpi of the figure, i.e., put this line of code directly after the figure creation with the
So, this workaround confirms that the Note that this has nothing to do with the keyword argument Note further that changing the |
Thanks for the clear and detailed report. |
I think the real problem here is that you cannot get the extention of a text without rendering it first; but rendering requires a DPI, even for backends that are pixel-independent in their output. A workaround which avoids the dpi coming out of sync would be to use the
|
I'm not sure what you're trying to do here, but if this is strictly what you're looking for (and not just some simplified case), a simpler workaround is to use the import matplotlib as mpl
mpl.use("PDF") # noqa
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots()
ax.plot([0, 1], [1, 0], "bo", ms=20)
text = ax.text(x=0.3, y=0.1, s="sample text", fontsize=20, family="Serif",
bbox={"fill": False, "lw": 0.2, "color": "r"})
fig.savefig("bbox2.pdf") The default text = ax.text(x=0.3, y=0.1, s="sample text", fontsize=20, family="Serif",
bbox={"fill": False, "lw": 0.2, "color": "r",
"boxstyle": "square,pad=0"}) |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Bug report
Bug summary
When transforming BoundingBox objects their width and height are calculated wrong. This happens, supposedly, for backends that have a different dpi setting from 72. The issue is present, e.g., for the backends "svg", "pdf", "cairo" (pdf, ps, svg). It is not present, e.g., for the backends "ps", "png".
Code for reproduction
Actual outcome
PS output
PDF output
Expected outcome
I expect that the BoundingBoxes for both used backends have the same width and height in Axes coordinates. However, this is not the case. The BoundingBox in the PDF version can be transformed to the correct size via multiplication of its width and height by the factor 100 / 72. Therefore, I assume that the default dpi of the backends plays a role here and is not handled properly.
Matplotlib version
Matplotlib was installed via conda.
Possibly related issues
The text was updated successfully, but these errors were encountered: