Skip to content

pcolor vs pcolorfast: unexpected white edgecolors using RGBA-alike colormaps #10115

Closed

Description

@liubenyuan

Bug report

Bug summary

When using pcolor and pcolorfast to plot the same image using alpha channels of colormaps, and then savefig to a pdf file, there is evident white edges in the one saved by pcolor. However, pcolorfast has visually more appealing results.

Code for reproduction

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


def rgba_blues():
    """white -> blue, with alpha channel"""
    cdict = {'red': ((0.0, 0.0, 0.0),
                     (1.0, 0.0, 0.0)),
             'green': ((0.0, 0.0, 0.0),
                       (1.0, 0.0, 0.0)),
             'blue': ((0.0, 0.0, 0.0),
                      (0.25, 0.5, 0.5),  # enable blue
                      (1.0, 1.0, 1.0)),
             'alpha': ((0.0, 0.0, 0.0),
                       (0.25, 0.5, 0.5),
                       (1.0, 1.0, 0.0))}

    # c = mpl.colors.LinearSegmentedColormap.from_list("", ["white", "blue"])
    c = mpl.colors.LinearSegmentedColormap('blues', cdict)
    return c


# Linear x array for cell centers:
x = np.arange(32)
y = np.arange(32)
z = x[:, np.newaxis].dot(y[:, np.newaxis].transpose())

# plot using pcolor
cmap = rgba_blues()
fig, ax = plt.subplots()
ax.pcolor(x, y, z, cmap=cmap, edgecolors='none', antialiased=True)
plt.show()
fig.savefig('pcolor.pdf')

# plot using pcolorfast
cmap = rgba_blues()
fig, ax = plt.subplots()
ax.pcolorfast(x, y, z, cmap=cmap)
plt.show()
fig.savefig('pcolorfast.pdf')

Actual outcome

see pcolor.pdf and pcolorfast.pdf

Expected outcome

can we remove the unwanted edgecolors in pcolor when using the alpha channels of colormap?

BTW: the quadmesh edges are also in white color when using the alpha channels in pcolormesh. I think these edges should also be removed if we specify edgecolors='none'.

When using pcolormesh, what I get in pdf is not the same as in the qt backend.

cmap = rgba_blues()
fig, ax = plt.subplots()
ax.pcolormesh(x, y, z, cmap=cmap, shading='gouraud')
plt.show()

BTW: can we have a pcolor that supports interpolating features?

Matplotlib version

  • Operating system: win 7 x64
  • Matplotlib version: 2.1.1
  • Matplotlib backend: qt5
  • Python version: 3.6.3
  • Other libraries:

Activity

  1. efiring commented on Dec 27, 2017

    @efiring
    Member

    This is not a bug, it is a fundamental limitation of the rendering algorithm that we encounter in many contexts. I suggest turning anti-aliasing off. Some other points and answers:

    1. Please note that pcolor, pcolormesh (with one unfortunate exceptional case), and pcolorfast are all designed for filling regions specified by their edges. They are not intended for regions specified based on their centers.
    2. These functions have different strengths and weaknesses, and use quite different algorithms. Pcolor is slow and should be avoided unless its particular characteristics are required; in most cases, pcolormesh is a better substitute. Pcolorfast uses different algorithms, and returns different objects, depending on the characteristics of the boundary grid that is supplied. The fastest available algorithm is used. For your test case, that is actually the same algorithm as is used in imshow.
    3. When you display a pdf, the rendering is being done by a different engine than when you plot to the screen, so it is common for the boundaries between colored regions to be rendered differently.
    4. Pcolor is not intended or suitable for interpolation. For interpolation, use imshow.
  2. efiring commented on Dec 27, 2017

    @efiring
    Member

    @anntzer, I think you gave the best explanation of the edge rendering problem some time in the last few months in reply to another in the long series of issues about this. If you can easily find it, please insert a reference to it here. Thank you.

  3. jklymak commented on Dec 27, 2017

    @jklymak
    Member

    I did a little writeup based on what @anntzer said, though I see that I failed to include his links...

    https://github.com/jklymak/contourfIssues

  4. anntzer commented on Dec 27, 2017

    @anntzer
    Contributor
  5. liubenyuan commented on Dec 28, 2017

    @liubenyuan
    Author

    Thanks, I will use pcolorfast instead. imshow has the interpolation capability, however, we can not use imshow(X, Y, Z) with coordinates data where Z=f(X, Y)

  6. liubenyuan commented on Dec 28, 2017

    @liubenyuan
    Author

    @jklymak In your post, pdf rendering using alpha values is really a problem. The lines are more visible (seems like their alpha channels are doubled and they produce unwanted results).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions