3

So I am trying something: I want to have a loop reading out the position of circles from a dat file. The plotting already works very well. However now I want to implement a gradual shading from semi to totally transparent. The semi transparent part in the middle i want to be able to set the opacity of. Using a predefined value in a .dat file.

I found these both links. However the way I am drawing does not really allow the functions of the second link.

TikZ: radial shading of a ring

Is it possible to make a gradient from 'opaque color' to 'total transparency'? gray should be white

Is there a way to accompany for this without loosing transparency?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{forloop}
\usepackage{pgfplotstable}
\usepackage{readarray}
\usepackage{ifthen}
\usepackage[left=4cm, right=10cm, top=2cm]{geometry}
\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}
name    test    .dat
opacity:    10      percent
X   Y   Rad
0   0   0.75
0   1.8 0.75
0   3.6 0.75
0   5.4 0.75
0.75    0   0.75
0.75    1.8 0.75
0.75    3.6 0.75
0.75    5.4 0.75
1.5 0   0.75
1.5 1.8 0.75
1.5 3.6 0.75
1.5 5.4 0.75
2.25    0   0.75
2.25    1.8 0.75
2.25    3.6 0.75
2.25    5.4 0.75
3   0   0.75
3   1.8 0.75
3   3.6 0.75
3   5.4 0.75
3.75    0   0.75
3.75    1.8 0.75
3.75    3.6 0.75
3.75    5.4 0.75
4.5 0   0.75
4.5 1.8 0.75
4.5 3.6 0.75
4.5 5.4 0.75
5.25    0   0.75
5.25    1.8 0.75
5.25    3.6 0.75
5.25    5.4 0.75
6   0   0.75
6   1.8 0.75
6   3.6 0.75
6   5.4 0.75
\end{filecontents*}

\pgfplotsset{compat=1.16}

\begin{document}

\xdef \name {test.dat}                        %% 
\readdef{\name}{\data}
\readarray\data\MyDat[-,39, 3]

\section*{\MyDat[1,1,2] }

\centering
\begin{tikzpicture}[square/.style={regular polygon,regular polygon sides=4}]

\def \opacity {\MyDat[1,2,2]/10*0.15}
   \foreach \row in {4,...,39}{

                       \def\x{\MyDat[1,\row,1]}
                       \def\y{\MyDat[1,\row,2]}
                       \def\radius{\MyDat[1,\row,3]}

                 %\fill[red,opacity= \opacity] (\x,\y)circle(\radius cm);
                 %%%% This does not work:
             \fill[even odd rule, inner color= red, outer color= red!5, ,opacity= \opacity,] (\x,\y)circle(\radius cm);
}
\end{tikzpicture}
\end{document}
ole
  • 33
  • Welcome to TeX-SE! Please provide one complete compilable example, not only fragments. In its current form it is not clear how the data gets read and used. Also you set \def \lineopacity {\MyDat[1,2,2]/12.7*0} once and for all. How will there be a gradient? –  Jul 04 '19 at 14:22
  • sorry I though that was enough – ole Jul 04 '19 at 14:27
  • lineopacity was a way for me to make the circumference of the circle visible. The gradient is something i want to implement. You can already see a gradient, but where there is no read it is not transparent but gray – ole Jul 04 '19 at 14:29
  • i think now it should be compilable – ole Jul 04 '19 at 14:34
  • Yes, it is, thanks! But it is not clear. Do you want to vary the transparency of the circles, i.e. each circle has a given transparency, or something along the lines of https://tex.stackexchange.com/questions/264384/is-it-possible-to-make-a-gradient-from-opaque-color-to-total-transparency, where the transparency varies continuously? –  Jul 04 '19 at 14:36
  • I want to have the same setting for all circles --> to have a gradient from semi transparent red in the center (defined in file) to 100% transparent on the rim. In the best case i would be able to set the gradient... – ole Jul 04 '19 at 14:39
  • This does not answer my question. Should each circle have a constant transparency or should the transparency vary also over the circles? –  Jul 04 '19 at 14:40
  • I want to have the same setting for all circles, meaning every cicle should be identical. In transparency and gradient – ole Jul 04 '19 at 14:45

2 Answers2

3

As explained in this nice answer, you can define your custom radial fading by first defining a radial shading and employing it for the fading. This allows you to have a nonconstant gradient. It is used here to create an approximate Gaussian fading. To this end, the value of a Gaussian (stored in the function mygauss) is evaluated at four points. This defines the transparency at these points. You can change the gradient by adjusting the function.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{fadings}
\usepackage{filecontents}
\begin{filecontents*}{test.dat}
name    test    .dat
opacity:    10      percent
X   Y   Rad
0   0   0.75
0   1.8 0.75
0   3.6 0.75
0   5.4 0.75
0.75    0   0.75
0.75    1.8 0.75
0.75    3.6 0.75
0.75    5.4 0.75
1.5 0   0.75
1.5 1.8 0.75
1.5 3.6 0.75
1.5 5.4 0.75
2.25    0   0.75
2.25    1.8 0.75
2.25    3.6 0.75
2.25    5.4 0.75
3   0   0.75
3   1.8 0.75
3   3.6 0.75
3   5.4 0.75
3.75    0   0.75
3.75    1.8 0.75
3.75    3.6 0.75
3.75    5.4 0.75
4.5 0   0.75
4.5 1.8 0.75
4.5 3.6 0.75
4.5 5.4 0.75
5.25    0   0.75
5.25    1.8 0.75
5.25    3.6 0.75
5.25    5.4 0.75
6   0   0.75
6   1.8 0.75
6   3.6 0.75
6   5.4 0.75
\end{filecontents*}
\usepackage{readarray}
% from https://tex.stackexchange.com/a/82444/121799



\begin{document}
\xdef\name{test.dat}                        %% 
\readdef{\name}{\data}
\readarray\data\MyDat[-,39, 3]

\section*{\MyDat[1,1,2]}
\tikzset{declare function={mygauss(\x)=100-100*exp(-5*\x*\x);}}%
\pgfmathtruncatemacro{\myvalA}{mygauss(0)}%
\pgfmathtruncatemacro{\myvalB}{mygauss(0.25)}%
\pgfmathtruncatemacro{\myvalC}{mygauss(0.5)}%
\pgfmathtruncatemacro{\myvalD}{mygauss(0.75)}%
%\typeout{\myvalA,\myvalB,\myvalC,\myvalD}%
\pgfdeclareradialshading{myring}{\pgfpointorigin}%
{%
color(0cm)=(transparent!\myvalA);%
color(2.5mm)=(transparent!\myvalB);%
color(5mm)=(transparent!\myvalC);%
color(7.5mm)=(transparent!\myvalD)%
}%
\pgfdeclarefading{ringo}{\pgfuseshading{myring}}%

\centering
\begin{tikzpicture}[]

\def\opacity{\MyDat[1,2,2]/12.7*0.15}
\def\lineopacity {\MyDat[1,2,2]/12.7*0}
\foreach \row in {4,...,39}{
\def\x{\MyDat[1,\row,1]}
\def\y{\MyDat[1,\row,2]}
\def\radius{\MyDat[1,\row,3]}
\path[fill=red,path fading=ringo] (\x,\y)circle[radius=\radius cm];  
}
\end{tikzpicture}
\end{document}

enter image description here

  • yes i also just figured it out... Thats kinda what i am looking for, however, now i need to adjust the gradient. The first link had some solution with adding a second circle – ole Jul 04 '19 at 15:05
  • Thanks alot, i think i will just keep it this way. Everything else might be an overkill... – ole Jul 04 '19 at 15:14
  • I am very sorry i seem to have difficulties to explain myself... – ole Jul 04 '19 at 15:14
  • i ment that i wanted to define how quick the transparency goes to 0, and setting that it should arrive at 0 at the defined radius... but i think it might not be nessecary. I appreciate your patience ( ͡° ͜ʖ ͡°) – ole Jul 04 '19 at 15:16
  • @ole You can adjust the percentage of the fadings and patch them together with even odd rule. Is that what you want? Can you tell me which transparency level at which radius you would like to have? –  Jul 04 '19 at 15:20
  • for example: a gaussian distribution, or a linear, or a bump funtion – ole Jul 04 '19 at 15:39
  • @ole You can achieve this with point meta that comes with pgfplots. It won't be fading then, but just a color gradient. But this color gradient can be specified in terms of a function, such as a Gaussian. Or you declare a shading with several points as in my updated answer. You can add more points to have something that comes close to a Gaussian. –  Jul 04 '19 at 15:42
  • @ole I added an example that adds a fading that is approximately Gaussian. –  Jul 04 '19 at 16:02
  • thanks alot!! This is amazing – ole Jul 04 '19 at 16:02
  • @ole You're welcome! If this answer works for you, could you perhaps consider accepting it by clicking at the check mark left of it? –  Jul 04 '19 at 16:04
  • one more question opened up. When you define the colors at different points, are those numbers absolute? or relative? because i want it to be able to change the radius in the .dat – ole Jul 04 '19 at 16:09
  • @ole Congratulations! You have just asked a question for which to the best of my knowledge there is no answer that works in all situations. According to how I read the pgfmanual it should be absolute but this does not seem to reflect what happens in examples. –  Jul 04 '19 at 16:16
  • That is too bad, i guess i will have to find a workaround with using variables... – ole Jul 04 '19 at 16:25
  • however i get errors with this code... ! Use of \pgf@parselastdomrgb doesn't match its definition. – ole Jul 04 '19 at 16:26
  • @ole When was the last time you updated your TeX installation? (Please do not tell me you are using overleaf. If you are, complain to them.) (BTW empirically I find that the fading scales, e.g. by using \path[fill=red,path fading=ringo] (\x,\y)circle[radius={sqrt(\row/4)*\radius cm}];.) –  Jul 04 '19 at 16:29
  • this comment section is getting long... no i am using latex on a computer i am not the admin of.. so basically the same thing... i cannot update it, i am happy i can use it at all. Was an effort for them to install it. – ole Jul 05 '19 at 06:33
  • @ole I understand but for me it is next-to-impossible to debug this because I would have to try out different constellations to reproduce the error first. You may be able to solve the issue by downloading pgfcoreshade.code.tex from CTAN and copying it to the same directory where you compile the file. –  Jul 05 '19 at 06:41
  • Ok it is not an issue of installation. I managed to recreate your code. for scaling this works well: color(0.1*\radius cm)=(transparent!\myvalB);% (with defining \radius before the pgfshading) however my bug appears again when i add more points... up to k – ole Jul 05 '19 at 06:51
  • @ole No, you cannot have an arbitrary number of points, only 3 or 4. However, you could use functional shading. –  Jul 05 '19 at 06:57
  • up there you said you could add more points. thats why i tried...i guess i will read up on point meta, but if i understood then i cannot have the transparency fade i am looking for. -.- – ole Jul 05 '19 at 07:06
0

All right i understood...

transparent gradient

Just wondering now how to define the gradient

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{forloop}
\usepackage{pgfplotstable}
\usepackage{readarray}
\usepackage{ifthen}
\usepackage[left=4cm, right=10cm, top=2cm]{geometry}

\usetikzlibrary{fadings}
\tikzfading[name=myfading, outer color=transparent!100, inner color=transparent!0]


\usepackage{filecontents}
\begin{filecontents*}{mydata.dat}
name    test    .dat
opacity:    10      percent
X   Y   Rad
0   0   0.75
0   1.8 0.75
0   3.6 0.75
0   5.4 0.75
0.75    0   0.75
0.75    1.8 0.75
0.75    3.6 0.75
0.75    5.4 0.75
1.5 0   0.75
1.5 1.8 0.75
1.5 3.6 0.75
1.5 5.4 0.75
2.25    0   0.75
2.25    1.8 0.75
2.25    3.6 0.75
2.25    5.4 0.75
3   0   0.75
3   1.8 0.75
3   3.6 0.75
3   5.4 0.75
3.75    0   0.75
3.75    1.8 0.75
3.75    3.6 0.75
3.75    5.4 0.75
4.5 0   0.75
4.5 1.8 0.75
4.5 3.6 0.75
4.5 5.4 0.75
5.25    0   0.75
5.25    1.8 0.75
5.25    3.6 0.75
5.25    5.4 0.75
6   0   0.75
6   1.8 0.75
6   3.6 0.75
6   5.4 0.75
\end{filecontents*}

\pgfplotsset{compat=1.16}

\begin{document}

\xdef \name {test.dat}                        %% 
\readdef{\name}{\data}
\readarray\data\MyDat[-,39, 3]

\section*{\MyDat[1,1,2] }

\centering
\begin{tikzpicture}[square/.style={regular polygon,regular polygon sides=4}]

\def \opacity {\MyDat[1,2,2]/10*0.15}
   \foreach \row in {4,...,39}{

                       \def\x{\MyDat[1,\row,1]}
                       \def\y{\MyDat[1,\row,2]}
                       \def\radius{\MyDat[1,\row,3]}

                 \fill[path fading=myfading, red,opacity= \opacity] (\x,\y)circle(\radius cm);

}
\end{tikzpicture}
\end{document}
ole
  • 33