Python Matplotlib - 如何在不使线条透明的情况下使标记面颜色透明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15928539/
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
Matplotlib - How to make the marker face color transparent without making the line transparent
提问by Curious2learn
I know how to set the transparency of a line in matplotlib. For example, the following code makes the line and the markers transparent.
我知道如何在 matplotlib 中设置线条的透明度。例如,以下代码使线条和标记透明。
import numpy as np
import matplotlib.pyplot as plt
vec = np.random.uniform(0, 10, 50)
f = plt.figure(1)
ax = f.add_subplot(111)
ax.plot(vec, color='#999999', marker='s', alpha=0.5)
I want line's alpha=1.0, and marker's face color to be semi-transparent (alpha=0.5). Can this be done in matplotlib?
我希望线条的 alpha=1.0,并且标记的面部颜色是半透明的(alpha=0.5)。这可以在 matplotlib 中完成吗?
Thank you.
谢谢你。
采纳答案by tacaswell
See @Pelson's answer below for the correct way to do this with one line.
有关使用一行执行此操作的正确方法,请参阅下面的@Pelson 的答案。
You can do this in a hacky way by sticky taping together two independent Line2Dobjects.
您可以通过将两个独立的Line2D对象粘在一起,以一种非常巧妙的方式做到这一点。
th = np.linspace(0, 2 * np.pi, 64)
y = np.sin(th)
ax = plt.gca()
lin, = ax.plot(th, y, lw=5)
mark, = ax.plot(th, y, marker='o', alpha=.5, ms=10)
ax.legend([(lin, mark)], ['merged'])
plt.draw()


see herefor explanation
看这里解释
回答by pelson
After reading the source code of matplotlib.line, it turns out there is a code path (at least in Agg, but probably all backends) which allows you to do this. Whether this was ever intentional behaviour, I'm not sure, but it certainly works at the moment. The key is notto define an alpha value for the line, but to define the colours desired along with an alpha value:
阅读 的源代码后matplotlib.line,发现有一个代码路径(至少在 Agg 中,但可能是所有后端)允许您执行此操作。我不确定这是否是故意的行为,但目前确实有效。关键不是定义线条的 alpha 值,而是定义所需的颜色以及 alpha 值:
import matplotlib.pyplot as plt
plt.plot([0, 1], [1, 0], 'k')
# Do not set an alpha value here
l, = plt.plot(range(10), 'o-', lw=10, markersize=30)
l.set_markerfacecolor((1, 1, 0, 0.5))
l.set_color('blue')
plt.show()
These can probably be given as arguments in plt.plot, so just
这些可能可以作为 中的参数给出plt.plot,所以只需
plt.plot(range(10), 'bo-', markerfacecolor=(1, 1, 0, 0.5), )
will do the trick.
会做的伎俩。
HTH
HTH

