Subversion Repositories cheapmusic

Rev

Blame | Last modification | View Log | RSS feed

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>5.50. Example - Box Plot with Data Reduction</title><link rel="stylesheet" type="text/css" href="phplotdoc.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.78.1" /><link rel="home" href="index.html" title="PHPlot Reference Manual" /><link rel="up" href="examples.html" title="Chapter 5. PHPlot Examples" /><link rel="prev" href="ex-horizerror.html" title="5.49. Example - Horizontal Error Plot" /><link rel="next" href="ex-boxplot2.html" title="5.51. Example - Box Plot with Outliers and Styles" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">5.50. Example - Box Plot with Data Reduction</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ex-horizerror.html">Prev</a> </td><th width="60%" align="center">Chapter 5. PHPlot Examples</th><td width="20%" align="right"> <a accesskey="n" href="ex-boxplot2.html">Next</a></td></tr></table><hr /></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="ex-boxplot1"></a>5.50. Example - Box Plot with Data Reduction</h2></div></div></div><p>
This is a box plot (plot type <a class="link" href="conc-plottypes.html#plottype-boxes" title="3.4.3. Plot Type: boxes (Box Plot)">boxes</a>),
without outliers.
This example also shows how a PHP function might be used to create a PHPlot
data array from experimental results. The input data is a series of lists
of test results, and the output is a PHPlot data array for a box plot, with
rows in the form <code class="literal">(label, Ymin, YQ1, Ymid, YQ3, Ymax)</code>.
</p><p>
Note: Box plots were added in PHPlot-6.1.0.
</p><div class="example"><a id="example-boxplot1"></a><p class="title"><strong>Example 5.50. Box Plot with Data Reduction</strong></p><div class="example-contents"><div class="informalfigure"><div class="mediaobject"><img src="examples/boxplot1.png" alt="Box Plot with Data Reduction Example" /></div></div><pre class="programlisting">&lt;?php
# PHPlot Example - Box Plot (without outliers)
require_once 'phplot.php';

# The experimental results:
$results = array(
    'Material A1' =&gt;
            array(11.1, 8.4, 11.9, 13, 10.2, 9.2),
    'Material A2' =&gt;
            array(10.6, 9.8, 9.1, 12, 8.5, 7.1, 8.2, 7.8, 11.8),
    'Material B1' =&gt;
            array(9.9, 11.2, 10.9, 12.9, 8.5),
    'Material C1' =&gt;
            array(10.1, 11.8, 11.6, 8.5, 9.6, 12, 12, 8.9, 12.7, 10.3, 11.1),
    'Material C2' =&gt;
            array(10.9, 11.9, 7.3, 11.7, 12.6, 9.2, 10, 7, 7.1),
    'Material C3' =&gt;
            array(11.9, 9.1, 10.5, 10.7, 10, 7.3, 10.1, 11.7, 10.4, 9),
    'Material D1' =&gt;
            array(12, 8, 9.8, 11.4, 10.5, 12.5, 7.1, 8.6, 7.6, 7.4, 8.2, 7.1),
    'Material E1' =&gt;
            array(10.6, 8.2, 8.2, 7.9, 12, 7, 9.3),
);

# Return the k-th percentile value of a sorted array, where 0 &lt;= $k &lt;= 1
# If there is no single value, return the average of the two adjacent ones.
# Caution: Don't copy this code. It may not be valid, but suits the example.
function kpercentile($row, $k)
{
    $n = count($row);
    $p = $k * ($n - 1);
    if ($p == (int)($p)) return $row[$p];
    return ($row[(int)$p] + $row[(int)($p+1)]) / 2;
}

# Convert the experimental results to a PHPlot data array.
function reduce_data($results)
{
    $data = array();
    foreach ($results as $label =&gt; $row) {
        $n_samples = count($row);
        sort($row);
        $data[] = array("$label\n($n_samples Samples)",
                         $row[0],                  // Minimum
                         kpercentile($row, 0.25),  // Q1 = 25%
                         kpercentile($row, 0.50),  // Median
                         kpercentile($row, 0.75),  // Q3 = 75%
                         $row[$n_samples-1]);      // Maximum
    }
    return $data;
}

$plot = new PHPlot(800, 600);
$plot-&gt;SetTitle('Box Plot (without outliers)');
$plot-&gt;SetDataType('text-data');
$plot-&gt;SetDataValues(reduce_data($results));
$plot-&gt;SetPlotType('boxes');
# These 2 lines make tick marks line up with text-data data points:
$plot-&gt;SetXTickIncrement(1);
$plot-&gt;SetXTickAnchor(0.5);
$plot-&gt;SetImageBorderType('plain'); // Improves presentation in the manual
$plot-&gt;DrawGraph();
</pre></div></div><br class="example-break" /></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ex-horizerror.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="examples.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ex-boxplot2.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">5.49. Example - Horizontal Error Plot </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 5.51. Example - Box Plot with Outliers and Styles</td></tr></table></div></body></html>