Menu

Always Ensure Axes Cross At Zero

2011-02-16
2012-09-07
  • Jon Reynolds

    Jon Reynolds - 2011-02-16

    Hi all,

    I am just starting out with phplot (and php in general) and I am building a
    small application to build simple line charts.

    The information to be displayed is mostly similar to sinusoidal type line
    charts, but the values can vary.

    My charts will always display lines that pass from positive to negative in Y,
    just like a sin wave. How do I ensure that the Y-axis scale always has a zero
    in it? I tried playing with SetPlotAreaWorld but can only get it to work
    (always show zero) if the figures are all either positive or negative. If I
    set it to zero in there, then the other half of the graph is cut off.

    Any suggestions?

    Thanks for the great project.

    Jon

     
  • lbayuk

    lbayuk - 2011-02-16

    Hello, and welcome to PHPlot.

    If I understand correctly, you are asking how to make sure the value 0 is one
    of the values displayed at a tick mark along the Y axis.

    Left to its own, PHPlot will pick min and max values for the Y range based on
    your data, and it currently isn't very smart about it. Then it will divide
    that range into tick intervals (10 by default). So if you have negative, real
    Y values, it is likely that PHPlot will pick a range and tick interval which
    do not result in a tick mark at 0.

    So you need to give PHPlot some help. If possible, and if it makes sense for
    your data, use SetPlotAreaWorld() to specify a symmetric range for Y. For
    example: $plot->SetPlotAreaWorld(NULL, -10, NULL, 10); which sets the min Y
    and max Y (and leaves the X range to default). Whenever you have a symmetrical
    range, and an even number of tick intervals (default is 10), there will be a
    tick mark at Y=0.

    You can also get a tick mark at Y=0 with a non-symmetrical range, but you may
    have to set the tick increment too. For example, if Ymin=-4 and Ymax=20, using
    SetYTickIncrement(2) results in tick marks at even integers, including 0.

    I hope to see smarter range and tick increment calculation in PHPlot soon,
    which will make it much more likely to get Y=0 by default, but currently you
    have to give it some help with the data range and possibly tick increment.

     
  • Jon Reynolds

    Jon Reynolds - 2011-02-18

    Hi and thanks for the reply.

    OK, so is there a way I could 'detect' what the max and min values are from my
    array so I could work some sort of variable to become the Y limits in
    SetPlotAreaWorld?

    Can you use vars in SetPlotAreaWorld($var1,$var2,$var3,$var4);?

    Cheers,

    Jon

     
  • lbayuk

    lbayuk - 2011-02-22

    Sure, you could loop through your data array and find the min and max Y
    values. But that is what PHPlot already does. Using those as the min and max Y
    values for the plot area doesn't necessarily get you a tick at Y=0.

    In the ideal case, you have some "external" knowledge of the data range, and
    you use that to tell PHPlot what range to use. If you pick a symmetrical range
    (-Ymin = Ymax), or a lower limit Ymin which is evenly divisible by your tick
    increment, then you will get a tick mark at Y=0.

    If you don't know the data range in advance, you could try some tricks, like
    finding the maximum value and rounding it to a power of ten or something like
    that. (I have been working for some time on changes to PHPlot along those
    lines, using powers of 10 times 1, 2, or 5, but it doesn't do a uniformly good
    job.)

    Sorry this isn't really helping you. I've thought about it, and it makes a lot
    of sense to force PHPlot to have a tick mark at Y=0 for bipolar data. But it
    looks very hard to do, since PHPlot starts at the bottom, minimum Y, and
    counts tick marks up from there.

     
  • Jon Reynolds

    Jon Reynolds - 2011-02-22

    Hi,

    Thanks for your reply. I had basically come to the same conclusion as you
    independently anyway. I analyse my data and determine min and maximum values
    and with a bit of rounding dictate sensible boundaries for the Y axis.

    Thanks

    Jon Reynolds

     
  • lbayuk

    lbayuk - 2011-05-18

    After some more thought, I wrote up a feature request for this.

    There is another post on this forum ("request for moving vertical grid
    lines"), and I realized both of these are asking for the same thing. In your
    case, we just tell PHPlot to put a tick mark at Y=0, and all the ticks will
    move to keep the same interval but ensure there is one at Y=0.

    I already have something coded and it looks good, so unless it falls apart
    with further testing, it will be in the next release.

     
  • Jon Reynolds

    Jon Reynolds - 2011-06-22

    Ibayuk!

    Thank you for this! Works perfectly for me now.

    Thank you.

    Jon Reynolds

     

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.