Logarithmic Bug

185 views
Skip to first unread message

Mark Melvin

unread,
May 19, 2014, 12:36:20 PM5/19/14
to pyqt...@googlegroups.com
Hi There,

I am trying to use pyQtGraph for an audio analyzer type application that I am making.  First, let me say this package looks awesome!  However, I am having trouble getting log mode to work.  After tearing my hair out trying to get it to work programmatically, I noticed that there were a bunch of options built right into the GUI when I right-click.  However, even these fail to work.  Here is the linear plot I have:


Turning on log mode, results in this:


I get the same thing when trying to do this programmatically, even if I manually set the range using setRange(0,5).

Any help would be much appreciated.

Thanks,
Mark.

  

Mark Melvin

unread,
May 23, 2014, 5:21:54 PM5/23/14
to pyqt...@googlegroups.com
I've tried to simplify this down, and I have found that simply plotting 30 data points causes the same problem.  For this data (tuples of (x,y)):

[(0.0, 4.424511280376464e-05), (5.859375, 2.409510125289671e-05), (11.71875, 1.2739132216665894e-05), (17.578125, 1.2313
864317547996e-05), (23.4375, 7.784949048073031e-06), (29.296875, 5.878156116523314e-06), (35.15625, 6.562402631971054e-0
6), (41.015625, 3.419693939576973e-06), (46.875, 5.722461992263561e-06), (52.734375, 1.1855792763526551e-05), (58.59375,
 1.2042775779264048e-05), (64.453125, 6.911080618010601e-06), (70.3125, 8.628138857602607e-06), (76.171875, 1.1969464139
838237e-05), (82.03125, 5.889412022952456e-06), (87.890625, 2.6964873995893868e-06), (93.75, 5.772120857727714e-06), (99
.609375, 6.0245174609008245e-06), (105.46875, 5.021342076361179e-06), (111.328125, 5.184605470276438e-06), (117.1875, 4.
545432602753863e-06), (123.046875, 5.096236236568075e-06), (128.90625, 5.161716217116918e-06), (134.765625, 3.1326860607
80543e-06), (140.625, 4.866687049798202e-06), (146.484375, 7.188179097283864e-06), (152.34375, 4.7222083594533615e-06),
(158.203125, 3.7413619793369435e-06), (164.0625, 7.523839485656936e-06), (169.921875, 7.544745585619239e-06)]

If I plot it in linear scale it looks fine, but if I try to manually or programmatically transform the x axis to log mode, the axis range blows up to be from 1 to 1e+160.  So it looks like the axis is trying to scale its range to 10**(maximum of the actual data values), rather than log10 of the data values.

I have read elsewhere on this list that you need to set the range on the axis programmatically from 0 to log10(max_val), which I have tried doing, but it does not seem to change the behavior at all.  I can do this manually in the GUI using the menus, and indeed my scale changes properly and the data looks good.  But how can I do this programmatically?

My code looks like this at the moment:

    win = pg.GraphicsWindow(title="Frequency Response")

    # Enable antialiasing for prettier plots
    pg.setConfigOptions(antialias=True)

    p1 = win.addPlot(title="Frequency Response")
    p1.setLogMode(True, False)
    x_axis = p1.getAxis("bottom")
    x_axis.setRange(0, 5)
    p1.plot(x_data, y_data)


Any help would be appreciated.

Thanks,
Mark.

Luke Campagnola

unread,
May 30, 2014, 12:34:41 AM5/30/14
to pyqt...@googlegroups.com
On Fri, May 23, 2014 at 5:21 PM, Mark Melvin <mark....@gmail.com> wrote:
I've tried to simplify this down, and I have found that simply plotting 30 data points causes the same problem.  For this data (tuples of (x,y)):

[(0.0, 4.424511280376464e-05), (5.859375, 2.409510125289671e-05), (11.71875, 1.2739132216665894e-05), (17.578125, 1.2313

If you remove the first point (x=0), it should behave as expected (this is somewhat unsurprising since log(0) doesn't plot so well). Usually it would just print a warning about numerical errors and move on, but in this case it seems to throw off the automatic scaling. I'll look deeper and see if there is anything to be done about that.

I have read elsewhere on this list that you need to set the range on the axis programmatically from 0 to log10(max_val), which I have tried doing, but it does not seem to change the behavior at all.  I can do this manually in the GUI using the menus, and indeed my scale changes properly and the data looks good.  But how can I do this programmatically?

My code looks like this at the moment:

    win = pg.GraphicsWindow(title="Frequency Response")

    # Enable antialiasing for prettier plots
    pg.setConfigOptions(antialias=True)

    p1 = win.addPlot(title="Frequency Response")
    p1.setLogMode(True, False)
    x_axis = p1.getAxis("bottom")
    x_axis.setRange(0, 5)

Don't access the AxisItems directly--these automatically determine their range from the PlotItem. Better to use PlotItem.setRange:
http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange

In the case of your data above, using values like `p1.setXRange(0.8, 2.2)` works well when the plot is in log mode.

Mark Melvin

unread,
May 30, 2014, 7:45:15 AM5/30/14
to pyqt...@googlegroups.com
On Fri, May 30, 2014 at 12:34 AM, Luke Campagnola <luke.ca...@gmail.com> wrote:
On Fri, May 23, 2014 at 5:21 PM, Mark Melvin <mark....@gmail.com> wrote:
I've tried to simplify this down, and I have found that simply plotting 30 data points causes the same problem.  For this data (tuples of (x,y)):

[(0.0, 4.424511280376464e-05), (5.859375, 2.409510125289671e-05), (11.71875, 1.2739132216665894e-05), (17.578125, 1.2313

If you remove the first point (x=0), it should behave as expected (this is somewhat unsurprising since log(0) doesn't plot so well). Usually it would just print a warning about numerical errors and move on, but in this case it seems to throw off the automatic scaling. I'll look deeper and see if there is anything to be done about that.

Thanks.  I was sure I tried this (and forgot to remove the zero from my example), but I'll give it another go.
 

I have read elsewhere on this list that you need to set the range on the axis programmatically from 0 to log10(max_val), which I have tried doing, but it does not seem to change the behavior at all.  I can do this manually in the GUI using the menus, and indeed my scale changes properly and the data looks good.  But how can I do this programmatically?

My code looks like this at the moment:

    win = pg.GraphicsWindow(title="Frequency Response")

    # Enable antialiasing for prettier plots
    pg.setConfigOptions(antialias=True)

    p1 = win.addPlot(title="Frequency Response")
    p1.setLogMode(True, False)
    x_axis = p1.getAxis("bottom")
    x_axis.setRange(0, 5)

Don't access the AxisItems directly--these automatically determine their range from the PlotItem. Better to use PlotItem.setRange:
http://www.pyqtgraph.org/documentation/graphicsItems/viewbox.html#pyqtgraph.ViewBox.setRange

In the case of your data above, using values like `p1.setXRange(0.8, 2.2)` works well when the plot is in log mode.


Great - thanks a lot.

Reply all
Reply to author
Forward
0 new messages