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

FIX set default imshow interpolation to nearest for composite.add_data #402

Merged
merged 1 commit into from
Oct 23, 2021

Conversation

mvdoc
Copy link
Contributor

@mvdoc mvdoc commented Oct 22, 2021

The default image interpolation in matplotlib is now antialiased. This can cause issues when plotting data containing NaNs with cortex.quickflat.make_figure. For example, when setting different dpi levels, the images can look wildly different. To fix this issue, this PR sets the default interpolation of the imshow in composite.add_data to nearest. (Thanks to @alexhuth for help debugging this issue!)

The following code demonstrates how antialiased interpolation can cause issues:

import cortex
import numpy as np
import matplotlib.pyplot as plt

vol = cortex.Volume.empty('S1', 'fullhead', vmin=0, vmax=2) + 1
vol.data[np.random.rand(*vol.data.shape) > 0.6] = np.nan

img = cortex.quickflat.make_flatmap_image(vol, thick=1, nanmean=False, height=800)

fig, ax = plt.subplots(1, 1)
ax.imshow(img[0])
fig.savefig("test-dpi-100-make_flatmap_image.png", dpi=100)
fig.savefig("test-dpi-300-make_flatmap_image.png", dpi=300)

test-dpi-100-make_flatmap_image
test-dpi-300-make_flatmap_image

Setting interpolation='nearest' fixes the problem:

import cortex
import numpy as np
import matplotlib.pyplot as plt

vol = cortex.Volume.empty('S1', 'fullhead', vmin=0, vmax=2) + 1
vol.data[np.random.rand(*vol.data.shape) > 0.6] = np.nan

img = cortex.quickflat.make_flatmap_image(vol, thick=1, nanmean=False, height=800)

fig, ax = plt.subplots(1, 1)
ax.imshow(img[0], interpolation="nearest")
fig.savefig("test-dpi-100-make_flatmap_image.png", dpi=100)
fig.savefig("test-dpi-300-make_flatmap_image.png", dpi=300)

test-dpi-300-make_flatmap_image-nearest
test-dpi-100-make_flatmap_image-nearest

@mvdoc mvdoc merged commit c01586b into gallantlab:main Oct 23, 2021
@mvdoc mvdoc deleted the fix/imshow_interp branch November 2, 2021 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants