98 |
- |
1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
|
|
|
2 |
<!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>
|
|
|
3 |
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>),
|
|
|
4 |
without outliers.
|
|
|
5 |
This example also shows how a PHP function might be used to create a PHPlot
|
|
|
6 |
data array from experimental results. The input data is a series of lists
|
|
|
7 |
of test results, and the output is a PHPlot data array for a box plot, with
|
|
|
8 |
rows in the form <code class="literal">(label, Ymin, YQ1, Ymid, YQ3, Ymax)</code>.
|
|
|
9 |
</p><p>
|
|
|
10 |
Note: Box plots were added in PHPlot-6.1.0.
|
|
|
11 |
</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"><?php
|
|
|
12 |
# PHPlot Example - Box Plot (without outliers)
|
|
|
13 |
require_once 'phplot.php';
|
|
|
14 |
|
|
|
15 |
# The experimental results:
|
|
|
16 |
$results = array(
|
|
|
17 |
'Material A1' =>
|
|
|
18 |
array(11.1, 8.4, 11.9, 13, 10.2, 9.2),
|
|
|
19 |
'Material A2' =>
|
|
|
20 |
array(10.6, 9.8, 9.1, 12, 8.5, 7.1, 8.2, 7.8, 11.8),
|
|
|
21 |
'Material B1' =>
|
|
|
22 |
array(9.9, 11.2, 10.9, 12.9, 8.5),
|
|
|
23 |
'Material C1' =>
|
|
|
24 |
array(10.1, 11.8, 11.6, 8.5, 9.6, 12, 12, 8.9, 12.7, 10.3, 11.1),
|
|
|
25 |
'Material C2' =>
|
|
|
26 |
array(10.9, 11.9, 7.3, 11.7, 12.6, 9.2, 10, 7, 7.1),
|
|
|
27 |
'Material C3' =>
|
|
|
28 |
array(11.9, 9.1, 10.5, 10.7, 10, 7.3, 10.1, 11.7, 10.4, 9),
|
|
|
29 |
'Material D1' =>
|
|
|
30 |
array(12, 8, 9.8, 11.4, 10.5, 12.5, 7.1, 8.6, 7.6, 7.4, 8.2, 7.1),
|
|
|
31 |
'Material E1' =>
|
|
|
32 |
array(10.6, 8.2, 8.2, 7.9, 12, 7, 9.3),
|
|
|
33 |
);
|
|
|
34 |
|
|
|
35 |
# Return the k-th percentile value of a sorted array, where 0 <= $k <= 1
|
|
|
36 |
# If there is no single value, return the average of the two adjacent ones.
|
|
|
37 |
# Caution: Don't copy this code. It may not be valid, but suits the example.
|
|
|
38 |
function kpercentile($row, $k)
|
|
|
39 |
{
|
|
|
40 |
$n = count($row);
|
|
|
41 |
$p = $k * ($n - 1);
|
|
|
42 |
if ($p == (int)($p)) return $row[$p];
|
|
|
43 |
return ($row[(int)$p] + $row[(int)($p+1)]) / 2;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
# Convert the experimental results to a PHPlot data array.
|
|
|
47 |
function reduce_data($results)
|
|
|
48 |
{
|
|
|
49 |
$data = array();
|
|
|
50 |
foreach ($results as $label => $row) {
|
|
|
51 |
$n_samples = count($row);
|
|
|
52 |
sort($row);
|
|
|
53 |
$data[] = array("$label\n($n_samples Samples)",
|
|
|
54 |
$row[0], // Minimum
|
|
|
55 |
kpercentile($row, 0.25), // Q1 = 25%
|
|
|
56 |
kpercentile($row, 0.50), // Median
|
|
|
57 |
kpercentile($row, 0.75), // Q3 = 75%
|
|
|
58 |
$row[$n_samples-1]); // Maximum
|
|
|
59 |
}
|
|
|
60 |
return $data;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
$plot = new PHPlot(800, 600);
|
|
|
64 |
$plot->SetTitle('Box Plot (without outliers)');
|
|
|
65 |
$plot->SetDataType('text-data');
|
|
|
66 |
$plot->SetDataValues(reduce_data($results));
|
|
|
67 |
$plot->SetPlotType('boxes');
|
|
|
68 |
# These 2 lines make tick marks line up with text-data data points:
|
|
|
69 |
$plot->SetXTickIncrement(1);
|
|
|
70 |
$plot->SetXTickAnchor(0.5);
|
|
|
71 |
$plot->SetImageBorderType('plain'); // Improves presentation in the manual
|
|
|
72 |
$plot->DrawGraph();
|
|
|
73 |
</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>
|