Menu

#2841 Incorrect autoscale regression in polar plots

None
open
nobody
None
3 days ago
2025-12-03
dima
No

Hello. This used to work with earlier gnuplot (last year maybe), but has a bug in 6.0.3 and in the most recent git.

This Python script shows the problem:

import numpy as np
import gnuplotlib as gp
N = 1000
rho = np.linspace(0, 2*np.pi, N)
gp.plot( rho,
         1./np.cos(rho) + 2.*np.cos(rho),
         _with  = 'lines',
         set    = 'polar',
         square = True,
         yrange = [-5,5],
         ascii = True,
         wait = 1)

The resulting gnuplot script is attached. Note that the yrange is [-5,5] as requested. The autoscaled xrange is [-300,300], This is incorrect: the data has x in [0,3]. Earlier gnuplot sets a reasonable xrange. For whatever reason, changing the N in the script above (sampling the data more coarsely) produces a tighter xrange. Something inside gnuplot is looking at the polar angle, and using it to scale x, due to a likely bug.

Thanks

1 Attachments

Discussion

  • Ethan Merritt

    Ethan Merritt - 2025-12-03

    The range of values in the 2nd column of the data is [-635.987: 212.005] . Since it is read in polar mode, this is r rather than y. After transformation into Cartesian x/y values this gives a range [0.00000:212.005] which autoscaling extends to the next rtick value. This translates to a range [-250:250] on both x and y, which is what you would see in the plot if both x and y were both autoscaled. But the script expicitly sets yrange [-5:5] and set size ratio -1 so most of the y values are out of range.

    I think the script really needs to say set rrange [-5:5] rather than set yrange [-5:5].

    And/or the python front end is producing mangled data values.

     
  • dima

    dima - 2025-12-04

    Hello. This plot is autoscaled on the x (where the data appears in a very small range), but the yrange is locked to [-5:5]. The data outside this yrange shouldn't affect this, and it didn't previously. I just did a bisection. 5.4 worked properly, 5.5 does not. The specific commit that broke it is: https://github.com/gnuplot/gnuplot/commit/6d3c518a5bdd0da34d7e7ba40e88d7e7576c41b2

     
  • Ethan Merritt

    Ethan Merritt - 2025-12-04

    Please look at your input script again. You have told the program to use polar coordinates. The input data values run from zero to 2π (1st column of data) which is perfectly normal for polar coordinates. The input data in the second column, the polar radius r, runs from -635.987 to 212. If you want to limit the data on input you can set the limit on. So set rrange rather than set yrange.

    The yrange limits what is drawn on the plot, not what is read in from the input file.

    I see identical results with version 5.4.10 and the current development version. If you are seeing a difference with something earlier in the 5.4 series it may be due to the command set size ratio -1. The interpretation of that final -1 did change slightly between versions 5 and 6 since version 6 introduced a separate command set isotropic to achieve uniform scaling on x and y. I don't know exactly what older gnuplot version you are comparing to, but I think that replacing these two commands:

    set yrange [-5:5]
    set size ratio -1
    

    with

    set autoscale noextend
    set rrange [0:5]
    

    will generate the same plot from both the old and current gnuplot versions. Note, however, that in this case all the negative r values are discarded so the lobe to the left of the origin is not shown. I don't know whether that is what you want or not. If you want to show the lobe to the left, modify this to

    set autoscale noextend
    set rrange [-5:5]
    unset clip
    

    Hope that helps,
    Ethan

     

    Last edit: Ethan Merritt 2025-12-04
  • Ethan Merritt

    Ethan Merritt - 4 days ago
    • status: open --> closed-not-a-bug
    • Group: -->
    • Priority: -->
     
  • dima

    dima - 4 days ago

    Hold on! I've been busy, and haven't had time to reply.

    I've read your comments multiple times, and I'm sure it makes sense to you, but it doesn't make sense to me.

    First, the regression. I check out the "5.4" git tag, and I see the old behavior. The breaking commit is linked above. I cannot say if it does the right thing.

    Next, the intent was not to limit the input (as rrange would do) but to limit the output (as yrange should do). The autoscaling logic, as I always understood it, should select the pan/zoom of the plot such that the data covers the whole plot window. For the purposes of evaluating "the data covering the window" it should be looking at (r cos(th), r sin(th)). Otherwise I don't know what the autoscaler is even trying to achieve.

    Along those lines, I would expect a polar plot of (th,r) to produce the same exact output as a non-polar plot of (r cos(th), r sin(th)), at least as far as the autoscaler is concerned. Does it make sense for the autoscaler to ever look at r without th?

    Looking at the generated plot a bit more... is it trying to fit the r axis into the plot? it sees that the maximum r has a certain value, and it then tries to make sure a full circle of radius r can be plotted? Even if there is no data there for all theta? If that's the case, I would consider this a bug: the autoscaler should only be concerned with the data being plotted. For the record, I just tried to unset raxis and rtics, and it didn't change anything.

    "set isotropic" is what I want, but I also want to be compatible with older gnuplots, so I use "set size ratio -1"

    Thank you for discussing this!

     
    • Ethan Merritt

      Ethan Merritt - 4 days ago

      Oops, sorry. I'll re-open the issue. But I am still failing to see a bug here.

      Looking at the generated plot a bit more... is it trying to fit the r axis into the plot? it sees that the maximum r has a certain value, and it then tries to make sure a full circle of radius r can be plotted?

      Yes. That.

      Even if there is no data there for all theta? If that's the case, I would consider this a bug: the autoscaler should only be concerned with the data being plotted.

      It scales to accommodate the data being plotted. That is why I keep saying that the problem is that the script should be filtering the data on r. set rrange [-5:5].

      The set yrange command is not filtering the data; it is limiting the area of the plot.

      Along those lines, I would expect a polar plot of (th,r) to produce the same exact output as a non-polar plot of (r cos(th), r sin(th)), at least as far as the autoscaler is concerned.

      Almost. In a non-polar plot you really are reading in x and y values. So the input y values and the plotted y values are the same, and setting yrange to [-foo:+foo] will limit the vertical extent of the plot. But it will continue to accept data point near the 45°/135° diagonals that where |r| exceeds foo so long as |r sin(θ)| < foo. So in that regard setting yrange in a non-polar plot is different from setting rrange in a polar plot.

       
  • Ethan Merritt

    Ethan Merritt - 4 days ago
    • status: closed-not-a-bug --> open
     
  • dima

    dima - 3 days ago

    Oh. OK. I understand what it's doing now, but I guess it just feels like a very strange thing to be doing. The output window is xy. The autoscaler untimately decides on xrange and yrange. So the xy data is what it should be using to make that decision. It sounds like it makes sense to you, so then yeah, there's no bug. Thanks for explaining.

     

Log in to post a comment.