Python 使用透明标记但非透明边缘绘图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23596575/
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-19 03:13:26  来源:igfitidea点击:

Plotting with a transparent marker but non-transparent edge

pythonnumpymatplotlibplottransparency

提问by IanRoberts

I'm trying to make a plot in matplotlib with transparent markers which have a fixed color edge . However, I can't seem to achieve a marker with transparent fill.

我正在尝试在 matplotlib 中绘制一个带有固定颜色边缘的透明标记的图。但是,我似乎无法实现带有透明填充的标记。

I have a minimum working example here:

我在这里有一个最低限度的工作示例:

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(10)
y1 = 2*x + 1
y2 = 3*x - 5

plt.plot(x,y1, 'o-', lw=6, ms=14)
plt.plot(x,y2, 'o', ms=14, markerfacecolor=None, alpha=0.5, markeredgecolor='red', markeredgewidth=5)

plt.show()

I tried two techniques I found online to achieve this: 1) Setting alpha parameter. However, this makes the marker edge transparent too, which is not the desired effect. 2) Setting markerfacecolor=None, although this has no effect on my plot

我尝试了两种我在网上找到的技术来实现这一点:1)设置 alpha 参数。但是,这也会使标记边缘变得透明,这不是预期的效果。2)设置markerfacecolor=None,虽然这对我的情节没有影响

Is there a solution to this please?

请问这个有解决办法吗?

采纳答案by Saullo G. P. Castro

This is tricky in Matplotlib... you have to use a string "None"instead of the value None, then you can just do:

这在 Matplotlib 中很棘手……您必须使用 string"None"而不是 value None,然后您可以这样做:

plt.plot(x,y2, 'o', ms=14, markerfacecolor="None",
         markeredgecolor='red', markeredgewidth=5)

回答by Robin De Schepper

In general it seems to be a better solution to use transparent colors, instead of the alphaparameter. First of all because it does not affect any other colors and it helps to avoid the black fill bug reported by some in the comments. In this example - using the voxelsfunction to draw 2 voxels on the plot - the 4th number in the tuples stored in colorsrepresents the alpha value of an RGBA color. These normalized RGBA tuple notations can be used as colors throughout matplotlib.

一般来说,使用透明颜色而不是alpha参数似乎是更好的解决方案。首先,因为它不会影响任何其他颜色,并且有助于避免评论中某些人报告的黑色填充错误。在此示例中 - 使用该voxels函数在图上绘制 2 个体素 - 存储在的元组中的第 4 个数字colors表示 RGBA 颜色的 alpha 值。这些标准化的 RGBA 元组符号可以用作整个 matplotlib 的颜色。

import matplotlib.pyplot as plt, numpy as np, mpl_toolkits.mplot3d
fig = plt.figure()
ax = fig.gca(projection='3d')
voxels = np.array([[[True],[True]]])
colors = np.array([[[(0., 0., 0., 0.)],[(1.0, 0., 0., 0.5)]]])
ax.voxels(voxels, facecolors=colors, edgecolor='k', linewidth=.5)
plt.show(block=True)

enter image description here

在此处输入图片说明