3

I'm writing some app, which generates a TeX code. My program creates variable number of plots and I'd like to use visualize them using pgfplots.

In the old version of this app (not created by me), plots are generated with gnuplot and they look like this:

enter image description here

In contrast, my plot is looking like this now:

enter image description here

The first problem is, it appears, that pgfplots automatically use lesser number of colors for plots that gnuplot, so when I add more plots to the picture, it just keeps switching between blue, red and black, solid, dashed etc. The problem is, when we have multiple plots, just 3 colors make them undifferentiable.

On the other hand, gnuplot seems to have a very nice ability to automatically differentiate them visually from each other.

Is there any way to get similar behavior with pgfplots? I can't set the color of each plot manually, because I simply don't know, how many of the will be there - app must work for arbitrary number of plots.

The second problem is the position of the plot itself in the paper. tikzpicture tends to have a huge indent from the left side of the paper and I'm not able to align it to the left.

enter image description here

Because of the size of plots I really need to force this position to use most of the paper for the plot.

Is there any way how to do it?


My code

\begin{document}

\flushleft
\begin{tikzpicture}[scale=1, remember picture,overlay]
\begin{axis}[
title={ TEST graf },
xlabel={ Number of samples },
ylabel={ Power[W] },
legend pos=outer north east,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
no markers,
width=18cm,
anchor=north west
]
\addplot table [x=ind, y=val, col sep=comma] {data0.csv};
\addplot table [x=ind, y=val, col sep=comma] {data1.csv};
\addplot table [x=ind, y=val, col sep=comma] {data2.csv};
\addplot table [x=ind, y=val, col sep=comma] {data3.csv};
\legend{ 0,1,2,3}
\end{axis}
\end{tikzpicture}

1st Attempt

After adding cycle list name = color list, removing anchor = north east and wrapping my tikzpicture into adjustbox environment with options valign=t, center my code looks like this:

\begin{adjustbox}{valign=t, center}
\begin{tikzpicture}[scale=1]
\begin{axis}[
title={ TEST graf },
xlabel={ Number of samples },
ylabel={ Power[W] },
legend pos=outer north east,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
no markers,
width=24cm,
height=15cm,
cycle list name = color list
]
\addplot table [x=ind, y=val, col sep=comma] {data0.csv};
\addplot table [x=ind, y=val, col sep=comma] {data1.csv};
\addplot table [x=ind, y=val, col sep=comma] {data2.csv};
\addplot table [x=ind, y=val, col sep=comma] {data3.csv};
\legend{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 }
\end{axis}
\end{tikzpicture}

\end{adjustbox}

And the plot is much better:

enter image description here

The only remaining problem is, that valign option is not working - the picture is not on the top of the page. What am I doing wrong this time?

10
  • 4
    For the colors, have a look at cycle lists in the manual, see section 4.7.7 Cycle Lists – Options Controlling Line Styles. Regarding the horizontal alignment of the plot, I'm guessing this has nothing to with tikzpicture, but that your text block is narrower than 18cm (the width of your axis), so it sticks into the right margin. To get around that you can use the techniques in Center figure that is wider than \textwidth or How can I center a too wide table? Jun 27, 2016 at 9:36
  • Also if you are not doing some custom placement or later referral remove remember picture,overlay from the picture options
    – percusse
    Jun 27, 2016 at 10:09
  • @TorbjørnT. Thank you very much! My only remaining problem is now, that vertical align doesn't work with adjustbox - could you, please have a look at it? (description of the problem was added into the question)
    – Eenoku
    Jun 27, 2016 at 11:12
  • You've misunderstood what valign does, it just sets the "anchor" of the box on the baseline, it's not relative to the page. Quick hack, try adding \vspace*{-3cm} just before the tikzpicture. Jun 27, 2016 at 11:14
  • ... but if the only thing in your document is this one figure, remove the adjustbox stuff, and switch to using \documentclass{standalone}. That will give you a page that is the size of the tikzpicture, without needing to adjust anything. Jun 27, 2016 at 11:16

1 Answer 1

2

The colors, markers and line styles used by pgfplots are defined in so called cycle lists. There are several such defined already, see the pgfplots manual section 4.7.7 Cycle Lists – Options Controlling Line Styles. If none of the existing ones are satisfactory, you can define your own cycle lists, see my answer to pgfplots adding unwanted dashed lines for one example.

There are a couple of options for centering the figure horizontally and vertically. For horizontal centering you could use the trick in Center figure that is wider than \textwidth: \makebox[\textwidth][c]{...}. To center it vertically one could add negative vertical space with \vspace*{-3cm}, but alternatively one can put everything in a minipage with a specified height, i.e. \begin{minipage}[c][\textheight]{\textwidth} ...

Another option is to add \usepackage{adjustbox}, and use \begin{adjustbox}{center,set height=\textheight} ... \end{adjustbox} around the figure. Both are demonstrated below, though they give slightly different vertical alignment (not sure why, at the moment).

\documentclass{article}
\usepackage[a4paper,landscape]{geometry}
\usepackage{pgfplots}
\usepackage{adjustbox}
\begin{document}
\noindent\begin{minipage}[c][\textheight]{\textwidth}
\makebox[\textwidth][c]{\begin{tikzpicture}
\begin{axis}[
title={ TEST graf },
xlabel={ Number of samples },
ylabel={ Power[W] },
legend pos=outer north east,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
no markers,
width=24cm,
height=15cm,
cycle list name = color list
]
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\legend{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 }
\end{axis}
\end{tikzpicture}}
\end{minipage}

\begin{adjustbox}{center,set height=\textheight}
\begin{tikzpicture}
\begin{axis}[
title={ TEST graf },
xlabel={ Number of samples },
ylabel={ Power[W] },
legend pos=outer north east,
xmajorgrids=true,
ymajorgrids=true,
grid style=dashed,
no markers,
width=24cm,
height=15cm,
cycle list name = color list
]
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\addplot {rnd};
\legend{ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 }
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .