Menu

Plot data & show outlier with differ...

Tariq
2012-04-13
2012-09-07
  • Tariq

    Tariq - 2012-04-13

    Hello,

    I have plotted some data in a graph as point graph, with this following code:

    $graphdata = array();
    while($row = mysql_fetch_array($result))
    {
    $graphdata [] = array('',$row['Height'],$row['Speed']);
    }
    $plot = new PHPlot(800, 600);
    $plot->SetImageBorderType('plain');
    $plot->SetDataType('data-data');
    
    $plot->SetPlotType('points');
    $plot->SetDataValues($graphdata);
    
    $plot->SetTitle('Height vs Speed');
    
    $plot->SetXTickIncrement(1.0);
    $plot->SetXTickAnchor(0.5);
    
    $plot->SetPlotAreaPixels(NULL, 40, 780, NULL);
    $plot->SetCallback('draw_graph', NULL, NULL);
    
    $plot->DrawGraph();
    

    The plot is drawn nicely. Now, I want to show the outliers within this data
    points and show them in different color point. I have already detected which
    points are the outliers.

    Suppose, my data array from the above MySQL query is:

    Height Speed


    23 12

    24 23

    41 33

    18 22

    17 21

    5 21

    21 33

    I have taken Height in X-axis and Speed in Y axis and plotted them. Now, I got
    the Outliers by running another MySQL query and the Outliers are:

    Height Speed


    41 33

    5 21

    Now, how can I color differently (say, in red color) these 2 outliers on the
    graph which I have drawn before?

    Please let me know if you need more information about my problem statement.

    Any help will be appreciated.

     
  • lbayuk

    lbayuk - 2012-04-13

    I can think of 4 ways to do this: multiple data sets, a custom data color
    callback function, multiple plot overlay, or a plot annotation callback. The
    first 2 are probably best here, so that's what I will describe.

    1. Use multiple data sets (more than one Y value for each X, but only one of them will have an actual value). Each data set uses a different color, so by moving your outliers to a second data set, then will be plotted in the second color. For a normal point, a row in your data array would look like this: array('', $row, $row, ''). For an outlier, it would look like this: array('', $row, '', $row). You can extend this to more colors (but it gets a little awkward building the data array). Note PHPlot ignores the points with Y = ''.

    2. Use a Custom Data Color callback function. This is a PHP function you write that PHPlot will call when it needs a color for a data point. Your function then has complete control over the colors. You can use the standard color for your normal points, and a different color for your outliers.

    The PHPlot manual has a reference
    section
    and example on using this feature.

     
  • Tariq

    Tariq - 2012-04-13

    Okay, thanks. I will try them. If I face some difficulties, I will post it
    here.

     
  • Tariq

    Tariq - 2012-04-13

    I used the first approach and it worked perfectly. So many thanks for the
    tips.

    One more quick question: now the normal data points are plotted in blue color
    and the outliers are in green colors. Using this approach, is there any way to
    specify the color for outliers?

     
  • lbayuk

    lbayuk - 2012-04-14

    Of course. For example, $plot->SetDataColors(array('red', 'blue')) makes the
    first Y value set red, and the second blue.

     
  • Tariq

    Tariq - 2012-04-14

    Great! Thanks.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.