Skip to content

Instantly share code, notes, and snippets.

@dilawar
Last active July 31, 2018 06:07
Show Gist options
  • Save dilawar/5b3d400079c912b42ba274035e655507 to your computer and use it in GitHub Desktop.
Save dilawar/5b3d400079c912b42ba274035e655507 to your computer and use it in GitHub Desktop.
My PGFPLOTS notes

Labels

  1. Attach label on axis by relative position \node[yshift=3mm0] at (rel axis cs:0,1) {\bf A};. Always put this line outsize axis environment. Otherwise, it will be clipped.

\addlegendentry inside \foreach loop

\foreach \x in {1,2,3}
{
     % add your plot.
    \edef\temp{\noexpand\addlegendentry{Ca=\SI{\x}{\nano M}}} \temp
}

Subplots

Use tabular environment with align=baseline options. Otherwise use a global tikzpicture in which each node itself contain a tikzpicture. The later has the benefit of creating arbitary layout.

matplotlib imshow like figure

\begin{tikzpicture}[scale=1]
    \begin{loglogaxis}[ 
        xlabel=$D_{PP1}$
        , ylabel=$D_{xy}$ 
        , view={0}{90}
        , width=0.8*\PLOTW cm %, height=\PLOTW cm
        , enlargelimits=0.05
        , colorbar
        , zmin=0
        ]

        \addplot3 [surf, shader=faceted, mesh/rows=7,] 
            table [col sep=comma,surf
                , x expr={10^\thisrow{Dpp1}}
                , y expr={10^\thisrow{Dxy}},z=Mean CaMKII
            ]{./data/CaM:4+PP:100+D:x:5e-14+y:5e-14+PP1:5e-14+N:4.csv};

        \addplot3[mesh/rows=7, only marks, shader=faceted
            , unbounded coords=jump
            % remove zero points
            , z filter/.expression={z>0?1:nan}
            ] table[col sep=comma
                , x expr={10^\thisrow{Dpp1}}
                , y expr={10^\thisrow{Dxy}},z=is_bistable
            ]{./data/CaM:4+PP:100+D:x:5e-14+y:5e-14+PP1:5e-14+N:4.csv};
    \end{loglogaxis}
\end{tikzpicture}

Genearates the following image (when files are available).

image

Error bar on every nth point

https://tex.stackexchange.com/a/99328/8087

Code from above link.

\documentclass[tikz,preview, border=5mm]{standalone}
\usepackage{tikz,pgfplots,filecontents}
\pgfplotsset{compat=1.7} 

%%% Code for "every nth mark" starts here...
\newcounter{marknumber}
\pgfplotsset{
    error bars/every nth mark/.style={
        /pgfplots/error bars/draw error bar/.prefix code={
            \pgfmathtruncatemacro\marknumbercheck{mod(floor(\themarknumber/2),#1)}
            \ifnum\marknumbercheck=0
            \else
                \begin{scope}[opacity=0]
            \fi
        },
        /pgfplots/error bars/draw error bar/.append code={
            \ifnum\marknumbercheck=0
            \else
                \end{scope}
            \fi
            \stepcounter{marknumber}    
        }
    }
}
%%% ... and ends here

\begin{filecontents*}{data.dat}
X Y Y_error
1 1.39 0.5
2 2.44 0.5
3 3.62 0.5
4 4.81 0.5
5 1.39 0.5
6 2.44 0.5
7 3.62 0.5
8 4.81 0.5
\end{filecontents*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    clip mode=individual,
    ]
    \addplot+[
            error bars/.cd,
                y dir=both,
                y explicit,
                every nth mark=2
        ] table[x=X,y=Y,y error=Y_error] {data.dat};
\end{axis}
\end{tikzpicture} 

\end{document}

Horizontal colorbar.

, colorbar horizontal
, colorbar style={ height=1.5mm, samples=3, draw=none
      , xticklabel style={font=\tiny}, at={(0,1.15)}
      , anchor=south west
      , xticklabel pos=upper
      }

image

Shorten line in legend

See This TeX.SE question

\pgfplotsset{
   compat=1.11,
   legend image code/.code={
      \draw[mark repeat=2,mark phase=2]
           plot coordinates {
           (0cm,0cm)
           (0.15cm,0cm)        %% default is (0.3cm,0cm)
           (0.3cm,0cm)         %% default is (0.6cm,0cm)
       };%
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment