2

I'm fairly new to pgfplots.

I'm trying to graph two things, and have them plot side by side. However, when I use subfigure, my plots (and axes labels) overlap. How can I fix this?

\documentclass{amsart} 
\usepackage{amsthm, amsfonts, amsmath, amssymb, mathrsfs, enumerate,graphicx} 
\usepackage{pgfplots} %For creating plots inside LaTeX itself
\usepackage{subcaption} %To create subfigures

\begin{document}
\begin{figure}
\centering
\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
                ymin=0,  xmin=0,
                %grid=major, % activate major grid lines
                xlabel=$\text{radius, }r$,
                ylabel=$\text{concentration, }c$,
                title={Steady state concentration},
                title style={yshift=1.5ex},
                axis on top, % descriptions over filled area
                legend pos=outer north east, % customize legend
        ]

        \addplot[smooth,blue,domain=0.5:2]
                {((1-0.00167)*ln(x)+0.00167*ln(0.5)-1*ln(2))/(ln(0.5)-ln(2))} ;
        \end{axis}
\end{tikzpicture}
\caption{Steady state species concentration profile. }
\end{subfigure}
\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
                ymin=0,  xmin=0,
                xlabel=$\text{radius, }r$,
                ylabel=$\text{chemical potential, }\mu^{c^*}$,
                title={Steady state chemical potential},
                title style={yshift=1.5ex},
                axis on top, % descriptions over filled area
                legend pos=outer north east, % customize legend
        ]

        \addplot[smooth,blue,domain=0.5:2]
                {8.31*310*ln(((1-.00167)*ln(x)+.00167*ln(0.5)-1*ln(2))/(ln(0.5)-ln(2))/.00167)} ;
        \end{axis}
\end{tikzpicture}
\caption{Steady state chemical potential profile}
\end{subfigure}
\caption{Illustration of analytical steady state solution.}
\end{figure}

\end{document}
4

The problem with \begin{tikzpicture}[scale=0.65] is that the fonts get scaled too making them look smaller. You can adjust the width of the plots instead with the option width provided by pgfplots. You can make this for all plots by adding

\usepackage{pgfplots} %For creating plots inside LaTeX itself
\pgfplotsset{width=2in} %%<-------------------- this

or for only specific plots by adding it to the `axis options like

\begin{axis}[ width=2in, ymin=0, xmin=0, . .

Now the fonts remain with the same size. Further, you can uae scale only axis, to scale only the axis without considering tick labels. And last, you may wish to add

ylabel style={overlay, anchor=north,},      %%% <-- this added

so that the labels are bit closer to the y axis.

\documentclass{amsart}
\usepackage{amsthm, amsfonts, amsmath, amssymb, mathrsfs, enumerate,graphicx}
\usepackage{pgfplots} %For creating plots inside LaTeX itself
\usepackage{subcaption} %To create subfigures
%\pgfplotsset{width=2in}

\begin{document}
\begin{figure}
\centering
\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
                width=\linewidth,  %%<----- here
                scale only axis,       %%% <-- this added
                ymin=0,  xmin=0,
                %grid=major, % activate major grid lines
                xlabel=$\text{radius, }r$,
                ylabel=$\text{concentration, }c$,
                ylabel style={overlay, anchor=north,},      %%% <-- this added
                title={Steady state concentration},
                title style={yshift=1.5ex},
                axis on top, % descriptions over filled area
                legend pos=outer north east, % customize legend
        ]

        \addplot[smooth,blue,domain=0.5:2]
                {((1-0.00167)*ln(x)+0.00167*ln(0.5)-1*ln(2))/(ln(0.5)-ln(2))} ;
        \end{axis}
\end{tikzpicture}
\caption{Steady state species concentration profile. }
\end{subfigure}%
\hfill
\begin{subfigure}{.45\linewidth}\centering
\begin{tikzpicture}
\begin{axis}[
                width=\linewidth,   %%<-------- here
                scale only axis,       %%% <-- this added
                ymin=0,  xmin=0,
                xlabel=$\text{radius, }r$,
                ylabel=$\text{chemical potential, }\mu^{c^*}$,
                ylabel style={overlay, anchor=north,},       %%% <-- this added
                title={Steady state chemical potential},
                title style={yshift=1.5ex},
                axis on top, % descriptions over filled area
                legend pos=outer north east, % customize legend
        ]

        \addplot[smooth,blue,domain=0.5:2]
                {8.31*310*ln(((1-.00167)*ln(x)+.00167*ln(0.5)-1*ln(2))/(ln(0.5)-ln(2))/.00167)} ;
        \end{axis}
\end{tikzpicture}
\caption{Steady state chemical potential profile}
\end{subfigure}
\caption{Illustration of analytical steady state solution.}
\end{figure}

\end{document}

enter image description here

  • While I like this idea, I feel like this is really shrinking the graphics as well. I recommend using the size of width=3in, at least for my template. @Harish, what part of the plot does this width tag modify? Just the black box containing the blue graph? Or also the numbers labeling the axes? Or even larger, the whole image including the graph title and axes labels? – user2441511 Dec 12 '14 at 19:30
  • @user30392 It is the width need for axis and ticklabels. For details refer pgfplots manual (texdoc pgfplots from terminal) section 4.10.1, page 237 (for v1.11, you may search for /pgfplots/width). You can safely say width=\linewidth in this case so that graph occupies max width i.e. width of subfigure. There is also scale only axis which changes only axis width/height. – user11232 Dec 12 '14 at 23:15
  • @user30392 I have updated the answer in which you may be interested. – user11232 Dec 12 '14 at 23:30
2

use for both

\begin{tikzpicture}[scale=0.65]
...
  • I tried that, but now it's not compiling. Do I also need to remove the centering? \begin{figure} \centering \begin{subfigure}[scale=0.65]\centering \begin{tikzpicture} – user2441511 Dec 10 '14 at 22:12
  • 1
    @user30392 It should be \begin{subfigure} \begin{tikzpicture}[scale=0.65]... Look carefully at what Herbert has written... – darthbith Dec 10 '14 at 22:17
  • I get it now...I need to define the size on the subfigure as well as defining the scale on the tikzpicture. Thanks @Herbert So now I have \begin{figure} \begin{subfigure}{.5\linewidth}\centering \begin{tikzpicture}[scale=0.65] Do I still need the scale on the subfigure? I think I need something there... – user2441511 Dec 10 '14 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.