Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

python - Pyplot pcolormesh confused when alpha not 1

I am having some difficulty with pyplot's awesome drawing abilities. I have selected my very own colormap

n = 6
map = matplotlib.cm.get_cmap('Dark2')
cmap = colors.ListedColormap([(0,0,0,0)] + [[map(i * 1.0 / n)[j] for j in range(3)] + [0.2] for i in range(1, n + 1)])

This is basically just the Dark2 colormap, discretized to n (in my case 6) values with the zero value mapping to pure white. The main difference, however, is that the alpha values for my custom colormap are set to 0.2, not 1 as is default.

The problem is that when I plot something using this, like

plt.pcolormesh(np.random.rand(10,10), cmap = cmapInv)

the result is something like this:

Result

This looks nice enough, but you can clearly see that around each box, there is a very thin border of the same color as the box but with alpha set to 1.

EDIT: As suggested in the comments, the cause of these borders is probably overlap between the boxes.

Is there a way to clean this up?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As a minor workaround in the meantime, I found you can get the image closer to what you want by messing with the edgecolor and linewidth attributes. For example, using the following input to pcolormesh:

    plt.pcolormesh(np.random.rand(10,10), cmap = cmapInv, edgecolor=(1.0, 1.0, 1.0, 0.3), linewidth=0.0015625)

outputs the following image:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...