Python 部分透明的散点图,但带有纯色条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4478725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:54:41  来源:igfitidea点击:

Partially transparent scatter plot, but with a solid color bar

pythonmatplotlibscattercolorbar

提问by Eric O Lebigot

In Python, with Matplotlib, how to simply do a scatter plot with transparency (alpha < 1), but with a color bar that represents their color value, but has alpha = 1?

在 Python 中,使用 Matplotlib,如何简单地绘制具有透明度(alpha < 1)的散点图,但使用表示其颜色值的颜色条,但 alpha = 1?

Here is what one gets, with from pylab import *; scatter(range(10), arange(0, 100, 10), c=range(10), alpha=0.2); color_bar = colorbar():

这是一个得到的,用from pylab import *; scatter(range(10), arange(0, 100, 10), c=range(10), alpha=0.2); color_bar = colorbar()

alt text

替代文字

How can the color bar be made non-transparent?

如何使颜色条不透明?

PS: I tried color_bar.set_alpha(1); draw(), but this did not do anything…

PS:我试过了color_bar.set_alpha(1); draw(),但这没有做任何事情......

采纳答案by Eric O Lebigot

Alright, I found one way to do it, that looks relatively clean: (using the ColorBarobject from the question)

好吧,我找到了一种方法,看起来比较干净:(使用ColorBar问题中的对象)

color_bar.set_alpha(1)
color_bar.draw_all()
# pylab.draw() or pyplot.draw() might be necessary

It would be great to get a confirmation that this is the most robust way to proceed, though! :)

不过,如果能确认这是最稳健的继续方式,那就太好了!:)

回答by Steve Tjoa

This is a huge, ugly hack. But no other way would work. Maybe someone else can improve.

这是一个巨大而丑陋的黑客。但没有其他方法行得通。也许其他人可以改进。

fig1 = pylab.figure()
fig2 = pylab.figure()
ax1 = fig1.add_subplot(111)
ax2 = fig2.add_subplot(111)
ax1.scatter(range(10), range(10), c=range(10), alpha=0.2)
im = ax2.scatter(range(10), range(10), c=range(10), alpha=1.0)
fig1.colorbar(im, ax=ax1)
fig1.show()

alt text

替代文字