Generation of sample points for log-scale plots

47 views
Skip to first unread message

Blair Mason

unread,
Apr 16, 2020, 4:21:42 AM4/16/20
to sage-devel
Hello all,

Should generate_plot_points() be modified to log-sample on plots where the x scale is logarithmic? I'd be willing to write the patch up but I'd like to know that it would be desired behavior before I do so.

I've been using sage to graph the frequency responses of a couple of filters.  While I was generating the plots, the results sage generated by default were not correct - sage had picked too few sample points.  Increasing the number of points by an order of magnitude worked, but it significantly slowed generation of the figures, which was undesirable as I was looking to make them interactive.  Restating the function in terms of x <- 2^x and changing the axes appropriately solved the problem.

I think plot() should 'just work' the way one would intuitively expect.  However, I don't know - there may be (/probably are) use cases where this would be far from expected behavior.

Code, to illustrate:

def mag2db(x):
   
return 20*log(x, 10)

def plot_tf(tf, cutoff, var, domain=(1, 1e6), thresh=1e-2, **kwargs):
    tf
= tf.substitute({var:var*i})
   
return plot_semilogx(mag2db(abs(tf)), (var, domain[0], domain[1]), base=2, **kwargs)

def plot_tf_log(tf, cutoff, var, domain=(1, 1e6), thresh=1e-2, **kwargs):
    tf
= tf.substitute({var:2^(var)*i})
   
return plot(mag2db(abs(tf)), (var, log(domain[0], 2), log(domain[1], 2)), **kwargs)
   
@interact
def _(w=slider(0, 20000, step_size=500, default=2000, label='$\\omega_c$'), R=slider(0, 5, default=1, step_size=0.2), C=slider(0, 10, default=1, step_size=0.2)):
    l
= L.substitute(omega_c=w, r=R, c=C)
    b
= B.substitute(omega_c=w, r=R, c=C)
    h
= H.substitute(omega_c=w, r=R, c=C)

    p
= plot_tf(l, w, s, color='blue')
    p
+= plot_tf(b, w, s, color='red')
    p
+= plot_tf(h, w, s, color='green')
    p
.show(ymin=-30, ymax=5)

    p2
= plot_tf_log(l, w, s, color='blue')
    p2
+= plot_tf_log(b, w, s, color='red')
    p2
+= plot_tf_log(h, w, s, color='green')
    p2
.show(ymin=-30, ymax=5)

   
print(l) # 4000000/(s^2 + 4000*s + 4000000)
   
print(b) # 2000*s/(s^2 + 4000*s + 4000000)
   
print(h) # s^2/(s^2 + 4000.00000000000*s + 4000000)

a.pngb.png

Thanks,


Blair Mason





Eric Gourgoulhon

unread,
Apr 16, 2020, 9:12:24 AM4/16/20
to sage-devel
Hi,


Le jeudi 16 avril 2020 06:21:42 UTC+2, Blair Mason a écrit :
Hello all,

Should generate_plot_points() be modified to log-sample on plots where the x scale is logarithmic? I'd be willing to write the patch up but I'd like to know that it would be desired behavior before I do so.


Yes please! Thanks for your initiative!
The current sampling is definitely annoying for log plots.

Best wishes,

Eric.

Samuel Lelievre

unread,
Apr 16, 2020, 10:10:03 AM4/16/20
to sage-devel


Thu 2020-04-16 09:12:24 UTC, Eric Gourgoulhon:

Le jeudi 16 avril 2020 06:21:42 UTC+2, Blair Mason:

Should generate_plot_points() be modified to log-sample
on plots where the x scale is logarithmic? I'd be willing to
write the patch up but I'd like to know that it would be
desired behavior before I do so.

Yes please! Thanks for your initiative!
The current sampling is definitely annoying for log plots.

+1 

Samuel Lelievre

unread,
Apr 19, 2020, 12:00:35 AM4/19/20
to sage-devel

Thu 2020-04-16 10:10:03 UTC, Samuel Lelievre:
For reference this is now:

- Sage Trac ticket 29523
  Sample points exponentially when plotting log-scale graphs

Thanks for opening it!

There is a long history of reports of user requests related to this,
including the following posts on sage-support and Ask Sage:

- sage-support, 2020-04
  Generation of sample points for log-scale plots

- sage-support, 2011-10
  plots with logarithmic axis?

- sage-support, 2013-11
  Log plots of points are blank when given small values
  
- sage-support, 2007-07
  Plotting semi-log graphs

- Plot points in loglog

- Plot boxes have jumpy xy ranges in log scale

- Changing vertical scale of log plot

- How to get log distribution on x-axis of semilogx plot?

- Plotting with logarithmic scaling for the 'x' axis?

- Creating a semilogx (or semilogy or loglog) plot

- Plotting a loglogplot

- Plotting problems

- How do I create a log plot of line data?

- Can sage compute the inverse of a function?

- How do I create a log plot of line data?

Reply all
Reply to author
Forward
0 new messages