QCustomPlot Discussion and Comments

How to add legend to SubplotReturn to overview
February 20, 2014, 17:16
by Jan

Hello guys,

I am just beginning to program using this fantastic library. The quality of the plots is awesome, better than anything, including Matlab. Although I thoroughly studied the examples, there is a question that still is unsolved for me:

How do I add a Legend to Sub-Plots? How would you add legends to the graphs e.g. in AdvancedAxesDemo?

Maybe someone could help me with this. I an sure this is super trivial, but somehow, I got stuck.

Cheers,
Jan

February 21, 2014, 01:32
by DerManu

Here's how you could do it:

 // create new axis rect and add it to the plot layout:
 QCPAxisRect *ar = new QCPAxisRect(ui->customPlot);
 ui->customPlot->plotLayout()->addElement(0, 1, ar);
 
 // setup an extra legend for that axis rect:
 QCPLegend *arLegend = new QCPLegend;
 ar->insetLayout()->addElement(arLegend, Qt::AlignTop|Qt::AlignRight);
 arLegend->setLayer("legend");
 ui->customPlot->setAutoAddPlottableToLegend(false); // would add to the main legend (in the primary axis rect)
 
 // create a graph in the new axis rect:
 QCPGraph *graph = ui->customPlot->addGraph(ar->axis(QCPAxis::atBottom), ar->axis(QCPAxis::atLeft));
 // add a legend item to the new legend, representing the graph:
 // arLegend->addItem(new QCPPlottableLegendItem(arLegend, graph));
 // Edit from the future: In newer QCP versions you can instead just call
 graph->addToLegend(arLegend);
 
 // this is how to remove the legend item again (e.g. before graph removal):
 // arLegend->remove(arLegend->itemWithPlottable(graph));
 // Edit from the future: In newer QCP versions you can instead just call
 graph->removeFromLegend(arLegend);

Adding and removing from the legend is a little less convenient/automated as for the default primary legend. I've noticed there's an easier way that QCustomPlot could provide: the QCPAbstractPlottable ::addToLegend /removeFromLegend could have an optional QCPLegend* parameter which could make it usable also for non-default legends. I'll try to add that in version 1.3.
// Edit from the future: QCustomPlot provides this now.

February 24, 2014, 09:36
by Jan

Thanks for your support! I really appreciate your effort and commitment.

- Jan

March 13, 2015, 13:09
by Evgenii

Thank you very much!! :-)

April 24, 2015, 10:29
by zzh

Thank you very much!! :-)

March 1, 2016, 15:54
by djentx

Thank you very much!! :-)

March 3, 2020, 06:52
by kingyue

Thank you very much!! :-)

May 25, 2022, 17:01
by eriks

Thank you very much!! :-)

December 2, 2022, 08:06
by Edmond

Thank you very much!! :-)

December 29, 2022, 14:36
by SlavaUkraini

Thank you very much!! :-)

January 9, 2023, 21:33
by liferips2022

Thank you very much!! :-)

December 20, 2023, 08:20
by dreamtale90

Thank you very much!! :-)

October 15, 2025, 17:59
by An

Thank you very much :D

October 16, 2025, 11:57
by DerManu

This clearly needs to be part of a demo :)

AltStyle によって変換されたページ (->オリジナル) /