First Plot - Getting Started with QwtPlot¶
A minimal Qwt program creates a QwtPlot window, attaches a grid and a curve, and calls replot() to display the data.
QwtPlot Basic Concepts¶
QwtPlot is the core class of the Qwt plotting library. It is a Qt widget for rendering 2D graphics. A QwtPlot can contain multiple plot items such as curves, grids, markers, etc. All plot items must be attached to the QwtPlot via the attach() method to be displayed.
Plot Item Types¶
The class diagram below introduces the plot items you will meet most often: QwtPlotCurve draws lines and points, QwtPlotGrid draws the coordinate reference lines, QwtPlotMarker annotates values, and QwtPlotBarChart renders bars. All of them derive from the common base class QwtPlotItem and share its attach() / detach() / setTitle() interface:
classDiagram
class QwtPlotItem {
+attach(plot)
+detach()
+setTitle()
+setItemAttribute()
}
class QwtPlotCurve {
+setSamples()
+setPen()
+setSymbol()
+setStyle()
}
class QwtPlotGrid {
+enableX()
+enableY()
+setMajorPen()
+setMinorPen()
}
class QwtPlotMarker {
+setValue()
+setLineStyle()
+setLabel()
}
class QwtPlotBarChart {
+setSamples()
+setLayoutPolicy()
+setSymbol()
}
QwtPlotItem <|-- QwtPlotCurve
QwtPlotItem <|-- QwtPlotGrid
QwtPlotItem <|-- QwtPlotMarker
QwtPlotItem <|-- QwtPlotBarChart
Axis System¶
QwtPlot provides four axis positions by default:
| Axis Position | Enum Value | Description |
|---|---|---|
| Bottom X-axis | QwtAxis::XBottom |
Visible by default |
| Top X-axis | QwtAxis::XTop |
Hidden by default |
| Left Y-axis | QwtAxis::YLeft |
Visible by default |
| Right Y-axis | QwtAxis::YRight |
Hidden by default |
Basic Usage Workflow¶
The basic steps for creating a plot with QwtPlot are as follows:
flowchart TD
A[Create QwtPlot object] --> B[Set title and style]
B --> C[Create plot items]
C --> D[Set plot item properties and data]
D --> E[Attach to QwtPlot]
E --> F[Set axis ranges]
F --> G[Refresh display with replot]
Simplest Example¶
The simple plot example is located at: examples/2D/simpleplot. A screenshot is shown below:

Complete Code¶
The example below puts all the pieces together: it constructs a QwtPlot window, attaches a QwtPlotGrid and a QwtPlotCurve styled with a QwtSymbol, feeds the curve a handful of QPointF samples through setSamples(), and finally shows the window; the whole program is only about 40 lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
Code Walkthrough¶
1. Creating the Plot Window¶
This first step builds the window itself: construct a plain QwtPlot object and give it a title plus a white canvas background via setCanvasBackground():
1 2 3 | |
QwtPlot is the main container for the plot, inheriting from QFrame. The canvas background color determines the base color of the plot area. It is recommended to use white or a light color for clear data display.
2. Adding a Grid¶
A QwtPlotGrid draws the horizontal and vertical reference lines behind your data; like every plot item, it only shows up after being mounted onto the plot with attach():
1 2 | |
The grid is a standalone plot item used to display coordinate reference lines. All plot items are attached to QwtPlot via the attach() method, and QwtPlot automatically manages the lifecycle of attached items.
Tip — Plot Item Lifecycle
Once a plot item is attached to QwtPlot, QwtPlot holds a reference to that item. When QwtPlot is destroyed, all attached plot items are automatically deleted as well. Therefore, you do not need to manually manage the memory of plot items.
3. Creating a Curve and Setting Its Style¶
The data itself is drawn by a QwtPlotCurve: setTitle() gives it a name for the legend, setPen() picks the line color and width, and setRenderHint() with QwtPlotItem::RenderAntialiased smooths the line:
1 2 3 4 | |
Curves are the most commonly used plot item type:
- setTitle() sets the curve title, which is displayed in the legend
- setPen() sets the line color, width, and style
- setRenderHint() enables anti-aliased rendering for smoother lines
4. Setting Data Point Symbols¶
To mark every sample with a shape, build a QwtSymbol (here an Ellipse with a yellow brush, a red pen, and a size of 8×8 pixels) and hand it to the curve with setSymbol():
1 2 3 4 5 6 7 | |
Symbols are used to display markers at each data point position. QwtSymbol supports various shapes:
- Ellipse - Ellipse/Circle
- Rect - Rectangle
- Diamond - Diamond
- Triangle - Triangle
- Cross - Cross
- XCross - X-shaped cross
5. Setting Curve Data¶
Samples reach the curve through setSamples(); the simplest overload takes a QPolygonF, a list of QPointF values, as shown below:
1 2 3 | |
setSamples() sets the curve data; Qwt provides several overloads:
| Method | Description |
|---|---|
setSamples(const QPolygonF&) |
Set from a QPointF array |
setSamples(const QVector<double>& x, const QVector<double>& y) |
Set from two arrays |
setSamples(const double* x, const double* y, int size) |
Set from raw arrays |
setRawSamples(const double* x, const double* y, int size) |
Directly reference external arrays (no copy) |
Warning — setRawSamples Caveats
When using
setRawSamples(), Qwt does not copy the data but directly references the arrays you provide. This means you must ensure the arrays remain valid for the lifetime of the curve and must not modify the array contents.
6. Setting Axes¶
Finally, setAxisScale() fixes an axis (selected by a QwtAxis::Position value such as QwtAxis::YLeft) to a given range instead of letting Qwt derive one from the data:
1 | |
setAxisScale() is used to manually set the axis range. If this method is not called, Qwt automatically calculates an appropriate range based on the data of the attached plot items.
Advanced Configuration¶
Multiple Curves¶
Plotting another data series takes only a second QwtPlotCurve: give it its own title and pen, load its samples with setSamples(), and attach it to the same QwtPlot:
1 2 3 4 5 | |
A single QwtPlot can have any number of plot items attached, and they are drawn in the order they were attached.
Setting Axis Titles¶
Axis captions are set with setAxisTitle(): the first argument selects the axis position (QwtAxis::XBottom, QwtAxis::YLeft, …) and the second is the text to display:
1 2 | |
Auto-Replot Mode¶
If you would rather not call replot() yourself after every change, setAutoReplot(true) lets the plot refresh automatically whenever an item's data or attributes change:
1 | |
When auto-replot is enabled, QwtPlot automatically calls replot() to update the display whenever a plot item's data or properties change. By default, you need to manually call replot() to refresh the plot.
Tip — Performance Tip
For real-time update scenarios, it is recommended to disable auto-replot and manually call
replot()once after batch updates are complete. This avoids the performance overhead of frequent refreshes.
QwtPlot Core Methods¶
| Method | Description |
|---|---|
setTitle() |
Set the plot title |
setCanvasBackground() |
Set the canvas background color |
setAxisScale() |
Set the axis range |
setAxisTitle() |
Set the axis title |
insertLegend() |
Insert a legend |
replot() |
Refresh the plot display |
setAutoReplot() |
Set auto-replot mode |
exportPlot() |
Export the plot to a file |
Example — Related Examples
- Basic plot:
examples/2D/simpleplot- Curve style demo:
examples/2D/curvedemo- Real-time data:
examples/2D/cpuplot