Domanda

If I save a figure with matplotlib, sometimes some of the characters, like endash, ű énd ő, are in a lower position than the others. Not just in the legend, but in labels and title as well. I tried it on Ubuntu 11.10 and 12.04 with matplotlib 1.0.1 and 1.1.1~rc1. The source of the file is encoded in utf-8, and the Python version is 2.7.2+. Here is the source:

import matplotlib.pyplot as plt
from numpy import sin, linspace, pi
x = linspace(0, 20, 1000)
plt.plot(x, sin(x), label=u"Hurrá, őrtűz! Szűztűz.")
plt.plot(x, sin(x), label=u"xxxxá, őx–x–xűx! Sxűxxűx.")
plt.plot(x, sin(x+pi/2), label=u"B–A őrűr ()")
plt.plot(x, sin(x-pi/2), label=u"B–A őrűr")
plt.plot(x, sin(x+pi), label=u"xãxâxőxŐxűxŰx–x endash")
plt.plot(x, sin(x+.1), label=u"őrtűz !")
plt.plot(x, sin(x+.2), label=u"őr tűz! (")
plt.plot(x, sin(x+pi+.2), label=u"őrült tűzlány ãxâ")
plt.title(u"matplotlib version 1.0.1, source: utf-8, Ubuntu 11.10")
plt.legend()
plt.savefig("tmp_accent.pdf")
plt.savefig("tmp_accent.eps")

And here is the part of the figure I got. (I viewed with evince, and with acroread).

The part of the PDF file I got (with evince, but with acroread is the same)

I am not good at fonts but I have set the font.sans-serif in the matplotlibrc with the fonts Bitstream Vera Sans, Lucida Grande, Verdana, Geneva, Lucid, Arial, Helvetica, Avant Garde one by one.

I can make a better version from the eps version, with the epspdf command line tool, but this method is platform dependent. Is there a better way? The problem is exists in newer matplotlib version or in newer Ubuntu?

È stato utile?

Soluzione

It seems as though there are various problems rendering unicode, especially to pdf files. I see the same problem that you do using a slightly different matplotlib version (see below). It doesn't happen if I switch to a different matplotlib backend, or alternatively, if you have latex installed, you can let latex do the text rendering (see http://matplotlib.org/users/usetex.html). On Ubuntu I believe you need the texlive-latex-extra package installed if you don't have it already.

Latex:

The latex method works the best, but it is much slower and of course introduces a large external dependency. I added the following three lines to the top of your file:

import matplotlib
matplotlib.rcParams['text.usetex']=True
matplotlib.rcParams['text.latex.unicode']=True

See the result below -- looks pretty good but you'd probably want to do something about the axis tick labels.

Cairo

Alternatively, you can use the the GTKCairo backend. I added two lines to the top of your code:

import matplotlib
matplotlib.use("GTKCairo")

You can see in the image below that it isn't perfect -- the legend text is aligned properly, but unfortunately it now strays outside the legend box. I don't really know why that happens but a possible workaround is to change the line plt.legend() to plt.legend(borderpad=2), which gives the final image below. There's now too much whitespace but at least the text fits. You might be able to try different legend options to get something that looks better.

The images

Note that they seem to look much worse here than in the original pdf files. I just uploaded the pdf files directly to imgur which seems to convert them to fairly low resolution images.

Originally:

TKAGG backend

With latex: Latex text rendering

A close-up screenshot of the latex pdf:

enter image description here

With GTKCairo backend:

GTKCairo

With GTKCairo and borderpad=2

GTKCairo with borderpad

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top