This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Creating Box-and-Whiskers Charts#

Shows how to create a box-and-whiskers chart.


This is part of the Charts with Widgets Gallery example.

The example also shows how to read the non-continuous data from a file, arrange it and find medians needed for box-and-whiskers plotting.


To show the share deviation of two companies we start by creating two QBoxPlotSeries to handle monthly data.

acmeSeries = QBoxPlotSeries()
acmeSeries.setName("Acme Ltd")
boxWhiskSeries = QBoxPlotSeries()
boxWhiskSeries.setName("BoxWhisk Inc")

QFile class is used to open a text file where the non-continuous data is kept. The BoxDataReader is an auxiliary class for reading the text file and finding the extreme and median values from the data. The BoxDataReader is explained in more detail later. The method readBox reads the values and sets them to the QBoxSet item which the method returns for the caller. The returned QBoxSet item is added to the series.

acmeData = QFile(":boxplot_a")
errorTemplate = "Failed to load '%1' file."
if not acmeData.open(QIODevice.ReadOnly | QIODevice.Text):
    m_loadError = errorTemplate.arg(acmeData.fileName())
    return False

dataReader = BoxPlotDataReader(acmeData)
while not dataReader.atEnd():
    set = dataReader.readBox()
    if set:

In this section a second file is opened for reading the data for the second company.

boxwhiskData = QFile(":boxplot_b")
if not boxwhiskData.open(QIODevice.ReadOnly | QIODevice.Text):
    m_loadError = errorTemplate.arg(acmeData.fileName())
    return False

while not dataReader.atEnd():
    set = dataReader.readBox()
    if set:

In this code snippet a new QChart instance is created and previously created series are added to it. The title is also defined and animation is set to be SeriesAnimation.

chart = QChart()
chart.setTitle("Acme Ltd. and BoxWhisk Inc. share deviation in 2012")

Here we ask the chart to create default axes for our presentation. We also set the range for the vertical axis by querying the pointer for the axis from the chart, and then setting the min and max for that axis.


In this section we set the legends to be visible and place them at the bottom of the chart.


Finally, we add the chart onto a view. We also turn on the antialiasing for the chartView.


The chart is ready to be shown.

Here the method readBox is explained in detail.

First, a line is read from the file and lines starting with # are rejected since they are considered as comment lines.

line = m_textStream.readLine()
if line.startsWith("#"):
    return None

In this file the data is arranged as number, space, number, or space. On this snippet the line is split into single number strings which are stored on QStringList.

strList = line.split(' ', Qt.SkipEmptyParts)

The sortedList will hold the numbers in continuous order and in this code segment we show how to do it. First the sortedList is cleared and numbers are read from the strList and stored into sortedList in double format. The qSort method arranges the sortedList into continuous order starting from the smallest.

for i in range(1, strList.count()):
std::sort(m_sortedList.begin(), m_sortedList.end())

Below you will find a code sample showing how to select extremes and medians from the continuous data. Firstly a new QBoxSet is created. Lower and upper extremes are simple to select; they are just first and last items on the sortedList. For medians we use a helper method findMedian which is explained later. For the median from the upper half we need to adjust the begin number if the amount of the numbers is even or uneven. The end number for lower half comes naturally from int rounding.

box = QBoxSet(strList.first())
box.setValue(QBoxSet.LowerExtreme, m_sortedList.first())
box.setValue(QBoxSet.UpperExtreme, m_sortedList.last())
box.setValue(QBoxSet.Median, findMedian(0, count))
box.setValue(QBoxSet.LowerQuartile, findMedian(0, count / 2))
box.setValue(QBoxSet.UpperQuartile, findMedian(count / 2 + (count % 2), count))

Below you will find the code sample for the method findMedian. If the amount of numbers is uneven we select the number from the middle. For even amount numbers we take two numbers from the middle and calculate the mean value.

count = end - begin
if count % 2:
    return m_sortedList.at(count / 2 + begin)
    right = m_sortedList.at(count / 2 + begin)
    left = m_sortedList.at(count / 2 - 1 + begin)
    return (right + left) / 2.0