Skip to content
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

No transparency in *.pgf file when using pgf Backend. #5867

Closed
overdetermined opened this issue Jan 17, 2016 · 14 comments
Closed

No transparency in *.pgf file when using pgf Backend. #5867

overdetermined opened this issue Jan 17, 2016 · 14 comments

Comments

@overdetermined
Copy link
Contributor

I seem to run into this problem when using the *.pgf backend. Both the pdf and png files show the transparency correctly, but the *.pgf file does not.

@jenshnielsen
Copy link
Member

CC @pwuertz

Can you provide a minimal example that triggers the issue

@overdetermined
Copy link
Contributor Author

Hi the following code produces transparency in the *.pdf, but when i compile the *.pgf using pdflatex this transparency is gone... (there is a possibility i am using latex wrong, but the *.pgf file contains no sign of transparencies...)

import numpy as np
import matplotlib as mpl

mpl.use('pgf')

import matplotlib.pyplot as plt

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    "pgf.preamble": [
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
        r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
        ]
    }
mpl.rcParams.update(pgf_with_latex)

def savefig(filename):
    plt.savefig('{}.pgf'.format(filename), transparent=True)
    plt.savefig('{}.pdf'.format(filename))
    plt.savefig('{}.png'.format(filename))


x = np.linspace(0,10, num = 10)
y1 = 1*x
y2 = 0.2*x + 4
plt.fill_between(x,0,y1,color='red',alpha=0.5)
plt.fill_between(x,0,y2,color='blue',alpha=0.5)
#plt.show()


savefig("test")

@jenshnielsen
Copy link
Member

Thanks, I edited your example to correctly render the code

@overdetermined
Copy link
Contributor Author

You were faster than me ;-) i had to look up how to format it...

@pwuertz
Copy link
Contributor

pwuertz commented Jan 17, 2016

If the PDF/PNG files generated by the backend are correct it is safe to assume that the generated PGF code and your TeX installation are fine.

When you compile your TeX document and the pgf code, are you using some kind of IDE with an integrated PDF viewer that might have issues rendering transparent objects? Other then that I guess there are always ways to break latex by combining various modules in the preamble. You could try compiling a minimal document just to be sure.

Btw, according to the documentation transparent=True only makes the background transparent, it shouldnt affect transparency within the figure in any way.
Oh and there is one odd thing: When you are using raw strings python still interprets "\u" as unicode control sequence and not generally as backslash-u.

@overdetermined
Copy link
Contributor Author

You sir are correct. I was using texstudio on windows and the internal PDF viewer did not show the transparency... What I find very odd is that when i include the *.pdf it would render correctly even in texstudio.

But thank you very much for the help. This one took me a long time to figure out. I apologize that this turned out not to be a problem with matplotlib afterall.

@WeatherGod
Copy link
Member

Not a problem. We all learned something new. Closing.

@overdetermined
Copy link
Contributor Author

Ummm. Sorry to do this, but I must have made a mistake when viewing that one pdf. It still seems to me that in all pdf documents I have opened the file, transparency does not come through. This issue should be reopened... :-(

@jenshnielsen jenshnielsen reopened this Jan 19, 2016
@pwuertz
Copy link
Contributor

pwuertz commented Jan 19, 2016

If you dump the .pgf file somewhere I'll have a look at it. First I'd like to confirm that the problem is not the pgf code generated by matplotlib.

@overdetermined
Copy link
Contributor Author

Hi Pwuertz,

i pasted the contents of the pgf created by the testscript here:
http://pastebin.com/vibamyRj

I can't read it very well myself, but find no hint of transparency in it...

@pwuertz
Copy link
Contributor

pwuertz commented Jan 19, 2016

The pgf commands \pgfsetstrokeopacity and \pgfsetfillopacity, which you can find within the code, are responsible for transparency. Compiling your code with latex gives me a PDF with transparent fills as expected. I'm closing this issue again since it is not a matplotlib problem, but we can still try to figure out whats wrong.
Did you try compiling a minimal tex document like this? It the PDF created from this document correct? (EDIT) Which pgf version is reported on your system?

\documentclass{article}
\usepackage{pgf}
\begin{document}
My pgf version is: \pgfversion\\
\scalebox{.5}{\input{test.pgf}}
\end{document}

@pwuertz pwuertz closed this as completed Jan 19, 2016
@overdetermined
Copy link
Contributor Author

Yes. In the minimal latex document transparency is fine. It even shows up in texstudio, so it must be one of the other packages which i am loading... there are many so it will take me a while to pin down which one it is. PGF version is 3.0

@overdetermined
Copy link
Contributor Author

You're not going to like this... apparently I was using a package named transparent... (god knows for what)
commenting out the line
%\usepackage{transparent}
does the magic trick.

The great help that i have receives has prompted me to make a small donation to matplotlib, so I guess at least some good as come of this.

Thanks for the great support.

@pwuertz
Copy link
Contributor

pwuertz commented Jan 19, 2016

Oh well, latex can be a real mess when it comes to interoperability of modules. Or understanding what a module actually does :). But it's good to know that the fix for your problem was rather simple in the end.
Thanks for your support too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants