-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Alignment of labels incorrect if fontsize<1pt PDF backend #9963
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
I can confirm this behaviour. Maybe some sort of roundoff issue w/ the PDF backend? But... those figure sizes are pretty pathological. |
The issue is with fontsize < 1. which it was in all your examples. In the PDF backend. Hard to get very worked up about cases where people want to plot fonts that are .35 mm high. Intellectually unsatisfying, but the easy solution is to make the figure bigger so the fonts can be larger than 1 pt. import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
a = np.random.rand(50,50)
fig, axs = plt.subplots(1,3, figsize=(1.4,0.7))
for nn, i in enumerate([0.5, 0.9, 1.1]):
ax = axs[nn]
ax.imshow(a)
ax.set_yticklabels(["",1,10,100, "ABCDEFGH", "X"])
ax.set_xticklabels(["",1,10,100, "long long label", "short"])
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.tick_params(axis='both', which='both', labelsize=i)
ax.tick_params(axis='x',which='both', rotation=90)
plt.savefig("fig.pdf")
plt.savefig("fig.png")
plt.show() |
Here is the PDF... fig.pdf |
Is this a problem of the pdf rendering, not interpreting the positions correctly, or a problem of the pdf backend not setting those positions correctly? I don't think the example is that pathological. You might want to create an image plot which has all points labeled and distribute it via pdf, such that the viewer may zoom into the pdf and see the labels, but that plot still having a defined size in inches (i.e. not to create a poster-size figure). |
FWIW mplcairo renders this correctly :-) |
If you print, then it’s fine to render as a png. If you are looking at it on a screen then the figure size is arbitrary. Yes this would be a nice bug to fix. I’m just not sure how high a priority it’ll get. I have no idea if it’s a basic PDF limitation. It renders incorrectly in both Preview and Acrobat. It would be interesting to see if it happens in EPS and SVG. |
I don't think we need to discuss the fact that it is often desired to create a pdf of predefined size. So I guess either it is possible to fix the issue or not, independent on whether everyone understands the use case of it. It seems SVG is created just fine. So a possible workaround could be to convert svg to pdf outside of matplotlib. However I haven't found a way to do that (Inkscape seems to rasterize the pdf). @anntzer 's suggestion to use cairo fails for me. The following image is the output from cairo |
mpl-cairo is a separate package that @anntzer is working on to replace agg.
It is fixing a *lot* of bugs.
…On Sat, Dec 9, 2017 at 12:59 PM, Importance of Being Ernest < ***@***.***> wrote:
I don't think we need to discuss the fact that it is often desired to
create a pdf of predefined size. So I guess either it is possible to fix
the issue or not, independent on whether everyone understands the use case
of it.
It seems SVG is created just fine. So a possible workaround could be to
convert svg to pdf outside of matplotlib. However I haven't found a way to
do that (Inkscape seems to rasterize the pdf).
@anntzer <https://github.com/anntzer> 's suggestion to use cairo fails
for me. The following image is the output from cairo
matplotlib.use("Cairo") with cairocffi-0.8 installed.
[image: image]
<https://user-images.githubusercontent.com/23121882/33798030-1b65d7c4-dd12-11e7-954b-1a28c052b5f9.png>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#9963 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARy-CjjdrJ6erMVtkGbrvlP7uxT1I2aks5s-spngaJpZM4Q7x26>
.
|
I tracked this all the way down into
|
@anntzer Does mplcairo output in PDF? I thought it was just an Agg replacement, but if it also puts all the vector outputs under one umbrella that seems very useful. Edit: should have read the README. Answer = Yes.... Is there a roadmap to put this into the main repo at some point? |
See these notes from FreeType's
|
cairo natively outputs to raster/pdf/ps/svg so mplcairo is actually a replacement for all these backends (modulo a few features, I believe the only missing things are: URLs in svg (may possibly happen in the next cairo release, apparently), and use of the core 14 fonts just from the metrics and without embedding in pdf). The plan to put this in the main repo is
|
TBH, I don't yet know how this affects things, especially if other file formats work (I didn't check too carefully)? Or how it's possible we render at <1pt either. |
yeah, it’s a bit mysterious. Particularly as Agg works just fine. Thanks for the pointer, I’m sure this is the problem, somehow... |
Err wait, if you increase DPI on the PNG so that you can see the text, it turns out that it never changes size below 1pt. It does change size in PDF because we embed the fonts themselves and let the viewer render it, but the size we get to allocate space is always for 1pt. |
Oh, great, well at least the world is consistent. So do we try and work around this, or tell people who want to use unreadable fonts to readjust their expectations? |
Well, I guess the question is how correct is mplcairo and how difficult it is to do whatever it is that it does to get it to work. |
OK, I can't reproduce the error in PNG: EDIT: Oh, duh. I see. In PNG the font sizes never drop below 1 pt... a = np.random.rand(50,50)
fig, axs = plt.subplots(1,3, figsize=(1.4,0.7), dpi=5000)
for nn, i in enumerate([0.5, 0.9, 1.1]):
ax = axs[nn]
ax.imshow(a)
ax.set_yticklabels(["",1,10,100, "ABCDEFGH", "X"])
ax.set_xticklabels(["",1,10,100, "long long label", "short"])
ax.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax.yaxis.set_major_locator(ticker.MultipleLocator(10))
ax.tick_params(axis='both', which='both', labelsize=i)
ax.tick_params(axis='x',which='both', rotation=90)
plt.savefig("fig.pdf")
plt.savefig("fig.png", dpi=5000)
plt.show() resulting png: |
Actually for mplcairo+pdf too the size plateaus at 1pt, not going lower. As noted by @QuLogic it's likely running into the same FreeType limitation. I'm just calling cairo_scaled_font_text_to_glyphs, which does all the layouting: https://www.cairographics.org/manual/cairo-cairo-scaled-font-t.html#cairo-scaled-font-text-to-glyphs / https://github.com/anntzer/mplcairo/blob/master/src/_util.cpp#L537 Interestingly the path (compile-time option) using libraqm (that handles right-to-left / complex layout languages) also has an issue with tiny fonts, but a different ones: the spaces between the glyphs are too big, although the texts are right-aligned correctly. |
So is there a path to fix here? Throw a |
But why? At the moment you can use smaller font sizes. (As seen from this comment above) |
You can't use smaller fontsizes in the Agg or Cairo backends, and you can't position them properly in the PDF backend. |
I guess I have to rethink what people mean by "scalable" when it comes to vectorgraphics. |
@ImportanceOfBeingErnest Any other options you had would be welcome. You could go the other route of trying to scale all the reported font dimensions by the <1 fontsize, but then you'd break the rendering for all the Agg/Cairo backends. That seems worse to me than restricting the scalability of the fontsize. The "proper" solution is to get FreeType to change their routine. I am not sure why they don't allow small fontsizes. |
Probably because font scaling is more complicated that just dilating or shrinking the whole thing (even for "scalable" fonts). See e.g. https://docs.google.com/document/d/1wpzgGMqXgit6FBVaO76epnnFC_rQPdVKswrDQWyqO1M/edit for a pretty good read (IMO). |
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! |
I think this is essentially a limitation of freetype we're unlikely to work around. |
Label padding around the axes depends on the length of the label when figure is saved as pdf.
The below code creates a figure and adds labels of different length. Those labels are not well aligned in the saved pdf version. Instead their padding towards the axes is depending on how many characters the label has. This issue is the more pronounced the larger the dpi (at constant figsize*dpi product).
The png files saved do not show this unexpected behaviour; all labels are aligned correctly. The expected output when saving the pdf file is of course the same as the png.
[reproduced with python 2.7, matplotlib 2.1, Qt4Agg backend as well as TkAgg backend]
This issue was initially brought up in this Stackoverflow question.
The text was updated successfully, but these errors were encountered: