Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle 0s when plotting in log scale #563

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions extra_foam/gui/plot_widgets/tests/test_plot_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ def testCurvePlotItem(self, dtype):
item.setData(np.arange(2).astype(dtype), np.arange(3).astype(dtype))

# test log mode
plot_bottom_right = np.log10(float(x[-1]))
self._widget._plot_area._onLogXChanged(True)
if dtype == float:
_display()
assert item.boundingRect() == QRectF(0, 0, 1.0, 13.5)
assert item.boundingRect() == QRectF(0, 0, plot_bottom_right, 13.5)
self._widget._plot_area._onLogYChanged(True)
if dtype == float:
_display()
assert item.boundingRect().topLeft() == QPointF(0, 0)
assert item.boundingRect().bottomRight().x() == 1.0
assert item.boundingRect().bottomRight().x() == plot_bottom_right
assert 1.2 > item.boundingRect().bottomRight().y() > 1.1

# clear data
Expand Down
2 changes: 1 addition & 1 deletion extra_foam/gui/pyqtgraph/graphicsItems/GraphicsObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def toLogScale(arr, policy=None):
"""Convert array result to logarithmic scale."""
ret = np.nan_to_num(arr)
ret[ret < 0] = 0
return np.log10(ret + 1)
return np.log10(ret, where=(ret > 0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the original solution does not work in some situations, as you have shown. However, there are also corner cases for the new one. It makes 1 and 0 the same. 0 will not be affected while 1 will become 0 after the log operation. Imaging you have a step function f(x) = 1 if x > 0 else 0, with the new solution the curve will become completely flat (all 0). Therefore, a better solution might be letting users choose among different methods.


def name(self):
"""An identity of the PlotItem.
Expand Down