Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix imshow edges #8300

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

Closed
wants to merge 3 commits into from
Closed

Conversation

tacaswell
Copy link
Member

attn @QuLogic

 - add a cleanup decorator out of paranoia
 - deepcopy rather than copy colormaps that we mutate
@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Mar 15, 2017
@tacaswell tacaswell added this to the 2.0.1 (next bug fix release) milestone Mar 15, 2017
Only mask out as 'bad' pixels which have alpha == 0
@tacaswell
Copy link
Member Author

This does not seem to fix the problem 😞

@tacaswell
Copy link
Member Author

This is trying to fix as issue brought up in the comments of #8024 which was in turn fixing #8012

@tacaswell tacaswell removed the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Apr 3, 2017
@tacaswell
Copy link
Member Author

@QuLogic We are going to unblock this for 2.0.1 to prevent the perfect from becoming the enemy of the good.

@QuLogic QuLogic modified the milestones: 2.0.1 (next bug fix release), 2.0.2 (next bug fix release) May 3, 2017
@QuLogic
Copy link
Member

QuLogic commented May 13, 2017

So I've had a chance to confirm that this PR does not correct the issue with Cartopy. Is there hope for pursuing an amendment to this PR or do you think a completely different approach is needed?

@tacaswell
Copy link
Member Author

I am not sure, still suspect that this is the right general direction, but it will depend on exactly what cartopy is feeding in (which I could not dig through the layers to sort out).

@QuLogic
Copy link
Member

QuLogic commented May 14, 2017

I extracted the data and outline path used by Cartopy and put it into a gist here that uses only Matplotlib.

Here's the blue image with 2.0.0:
result-blue
and with 2.0.1:
result-blue
I changed the background patch to red which you can see bleeding through a bit more obviously in the second image.

@tacaswell
Copy link
Member Author

On a bit deeper inspection I am not sure if this is something that can / should be fixed on the matplotlib side. Tweaking the example a bit I think the issue is that the data / mask are a few pixel too small.

Marking the bad pixels as pink and zooming in gives (ignoring the non-clipping of the patch)

so

and eroding the mask (because high is the pixels to exclude) by 2 gives

so

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import matplotlib.path as mpath
import os
import scipy.ndimage.morphology
import matplotlib.cm as mcm

plt.style.use('classic')
# os.chdir('/tmp/b8ebfd6e1bfc304d39487750bc2a48be')
data = np.load('cp-data.npz')
path = mpath.Path(vertices=data['verts'], codes=data['codes'],
                  _interpolation_steps=data['steps'], readonly=data['ro'])
imshow_kwargs = {
    'vmin': None,
    'vmax': None,
    'resample': None,
    'filterrad': 4.0,
    'alpha': None,
    'filternorm': 1,
    'aspect': None,
    'interpolation': 'none',
    'cmap': None,
    'data': None,
    'url': None,
    'shape': None,
    'extent': [-20037508.342789244, 20037508.342789244,
               -8683259.716434667, 8683259.716434667],
    'norm': None,
    'imlim': None,
    'origin': 'lower'
}

for color in ['fc', 'red', 'green', 'blue']:
    mask = data[color + '_mask']
    # mask = scipy.ndimage.morphology.binary_erosion(mark, iterations=2)
    img = np.ma.MaskedArray(data[color + '_data'], mask)

    fig, ax = plt.subplots(figsize=(10, 5))

    background = mpatches.PathPatch(path, edgecolor='none', facecolor='red',
                                    zorder=-1, clip_on=False,
                                    transform=ax.transData)
    ax.add_patch(background)

    outline = mpatches.PathPatch(path, edgecolor='black', facecolor='none',
                                 zorder=2.5, clip_on=False,
                                 transform=ax.transData)
    ax.add_patch(outline)

    # Not necessary?
    ax.patch = background

    if color != 'fc':
        cmap = mcm.get_cmap(color.title() + 's')
        cmap.set_bad('pink')
        imshow_kwargs['cmap'] = cmap
    else:
        imshow_kwargs['cmap'] = None
    ax.imshow(img, **imshow_kwargs)

    ax.set_title('%s %s' % (color, mpl.__version__))

    fig.savefig('result-' + color + '.png')

plt.show()

@QuLogic
Copy link
Member

QuLogic commented May 17, 2017

From what I can tell, Cartopy determines the mask from the points that don't transform back to a valid point in the source domain. But since this is imshow, I think that comes out of the central point and not the corners, so it doesn't know about edge pixels. I'm not sure how easy it would be to change this clipping.

@tacaswell tacaswell added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Jul 10, 2017
@tacaswell tacaswell modified the milestones: 2.0.3 (next bug fix release), 2.1 (next point release) Jul 10, 2017
@tacaswell tacaswell removed the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Aug 7, 2017
@tacaswell
Copy link
Member Author

Super-sceded by #8966

@tacaswell tacaswell closed this Aug 7, 2017
@tacaswell tacaswell deleted the fix_imshow_edges branch August 7, 2017 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants