leandro leandro - 1 year ago 109
Python Question

Font rendering in PDF when using "annotate" in Python/Matplotlib

I am plotting a series of graphs using matplotlib (in Linux Mint 18 - python 2.7.12), using the code below. The problem is, however, that when generating the PDF file, the fonts associated with the "annotate" directive (plt.annotate) are very badly rendered. I could not find the problem. Does anyone has a clue?

The resulting picture is this:
PDF file

(I expect that you see that the 0%, 10% ... 50% annotations have low resolution). Other plots with annotations did not have the same problem.
If I zoom into the picture, I see that these annotations are rendered as vector, but somehow they are rendered differently from the other fonts of the plot.

font = {'weight' : 'normal',
'size' : 8}
matplotlib.rc('font', **font)

concentrations = ["0","10","20","30","40","50"]
subplot = [ 321, 322, 323, 324, 325, 326 ]
runs = ["01","02","03","04","05","06","07","08","09","10"]
ic = -1
for c in concentrations :
ic = ic + 1
for run in runs :
x, y = np.loadtxt(run+".dat",usecols=(0,1),comments="#",unpack=True)
plt.plot(x, y, 'b-', linewidth=1.0)
plt.annotate(c+"%", xy=(0.03, 0.02),**font)


Answer Source

It looks different, because you print these labels (XX%) several times (for each run).

Reducing the indentation of the annotate command (so it will only run once per each subplot) will fix the problem

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download