JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
Example (JKQTPlotter): Different Types of Errorindicators

This project (see ./examples/errorbarstyles/) simply creates a JKQTPlotter widget (as a new window) and adds several curves show-casing different styles of error indicators. Data is initialized from two QVector<double> objects.

Note: This examples explains how to plot graphs with error indicators, when the data has already been calculated. The tutorial 1-Dimensional Group Statistics with JKQTPDatastore explains one way how to use the JKQTPlotter Statistics Library in order to calculate the errors from data.

The source code of the main application can be found in errorbarstyles.cpp.

First some data is added to the internal datastore (mostly, like explained in several other examples, like e.g. Line Graph with Different Symbols and Line Styles). The (in a loop) several graphs are added, each with a distinct style for its error indicators:

// 3. now we make several plots with different error styles
// for that we iterate over every style from the vector errorStyles
// the array showXandYError indicates whether we want x- and y-error
// for a style for all stywhere this is false, only y-error-indicators
// are shown
QVector<bool> showXandYError { false , true , true , false , false , true , true , false , false , false , false };
for (int errorID=0; errorID<errorStyles.size(); errorID++) {
// generate some plot data
QVector<double> Y;
for (auto& xx: X) {
Y<<xx*0.5+static_cast<double>(errorID)*2.5;
}
// create a graph object
// copy data into datastore and immediately set the yColumn
graph->setXColumn(columnX);
graph->setYColumn(ds->addCopiedColumn(Y, "y"+QString::number(errorID)));
graph->setXErrorColumn(columnXError);
graph->setYErrorColumn(columnYError);
// set error style, for the y-axis
graph->setYErrorStyle(errorStyles[errorID]);
// no error indicators for the x-values
// ... unless: for some error styles we want error in both directions
if (showXandYError[errorID]) {
graph->setXErrorStyle(errorStyles[errorID]);
graph->setDrawLine(false);
}
// make error indicator 30% transparent
QColor c=graph->getErrorFillColor();
c.setAlphaF(0.3);
graph->setErrorFillColor(c);
// set error indicator line width
graph->setErrorLineWidth(1);
// set length of small bars at the end of error bars
graph->setErrorBarCapSize(15);
// set symbol (cross/X) + pen style (and color)dashed)
graph->setLineStyle(Qt::DashLine);
// set symbol size
graph->setSymbolSize(5);
// set width of symbol lines
graph->setSymbolLineWidth(1);
// set width of graph line
graph->setLineWidth(1);
// graph title is made from symbol+errorStylestyle, we use the LaTeX instruction \verb around the
// result of JKQTPErrorPlotstyle2String(), because it contains underscores that would otherwise
// lead to lower-case letter, which we don't want
graph->setTitle("\\verb{"+JKQTPErrorPlotstyle2String(errorStyles[errorID])+"}");
// add the graph to the plot, so it is actually displayed
plot.addGraph(graph);
}
QColor getErrorFillColor() const
get the fill color of the error indicators
void setErrorBarCapSize(double __value)
size of the error bar end markers [pt]
void setErrorLineWidth(double __value)
set the width [pt] of the error indicator (out)lines
void setErrorFillColor(const QColor &__value)
set the fill color of the error indicators
void setLineWidth(double __value)
set the line width of the graph line (in pt)
void setLineStyle(Qt::PenStyle __value)
set the style of the graph line
void setSymbolLineWidth(double __value)
set the line width of the graph symbol outline (in pt)
void setSymbolSize(double __value)
set the size (=diameter in pt) of the graph symbol (in pt)
void setSymbolType(JKQTPGraphSymbols __value)
set the type of the graph symbol
virtual void setTitle(const QString &__value)
sets the title of the plot (for display in key!).
void setXErrorColumn(int __value)
the column that contains the error of the x-component of the datapoints
void setXErrorStyle(JKQTPErrorPlotstyle __value)
how to draw the errors (if available) of the x-value
void setYColumn(int __value)
the column that contains the y-component of the datapoints
void setXColumn(int __value)
the column that contains the x-component of the datapoints
This implements xy line plots with x and y error indicators.
Definition jkqtplines.h:112
void setDrawLine(bool __value)
indicates whether to draw a line or not
void setYErrorColumn(int __value)
the column that contains the error of the x-component of the datapoints
void setYErrorStyle(JKQTPErrorPlotstyle __value)
how to draw the errors (if available) of the x-value
JKQTPLOTTER_LIB_EXPORT QString JKQTPErrorPlotstyle2String(JKQTPErrorPlotstyle pos)
converts a JKQTPErrorPlotstyle variable into a human-readable string
@ JKQTPErrorSimpleBars
simplified error bars for each data point
Definition jkqtptools.h:693
@ JKQTPErrorBarsPolygons
error bars and polygons for each data point
Definition jkqtptools.h:719
@ JKQTPErrorEllipses
an ellipse spanned by the errors
Definition jkqtptools.h:696
@ JKQTPErrorLines
a second and third graph line above and below the actual data which indicates the error value
Definition jkqtptools.h:694
@ JKQTPErrorPolygons
line error lines, but with filled range in between
Definition jkqtptools.h:695
@ JKQTPNoError
don't show error information
Definition jkqtptools.h:692
@ JKQTPErrorBoxes
a box spanned by the errors
Definition jkqtptools.h:697
@ JKQTPErrorSimpleBarsPolygons
simplified error barsand polygons for each data point
Definition jkqtptools.h:712
@ JKQTPErrorSimpleBarsLines
simplified error bars and line for each data point
Definition jkqtptools.h:713
@ JKQTPErrorBarsLines
error bars and lines for each data point
Definition jkqtptools.h:718
@ JKQTPErrorBars
error bars for each data point
Definition jkqtptools.h:711
@ JKQTPCross
a X cross
Definition jkqtpdrawingtools.h:146

The error styles are set in these lines:

// set error style, for the y-axis
graph->setYErrorStyle(errorStyles[errorID]);
// no error indicators for the x-values
graph->setXErrorStyle(errorStyles[errorID]);

There are several variables that can be used to further style the error indicator, like:

// make error indicator 30% transparent
QColor c=graph->getErrorFillColor();
c.setAlphaF(0.3);
// set error indicator line width
// set length of small bars at the end of error bars
graph->setErrorBarCapSize(15);

There are more properties that you can find in the documentation of the mix-in classes JKQTPXYGraphErrors, JKQTPXGraphErrors, JKQTPYGraphErrors, JKQTPGraphErrors.

In addition the plot key is moved outside the pot and the grid in the plot is switched off:

// 6. change locaion of key (outside top-right)
plot.getPlotter()->setKeyPosition(JKQTPKeyOutsideRightTop);
// ... and switch off the grid
plot.getXAxis()->setDrawGrid(false);
plot.getXAxis()->setDrawMinorGrid(false);
plot.getYAxis()->setDrawGrid(false);
plot.getYAxis()->setDrawMinorGrid(false);
@ JKQTPKeyOutsideRightTop
the key is positioned on the left side of the graph, towards the top
Definition jkqtptools.h:579

The result looks like this:

errorbarstyles

Error bars are implemented in the mixin-classes JKQTPXYGraphErrors, JKQTPXGraphErrors and JKQTPYGraphErrors that are all derived from JKQTPGraphErrors. With these it is simple to add error indicators to several different plot styles. Usually you can recognize these by looking at the class name, e.g. JKQTPXYLineGraph is a simple line+symbol graph, and JKQTPXYLineErrorGraph is the same with error indictaors (see above). There are also several other plots with error indicators: