2

I am using pgfplots for crystallographic data analysis. I can effectively use yshift to shift plots within the axis environment, however, the legend entry line reference also shifts. I originally intended to use pgfplotstable to simply add or subtract from every data point to shift but this was computationally intensive (many data points). Excuse the constant plot hack to have a drop-down plot. Representative Code (courtesy of commenter):

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat = 1.13}
\pgfplotstableset{col sep = comma}

\begin{document}
\pgfplotstableread{
20, 1000
70, 500
}{\data}

\begin{tikzpicture}
\begin{axis}

\addplot[black] table[x index = 0, y index = 1, col sep = comma] {\data};
\addlegendentry[]{040\_Mn}
\addplot[red!20!blue] table[x index = 0, y expr={\thisrowno{1} - 500}] {\data};
\addlegendentry[]{023\_Mn}
\addplot[yshift=-2cm,red!20!blue] table[x index = 0, y index=1] {\data};
\addlegendentry[]{035\_Mn}
\addplot[yshift = -5cm, red!30!blue] expression {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

Result

4
  • It would be much more convenient if you could make an example that didn't rely on other files that are not available. So something like \addplot[red!20!blue] table[x index = 0, y expr={\thisrowno{1} - 500}] {\twentythreeMn}; was very slow? Commented Sep 6, 2016 at 13:59
  • I tried to load the table first as a macro using \pgfplotstable, which in some cases went over memory. With this method it is approximately 1 minute per graph. Thank you! This works because I can do formatting using the key each nth point and then do a final run with all data points. In the future I thought of doing xshift and additional manipulation like scale treating the addplot as a layer within the axis environment. Can we separate the plot styling from its legend entry? Attempting to reciprocally shift the legend entry doesn't work. Commented Sep 6, 2016 at 14:42
  • Sorry, I didn't mean that you should remove the plots altogether, the code you have now doesn't show the problem at all, as there is no yshift anywhere. I mean either add the datafiles, or use some dummy data, e.g. as in bitbucket.org/snippets/torbjornt/ogExK Commented Sep 6, 2016 at 14:44
  • Very obvious, sorry, updated with your suggestion. Commented Sep 6, 2016 at 14:47

1 Answer 1

1

Here are two options. First, instead of yshift, use something like

\addplot[red!20!blue] table[x index = 0, y expr={\thisrowno{1} - 500}] {\data};

This subtracts 500 from the y-value in the table. You can use the name of a text file instead of loading into a macro (here \data) first.

Another option is to use yshift, but add forget plot to the \addplot options. forget plot disables adding the plot to the legend, so you need \addlegendimage{<style options for plot>} instead.

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat = 1.13}
\pgfplotstableread[col sep=comma]{
20, 1000
70, 500
}{\data}

\begin{document}
\begin{tikzpicture}
\begin{axis}

\addplot[black] table[x index = 0, y index = 1] {\data};
\addlegendentry[]{040\_Mn}
\addplot[red!20!blue] table[x index = 0, y expr={\thisrowno{1} - 500}] {\data};
\addlegendentry[]{023\_Mn}
\addplot[yshift=-2cm,green,forget plot] table[x index = 0, y index=1] {\data};
\addlegendimage{green}
\addlegendentry{shifted}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.