Matplotlib uses broken pdfmarks in latex - matplotlib

I have a really weird problem:
plt.xlabel("$t$[ms]")
plt.ylabel("$\dot{q}$[deg]")
plt.axvline(x=span2Stop,lw='0.3',c='0.5')
plt.axvspan(spanStart, spanStop, facecolor='0.9', alpha=1,edgecolor='0.9',lw=0)
plt.annotate('11ms',fontsize='9', xy=(11, -500),xytext=(2, -500),verticalalignment='center',arrowprops=myarrow)
plt.annotate('13ms',fontsize='9', xy=(13, 1000),xytext=(2, 1000),verticalalignment='center',arrowprops=myarrow)
plt.annotate('17ms',fontsize='9', xy=(17, -500),xytext=(20, -500),verticalalignment='center',arrowprops=myarrow)
plt.tight_layout()
plt.savefig(imagePath + "snapAHVjoints.pdf")
plt.savefig(imagePath + "snapAHVjoints.svg")
plt.show()
generates a nice looking picture. But plt.axvline(x=span2Stop,lw='0.3',c='0.5') causes a broken pdfmark in the exported pdf if included in latex as a pdf. I spent ages to find that.
Compiled with xelatex to pdf. The problem does not seem to be related to the loaded packages (to be honest I didn't try to to disable hyperref but tested almost all options I found).
Any Idea what's wrong with that?

lw='0.3' should be lw=0.3 Nevertheless, no broken pdf should be exported. If you export it as eps matplotlib complains, so I recognized it that way
Is that a bug or am I again getting wrong soem pythonic things?

Related

Using matplotlib with latex mode with non-default fonts

I am using Windows 10 with Anaconda and Spyder 4. When using matplotlib, I would like to use the font Proxima Nova and render with LaTeX.
If in my matplotlibrc file I specify
font.family : Proxima Nova
then the figure renders with the font Proxima Nova. This means that the font is installed on my system (as it is) and matplotlib can use it. However, if in the matplotlibrc file I also specify
text.usetex: True
then, even though I have specified Proxima Nova as the font, the figure renders in the default LaTeX font, which I guess is Computer Modern.
I have tried
matplotlib.font_manager._rebuild()
In the source code file and also have tried specifying the fonts in the source code file and not in the matplotlibrc file. However I always get the same result. I have also followed all the advice on this help page, including making sure that latex, dvipng and ghostscript are all the PATH variable. However nothing seems to work.
I would like to note that I can use Proxima Nova separately when compiling Latex documents, so that should not be an issue either.
How can I get matplotlib to be able to use a non-default font and render with LateX at the same time?
After some further investigation, I was able to get to use Proxima Nova with Latex, although there are still some outstanding issues.
The main issue is that if the font Proxima Nova is used with Latex, one needs to use Lualatex and not plain Latex. Here is the Matplotlib instruction on using matplotlib with Lualatex.
The key to getting things to work was this post.
At the very beginning of my .py file, I have the following code:
import matplotlib as mpl
mpl.use("pgf")
mpl.rcParams.update({
'font.family': 'sans-serif',
'text.usetex': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'lualatex',
'pgf.preamble': r'\usepackage{fontspec} \setmainfont{Proxima Nova}',
})
The code above should be placed at the very top of the code, above any other imports.
The problem, however is that this solution works only after performing the following steps:
Delete the .matplotlib/tex.cache folder and restart spyder
Replace 'font.family': 'sans-serif' and \setmainfont{Proxima Nova} with 'font.family': 'serif' and \setmainfont{Times New Roman} respectively. Run python once.
Revert back to 'font.family': 'sans-serif' and
\setmainfont{Proxima Nova} and run python again.
The output with the correct font is produced.
Unless the above 4 steps are performed, the output is compiled with the default DejaVu Sans font and not with Proxima Nova. I am not sure why...
After getting help on the matplotlib github forum, I was pointed to the following solution:
mpl.rcParams.update({
'font.family': 'sans-serif',
'text.usetex': True,
'pgf.rcfonts': False,
'pgf.texsystem': 'lualatex',
'pgf.preamble': r'\usepackage{fontspec} \setsansfont{Proxima Nova}',
})
In other words you need to use setsansfont in stead of setmainfont. You can see the matplotlib forum page here.

Use lualatex in mathplotlib without pgf backend

basically the title is the question:
I would like to use lualatex for all the text handling in a matplotlib plot without using the pgf backend.
I need fontenc package and customized fonts to have identical fonts in plots and in my latex documents but do not want to use the pgf backend.
Is there a hidden option somewhere?
The context is the following: I have a mycommands.sty file where all my defind \newcommand for math are stored. I use some specific fonts for, e. g., \mathscr{p}, which is not possible (small letter) without the fontenc package.
Now I want to use these custom commands in different places (legend, labels, title, ...) in the plot and have them work and look exactly the same as in the document I write and compile with lualatex.
The only point why it is not possible is that matplotlib internally uses pdflatex for the compilation which gives me errors when using fontenc and therefore some of my commands do not work.
Thanks.

Matplotlib seeing new font but not applying it, xkcd mode

I'm trying to get Matplotlib's xkcd style to use the font designed for it, Humor Sans.
I'm getting Comic Sans instead, see pic
Now, the point is about the correct font not getting recognised here, I have removed the cache as per here and as suggested in the same question. Basically what is suggested in this other question.
I have even "forced" Matplotlib to regenerate the font cache as
f = matplotlib.font_manager.FontManager()
and looks like the desired font is there:
f.findfont("Humor Sans")
'/Users/myname/Library/Fonts/Humor-Sans.ttf'
So I don't get why the plot still shows Comic Sans? It has been obtained with code
plt.xkcd()
plt.plot([i for i in range(10)], np.sin([i for i in range(10)]))
plt.title('A sine wave')
plt.show()
I even tried changing backend as per this Q&A, to no avail.
The solution was to also clear the cached font_manager instance, as
rm ~/.matplotlib/fontList.py3k.cache

cefpython3 (cefwx example2) iCCP: known incorrect sRGB profile

How do i fix or ignore this warning popup? The example works fine, but this warning popup keeps showing. I looked around for a solution and image magic seems to be the fix. However, there are no images causing this problem in the sample code. I'm assuming it is what is being loaded by the browser. Can someone help me solve this problem please?
I can confirm this issue with wx.version=3.0.2.0 msw (classic) and cefpython 31.2. The navigation bar in this example loads three buttons (back, forward, reload) from this directory: c:\Python27\Lib\site-packages\cefpython3\wx\images:
Arrow Left.png
Arrow Right.png
Button Load.png
These cause the "iCCP: known incorrect sRGB profile" error.
Solution: In the c:\Python27\Lib\site-packages\cefpython3\examples\wx\ directory there are three images (back.png, forward.png, reload_page.png). Replace the problematic images with these by renaming them (back.png to Arrow Left.png etc) and copying to the c:\Python27\Lib\site-packages\cefpython3\wx\images directory.
Created Issue #221 to fix this.
If using wx 2.9/3.x you might also encounter text input issue. The cefpython 31.2 release was tested with wx 2.8 only.

pdfbox Font TrueType cause stripper.getText() error

I have some pdf need to extract to text
only one pdf is truetype
and it cause following error :
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.fontbox.ttf.TrueTypeFont.getAdvanceWidth(I)I
at org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.getFontWidth(PDTrueTypeFont.java:677)
at org.apache.pdfbox.pdmodel.font.PDSimpleFont.getFontWidth(PDSimpleFont.java:231)
at org.apache.pdfbox.util.PDFStreamEngine.processEncodedText(PDFStreamEngine.java:411)
at org.apache.pdfbox.util.operator.ShowText.process(ShowText.java:45)
at org.apache.pdfbox.util.PDFStreamEngine.processOperator(PDFStreamEngine.java:557)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:268)
at org.apache.pdfbox.util.PDFStreamEngine.processSubStream(PDFStreamEngine.java:235)
at org.apache.pdfbox.util.PDFStreamEngine.processStream(PDFStreamEngine.java:215)
at org.apache.pdfbox.util.PDFTextStripper.processPage(PDFTextStripper.java:460)
at org.apache.pdfbox.util.PDFTextStripper.processPages(PDFTextStripper.java:385)
at org.apache.pdfbox.util.PDFTextStripper.writeText(PDFTextStripper.java:344)
at org.apache.pdfbox.util.PDFTextStripper.getText(PDFTextStripper.java:257)
I check the api source code and still can't get through it.
Is there something I can do?
or the 2.0 version solve this problem.
It's not an big issue but some tiny mistake
Thanks to mkl and Tilman Hausherr
It's only buildpath problem.
First , I have muti versions of fontbox that I didn't aware of.
Just check it again and check it hard !
If others have same error, check the dependencies
pdfbox-1.x.x.jar
fontbox-1.x.x.jar
they need to be the same!