I am having an issue setting the number of key (i.e., legend) columns using Gnuplot with the TiKz terminal. Here's a minimal working example of what I'm talking about:

#!/usr/bin/env gnuplot
set term tikz solid
set key above
set output "out.tex"

plot sin(x) title "Title 1",    cos(x) title "Title 2",    sin(x+1) title "Title 3",    cos(x+1) title "Title 4"

Which outputs this: Normal output.

This image has each of the plot titles in the legend arranged in 4 columns. However, once I add a really long title, it breaks it into 2 columns like so:

Output with long title.

Originally I thought that by reducing the font size used in the key it would alter the number of columns. For example, one could add set key font ",2" to Gnuplot which would change the size of the font in the key. This does reduce the size of the font, but keeps it as 2 columns as opposed to 4 like so:

Output with set key font.

I've also tried to reduce the size of the title by using LaTeX commands with the Gnuplot script. For example, I added title "\\tiny{Really Long Title}" to the Gnuplot script. However, this also only creates 2 columns in the key as opposed to 4 like so:

Output with LaTeX commands.

Ideally what I would like is a way to reduce the size of plot titles in the legend so that they reorganize into more short columns as opposed to few long columns. Any ideas on how to produce these results?

Update: I've tried other terminals (e.g., pdf) and it produces the same results so I'm assuming this is not just related to the TiKz terminal.

  • Can you please make up a very simple plot (having 3-4 coordinates or one simple function) and show exactly what doesn't work? You can strip off all the irrelevant parts and make a compilable Minimal Working Example. We can then try to help you a lot faster. – percusse Jan 15 '12 at 16:10
  • I've updated the post to include a MWE and screenshots of what I'm talking about. I hope this helps. – Chris Cannon Jan 17 '12 at 18:24

I took a look into the gnuplot manual and may have found what you are looking for:

set key above vertical maxrows 1

setting the key to vertical and then specifying the maximum number of rows to 1 sets all keys in one row. Since the key might then be too long to be fully displayed you might need to play with width:

This code

#!/usr/bin/env gnuplot
set key above width -8 vertical maxrows 1
set terminal jpeg
set output "test.jpg"

plot sin(x) title "really long title",    cos(x) title "Title 2",    sin(x+1) title "Title 3",    cos(x+1) title "Title 4"

gave this test.jpg:

enter image description here

  • Yes, this is the answer I was looking for! I have no idea how I missed that setting in the gnuplot manual, but thanks! – Chris Cannon Jan 20 '12 at 15:00
  • Which version of Gnuplot does this refer to? It does not seem to work with version 4.4 – Michael Palmer Jan 28 '12 at 17:13
  • The version I made it with was version 4.4 patchlevel 3. – clemens Jan 28 '12 at 17:17

This is not a true answer but an alternative approach using pgfplots (note that you have to use shell-escape in order to access gnuplot). The key legend columns=<num> enables you to specify the number of columns. -1 means "draw all entries horizontally".

  every axis/.append style={width=10cm,height=6cm},
  every axis plot post/.append style={mark=none},
  every axis plot/.append style={domain=-10:10,samples=75},
  every axis legend/.append style{at={(.5,1.05)},anchor=-90,draw=none,font=\small}

 \begin{axis}[legend columns=-1]
  \addplot gnuplot {sin(x)};
  \addplot gnuplot {cos(x)};
  \addplot gnuplot {sin(x+1)};
  \addplot gnuplot {cos(x+1)};
  \legend{very long title,Title 2,Title 3, Title 4}

 \begin{axis}[legend columns=2]
  \addplot gnuplot {sin(x)};
  \addplot gnuplot {cos(x)};
  \addplot gnuplot {sin(x+1)};
  \addplot gnuplot {cos(x+1)};
  \legend{very long title,Title 2,Title 3, Title 4}


enter image description here

  • Could I call a Gnuplot script file from within pgfplot? – Chris Cannon Jan 17 '12 at 20:26
  • I have never tried it, but there is the shell key which lets you access any shell commands. Also you can include different kinds of files like an eps generated by gnuplot. – clemens Jan 17 '12 at 20:35
  • 3
    Yes, you can: use \addplot gnuplot {<function expression>}; to plot a function, use \addplot gnuplot[raw gnuplot] {<gnuplot script>}; to enter a raw gnuplot script (which is supposed to plot data files eventually). – Christian Feuersänger Jan 17 '12 at 20:56

This may or may not work - I don't have the TikZ terminal installed. However, you could try to set the title to "\\short" in Gnuplot and then use a \newcommand{\short}{Long title} in TeX. Obviously, you would still have to make sure your substituted string actually fits.

  • That's a pretty nifty hack and it does work. – Chris Cannon Jan 17 '12 at 20:25

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.