Histogram - QwtPlotHistogram¶
QwtPlotHistogram is used to draw histograms, representing a series of intervals associated with values. Each interval corresponds to a numeric value, commonly used in statistics to display data distribution frequency.
Key Features¶
- Multiple display styles: Supports outline, column, and line drawing modes
- Interval data structure: Uses interval+value data format, suitable for statistical distributions
- Symbol customization: Configurable display style for each interval column
- Baseline configuration: Supports custom baseline position
Basic Concepts¶
Difference Between Histogram and Bar Chart¶
| Feature | Bar Chart | Histogram |
|---|---|---|
| Data structure | Point values (x, y) | Interval values ([x1, x2], y) |
| Bar width | Fixed or adjustable | Determined by interval |
| Use case | Categorical data comparison | Continuous data distribution |
| X-axis meaning | Category labels | Numeric intervals |
Histogram Styles¶
| Style | Enum Value | Description |
|---|---|---|
| Outline | Outline |
Draw overall outline with fill |
| Columns | Columns |
Draw independent column for each interval (default) |
| Lines | Lines |
Draw horizontal line for each interval |
Data Structure¶
Histograms use QwtIntervalSample to represent data:
1 2 3 4 | |
Usage¶
1. Basic Histogram¶
The QwtPlotHistogram constructor takes a title string, and setStyle() selects the drawing mode (Outline / Columns / Lines, defaulting to Columns). Data is fed as a list of QwtIntervalSample, each pairing a QwtInterval range with a value; setPen() and setBrush() then control the column border and fill. The example below builds a plot, fills four bins, and attaches the histogram:
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 | |
2. Setting Data¶
setSamples() is overloaded: it accepts either a QVector<QwtIntervalSample> of interval/value samples, or a QwtSeriesData<QwtIntervalSample>* for custom or dynamically generated data sources. The snippet below shows both forms:
1 2 3 4 5 6 7 8 9 10 11 12 | |
3. Display Styles¶
Columns Style¶
This is the most commonly used style, with each interval displayed as an independent column:
1 2 3 | |
Outline Style¶
Draws the overall outline, treating all intervals as a single entity:
1 2 3 4 5 | |
Warning — Outline Style Requirements
Outline style requires the following conditions:
1. Intervals must be in ascending order
2. Intervals must not overlap
3. Preferably no gaps between intervals
Lines Style¶
Draws only the value line for each interval:
1 2 3 | |
4. Column Symbol Configuration¶
In Columns style, setSymbol() replaces the plain pen/brush rectangle with a QwtColumnSymbol. The symbol's constructor takes a Style (here Box), setFrameStyle() picks the frame look (NoFrame / Plain / Raised), and the symbol's own setPen() / setBrush() control its border and fill. The example below creates a box symbol with a vertical gradient fill and assigns it to the histogram:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
5. Baseline Settings¶
setBaseline() defines the reference value from which each column is drawn; the default is 0.0. The snippet below resets it explicitly and shows how a negative baseline can be used to emphasize positive intervals:
1 2 3 4 5 | |
6. Statistical Histogram Example¶
Compute and draw a histogram from raw data:
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 | |
Core Methods Summary¶
| Method | Description |
|---|---|
setSamples() |
Set interval data |
setStyle() |
Set display style |
setPen() |
Set line pen |
setBrush() |
Set fill brush |
setSymbol() |
Set column symbol |
setBaseline() |
Set baseline position |
data() |
Get data object |
boundingRect() |
Get bounding rectangle |
QwtInterval Class¶
QwtInterval represents a numeric interval:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Tip — Histogram Application Scenarios
- Statistical data distribution frequency
- Image brightness distribution analysis
- Discretized display of continuous variables
Example — Related Examples
- Histogram demo: Created in conjunction with custom examples