QCustomPlot Discussion and Comments

Plot labelling for publication outputReturn to overview

Hi,
I want to add a label to the left of a series of subplots. In this case each plot is stacked vertically. The labels would read (a), (b), (c) etc as is common in report writing or academic output.

I was hoping I could use an equivalent approach to adding a plot title.

QCPTextElement *title = new QCPTextElement(customPlot);
title->setText("(a)");
customPlot->plotLayout()->insertColumn(0); // insert an empty column to the left of the axis rect
customPlot->plotLayout()->addElement(0, 0, title); // place the title in the empty cell we've just created
//finally adjust column stretchfactors

Whilst this does create a new column and adds the title, the plot now only occupies the top half of the entire layout as if a blank row were inserted below. However, row count confirms this is not the case. I am clearly misunderstanding the layout system. Can anyone identify where I've gone wrong?

Thanks.

QCPTextElement is one of the few layout elements that provide a maximum size hint. That's because it was meant as a header/title to stretch horizontally, but only have the height proportional to its font size. Accordingly, if you put it next to an axis rect, that text element dominates the height of the entire row and also forces the axis rect to compress in height.

If you want to stick with using QCPTextElement, the solution is simply just overriding the maximum size hint, by providing your own manual maximum size, e.g. like so:

title->setMaximumSize(25, 10000);

Alternatively I'd recommend looking into QCPItemText which might be more suitable for placing subfigure labels, especially when they should be located around the axis rect box or even within the axis rect, as some publications prefer. The positioning would then be done in ptAxisRectRatio mode, like so:

  mPlot->axisRect()->setMinimumMargins(QMargins(40, 15, 15, 35)); // add some more minimum space at bottom left of rect for subfigure label (15 is default)
  
  QCPItemText *subFigLabel = new QCPItemText(mPlot);
  subFigLabel->setClipToAxisRect(false);
  subFigLabel->setText("(a)");
  QFont font = subFigLabel->font();
  font.setBold(true);
  subFigLabel->setFont(font);
  
  // position just outside bottom left corner of axis rect
  subFigLabel->position->setType(QCPItemPosition::ptAxisRectRatio);
  subFigLabel->position->setAxisRect(mPlot->axisRect());
  subFigLabel->position->setCoords(0, 1); // bottom left of axis rect...
  subFigLabel->setPositionAlignment(Qt::AlignTop|Qt::AlignRight); // ...matches top right of label
  subFigLabel->setPadding(QMargins(0, 15, 15, 0)); // and add some padding on top right of label
  

Hi,

Thanks for this. More than enough info here for me to tweak and get the desired layout.