From: Loic E. <loi...@in...> - 2014-11-19 14:47:04
|
Dear all, I am trying to create a colormap with a single color (red in the example below) where the alpha varies from 0 to 1. It does look like I am getting some grayish color near the low alpha values (around alpha = 0.2). Is that expected somehow? The plot I get is here: https://cloud.githubusercontent.com/assets/1680079/5084457/7d2d3790-6f06-11e4-9021-5b9e77e6a9c4.png I am using matplotlib 1.4.2. Here is a snippet which reproduces the issue. import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap import matplotlib matplotlib.rcParams['figure.facecolor'] = 'white' cm_dict = {'red': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } my_cm = LinearSegmentedColormap('my_cm', cm_dict) vals = np.tile(np.linspace(-1, 1, 30), (20, 1)) fig = plt.figure() ax = plt.imshow(vals, cmap=my_cm) plt.colorbar() plt.show() Cheers, Loïc |
From: Benjamin R. <ben...@ou...> - 2014-11-19 15:20:50
|
Confirmed. I am going to wager that this might be related to some of the work that is being done right now in master to improve alpha handling, particularly with images. Notice that the colormap looks fine for the colorbar because it isn't using imshow() under the hood. First, if you could try using the development version of matplotlib, we have updated the AGG codebase. Maybe that might make an improvement off the bat. If not, you can follow this PR that is a work in progress that might be relevant: https://github.com/matplotlib/matplotlib/pull/3783 I hope this helps! Ben Root On Wed, Nov 19, 2014 at 9:46 AM, Loic Esteve <loi...@in...> wrote: > Dear all, > > I am trying to create a colormap with a single color (red in the example > below) where the alpha varies from 0 to 1. It does look like I am getting > some grayish color near the low alpha values (around alpha = 0.2). Is that > expected somehow? > > The plot I get is here: > > https://cloud.githubusercontent.com/assets/1680079/5084457/7d2d3790-6f06-11e4-9021-5b9e77e6a9c4.png > > I am using matplotlib 1.4.2. > > Here is a snippet which reproduces the issue. > > import numpy as np > > import matplotlib.pyplot as plt > from matplotlib.colors import LinearSegmentedColormap > > import matplotlib > > matplotlib.rcParams['figure.facecolor'] = 'white' > > cm_dict = {'red': ((0.0, 1.0, 1.0), > (1.0, 1.0, 1.0)), > 'green': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'blue': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'alpha': ((0.0, 0.0, 0.0), > (1.0, 1.0, 1.0)) > } > > my_cm = LinearSegmentedColormap('my_cm', cm_dict) > > vals = np.tile(np.linspace(-1, 1, 30), (20, 1)) > > fig = plt.figure() > ax = plt.imshow(vals, cmap=my_cm) > plt.colorbar() > plt.show() > > Cheers, > Loïc > > > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > > http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > > |
From: Gael V. <gae...@no...> - 2014-11-19 15:24:06
|
On Wed, Nov 19, 2014 at 10:20:23AM -0500, Benjamin Root wrote: > Notice that the colormap looks fine for the colorbar because it > isn't using imshow() under the hood. As a short-term workaround (I work with Loic, and I it would help me a lot if his problem was solved with a hack), can we leverage the mechanism used to plot the colorbar ourselves? That doesn't preclude fixing the problem in master, of course. Cheers, Gaël |
From: Jody K. <jk...@uv...> - 2014-11-19 15:28:01
|
Did you try pcolormesh? Cheers, Jody > On Nov 19, 2014, at 7:23 AM, Gael Varoquaux <gae...@no...> wrote: > > On Wed, Nov 19, 2014 at 10:20:23AM -0500, Benjamin Root wrote: >> Notice that the colormap looks fine for the colorbar because it >> isn't using imshow() under the hood. > > As a short-term workaround (I work with Loic, and I it would help me a > lot if his problem was solved with a hack), can we leverage the mechanism > used to plot the colorbar ourselves? > > That doesn't preclude fixing the problem in master, of course. > > Cheers, > > Gaël > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users |
From: Benjamin R. <ben...@ou...> - 2014-11-19 15:28:33
|
The underlying mechanism is a pcolormesh(). The key thing to remember when going back and forth between imshow and pcolors is that coordinates for imshow refer to the center of the pixel, while coordinates for pcolors refer to the corners, IIRC (I might have that backwards). Cheers! Ben Root On Wed, Nov 19, 2014 at 10:23 AM, Gael Varoquaux < gae...@no...> wrote: > On Wed, Nov 19, 2014 at 10:20:23AM -0500, Benjamin Root wrote: > > Notice that the colormap looks fine for the colorbar because it > > isn't using imshow() under the hood. > > As a short-term workaround (I work with Loic, and I it would help me a > lot if his problem was solved with a hack), can we leverage the mechanism > used to plot the colorbar ourselves? > > That doesn't preclude fixing the problem in master, of course. > > Cheers, > > Gaël > |
From: Loïc E. <loi...@in...> - 2014-11-19 17:57:33
Attachments:
alpha_colormap_pcolormesh.png
|
Thanks for the suggestions, I have tried the easiest one for now, namely pcolormesh, see attached plot. The alpha colormap look great but I can't seem to figure out how to prevent the edges of the cells from being visible. I tried using edgecolors='none' to no avail. I guess retrospectively that is similar to the lines we see in the colormap on the right. The snippet I am using: import numpy as np import matplotlib.pyplot as plt from matplotlib.colors import LinearSegmentedColormap import matplotlib matplotlib.rcParams['figure.facecolor'] = 'white' cm_dict = {'red': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)), 'green': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'blue': ((0.0, 0.0, 0.0), (1.0, 0.0, 0.0)), 'alpha': ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)) } my_cm = LinearSegmentedColormap('my_cm', cm_dict) vals = np.tile(np.linspace(-1, 1, 30), (20, 1)) fig = plt.figure() ax = plt.pcolormesh(vals, cmap=my_cm) plt.colorbar() plt.show() Cheers, Loïc |
From: Benjamin R. <ben...@ou...> - 2014-11-19 18:04:16
|
What you are seeing is the fact that the adjacent cells share the same coordinates, so neighboring cells overlap by one pixel. This is only visible when alpha != 1. This is a tricky issue to solve, but I could have sworn we made some progress on that front by setting "snap" to False somewhere. There have been past discussions about it, for sure... Ben Root On Wed, Nov 19, 2014 at 12:57 PM, Loïc Estève <loi...@in...> wrote: > Thanks for the suggestions, I have tried the easiest one for now, namely > pcolormesh, see attached plot. The alpha colormap look great but I can't > seem to figure out how to prevent the edges of the cells from being > visible. I tried using edgecolors='none' to no avail. I guess > retrospectively that is similar to the lines we see in the colormap on the > right. > > The snippet I am using: > > import numpy as np > > import matplotlib.pyplot as plt > from matplotlib.colors import LinearSegmentedColormap > > import matplotlib > > matplotlib.rcParams['figure.facecolor'] = 'white' > > cm_dict = {'red': ((0.0, 1.0, 1.0), > (1.0, 1.0, 1.0)), > 'green': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'blue': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'alpha': ((0.0, 0.0, 0.0), > (1.0, 1.0, 1.0)) > } > > my_cm = LinearSegmentedColormap('my_cm', cm_dict) > > vals = np.tile(np.linspace(-1, 1, 30), (20, 1)) > > fig = plt.figure() > ax = plt.pcolormesh(vals, cmap=my_cm) > plt.colorbar() > plt.show() > > Cheers, > Loïc > |
From: Eric F. <ef...@ha...> - 2014-11-20 20:14:35
|
On 2014/11/19, 1:03 PM, Benjamin Root wrote: > What you are seeing is the fact that the adjacent cells share the same > coordinates, so neighboring cells overlap by one pixel. This is only > visible when alpha != 1. This is a tricky issue to solve, but I could > have sworn we made some progress on that front by setting "snap" to > False somewhere. There have been past discussions about it, for sure... I don't think we ever made any progress; it seems like a problem with the renderer itself, agg in this case, and one that differs from one renderer to another (e.g., if the plot is saved as pdf and then rendered by different libraries). Try turning off antialiasing. Eric > > Ben Root > > On Wed, Nov 19, 2014 at 12:57 PM, Loïc Estève <loi...@in... > <mailto:loi...@in...>> wrote: > > Thanks for the suggestions, I have tried the easiest one for now, > namely pcolormesh, see attached plot. The alpha colormap look great > but I can't seem to figure out how to prevent the edges of the cells > from being visible. I tried using edgecolors='none' to no avail. I > guess retrospectively that is similar to the lines we see in the > colormap on the right. > > The snippet I am using: > > import numpy as np > > import matplotlib.pyplot as plt > from matplotlib.colors import LinearSegmentedColormap > > import matplotlib > > matplotlib.rcParams['figure.__facecolor'] = 'white' > > cm_dict = {'red': ((0.0, 1.0, 1.0), > (1.0, 1.0, 1.0)), > 'green': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'blue': ((0.0, 0.0, 0.0), > (1.0, 0.0, 0.0)), > 'alpha': ((0.0, 0.0, 0.0), > (1.0, 1.0, 1.0)) > } > > my_cm = LinearSegmentedColormap('my___cm', cm_dict) > > vals = np.tile(np.linspace(-1, 1, 30), (20, 1)) > > fig = plt.figure() > ax = plt.pcolormesh(vals, cmap=my_cm) > plt.colorbar() > plt.show() > > Cheers, > Loïc > > > > > ------------------------------------------------------------------------------ > Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server > from Actuate! Instantly Supercharge Your Business Reports and Dashboards > with Interactivity, Sharing, Native Excel Exports, App Integration & more > Get technology previously reserved for billion-dollar corporations, FREE > http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk > > > > _______________________________________________ > Matplotlib-users mailing list > Mat...@li... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > |