Menu

(no subject)

gw1500se
2014-08-21
2014-08-22
  • gw1500se

    gw1500se - 2014-08-21

    I've created a normal distribution curve (data-data) and need to label the tick marks on the x-axis. I have the ticks (7) where they need to be. Now I need to label them as:

    -3σ
    -2σ

    0
    σ

    I can't seem to figure that out from the documentation. TIA.

    Rats! How do I add a subject line now?

     

    Last edit: gw1500se 2014-08-21
  • lbayuk

    lbayuk - 2014-08-22

    This is not as easy as one would think. The reason is that the data labels (the first value in each row of the data array) label the data points, but the tick marks are at uniform values that do not necessarily correspond to the data points. So in most cases, if you turn on data labels and tick marks, you will not get data labels under the tick marks along the axis.

    If you do happen to have data points at those specific X values (sigma * -3 through sigma * 3), you could use data labels, but that is pretty restrictive. So here is how to do it in the more general case.

    You make a custom label formatting function for the X tick labels and tell PHPlot to use it with SetXLabelType('custom', ...). In that function, you can transform the tick labels however you want, including appending the sigma character. You also have to set up the tick marks to appear where you want them (you did say you already did that).

    Here is a complete script that shows what I mean. The custom format function (my_labels) makes the labels the way you show them above, but it probably needs work for arbitrary sigma so it won't suffer from round-off error. ("Left as an exercise to the reader", as they say.) Also you may need to select a specific TrueType font, which is needed to get any special characters, although on my system it works as shown.

    <?php
    # Produce 7 X tick marks at multiples of sigma with labels.
    require_once 'phplot.php';
    
    // Anything should work here:
    $sigma = 2;
    
    // Data array for testing:
    $data = array(
      array('', -4, 1),
      array('', -2, 2),
      array('', -1, 3),
      array('',  0, 5),
      array('',  1, 3),
      array('',  2, 2),
      array('',  4, 1),
    );
    
    // Label formatting function:
    function my_labels($label, $sigma)
    {
      // Note: This should deal with round-off...
      $factor = $label / $sigma;
    
      // Special cases for -1 and 1 (omit the 1) and 0 (omit the sigma).
      switch ($factor) {
      case 0:    return "0";
      case 1:    return "&#963;"; // 963 = UTF-16 for lower case sigma
      case -1:   return "-&#963;";
      default:   return $factor . "&#963;";
      }
    }
    
    $plot=new PHPlot;
    // You must use a TrueType font in order to get the 'sigma' character.
    // On some systems, you need to select a TrueType font by path instead of this:
    $plot->SetUseTTF(True);
    
    $plot->SetDataValues($data);
    $plot->SetDataType('data-data');
    $plot->SetPlotType('lines');
    
    // Set the custom label formatting function:
    $plot->SetXLabelType('custom', 'my_labels', $sigma);
    
    // Set the X axis range and tick marks:
    $plot->SetPlotAreaWorld(-3.5 * $sigma, NULL, 3.5 * $sigma, NULL);
    $plot->SetXTickIncrement($sigma);
    $plot->SetXTickAnchor(0);
    
    $plot->SetXTitle('Standard Deviation');
    $plot->DrawGraph();
    

    P.S. I tried but apparently even an admin can't change the subject of a thread.

     
  • gw1500se

    gw1500se - 2014-08-22

    Thanks. I didn't have to calculate tick marks as this code worked out correctly:

    $plot->SetXTickAnchor(0);
    $plot->SetNumXTicks(7);
    

    For a normal distribution curve the ticks will always be the same (what I originally posted) regardless of the data. I can simplify what you suggested.

     

    Last edit: gw1500se 2014-08-22
  • gw1500se

    gw1500se - 2014-08-22

    Now that I've played with this a bit and think I understand what is happening, how do I integrate this into my data? None of the data points correspond to a tick mark. So how do I tell in the callback when it is a tick mark and how do I get the callback to run in the first place? With my current data it only runs once for each of the 2 lines I am plotting.

     
  • gw1500se

    gw1500se - 2014-08-22

    Never mind. I think I got it. Thanks.

     

Log in to post a comment.