How to get gnuplot to use a centered multi-line title, with left-aligned lines?

alignment, centering, gnuplot, title

Solution

This is a bit tricky. As said in the gnuplot documentation:

The `set title` command produces a plot title that is centered at the top of
the plot.  `set title` is a special case of `set label`.

Although the `label` command accepts a justification parameter, e.g.

set label "mylabel" right

`title` does not: it is hard-set to be centered. The workaround I have is to use a `label` at the position where the title would be. To make multiple lines use a newline (`\n`) within double quotes.

set title "\n"
set label 1 "first line\nsecond line" at graph 0.5,1.125 left

The dummy `set title` command is so that gnuplot adjusts the top margin for a two-line title. I found that that position (0.5,1.125) reproduces the default title position well. This won't center the label around the middle of the plot, though--it will be left- or right-justified to the center line. The workaround would be to manually adjust the x-position of the label:

set title "\n"
shift = 0.05 # manually adjust
set label 1 "first line\nsecond line" at graph (0.5-shift),1.125 left

Problem

I want my (gnu)plot to have a multi-line title. I would like the title to be centered (i.e. the distance from the center of the widest title line to the edges should be equal), but not to have each line centered independently of the others, which is the default behavior; I want to have the title lines left-aligned and centered only as a block. How can I achieve this?

Original source