gpt4 book ai didi

python - Matplotlib 透明线上方的透明点

转载 作者:行者123 更新时间:2023-12-02 11:48:25 29 4
gpt4 key购买 nike

我想在连续线上绘制散点。我选择的颜色的值是 alpha<1 。当我在线上绘制点时,结果变得更加不透明(这是预期的)。这是一张图片:

enter image description here

这是相关的源代码:

import matplotlib.pyplot as plt
plt.style.use("default")
color = (0.4, 0.1, 0.9, 1.0)
color50 = (0.4, 0.1, 0.9, 0.5)

# A line
fig, ax = plt.subplots(1,1,figsize=(6,4))
ax.plot([1,2],[1,2], lw = 10, color =color50)

# Point A
ax.scatter(1.4,1.4, s = 500, color =color)
ax.text(1.38,1.47,"A",)

# Point B
ax.scatter(1.5,1.5, s = 500, color =color50,alpha = 1.0)
ax.text(1.48,1.57,"B",)

# Point C
ax.scatter(1.7,1.7, s = 500, color =color50)
ax.text(1.68,1.77,"C",)

# Point D
ax.scatter(1.7,1.3, s = 500, color =color50)
ax.text(1.68,1.37,"D")

我想要的结果看起来有点像点 D在线上,而不使线变得更加不透明(就像 C 的情况一样)。

最佳答案

在 scleronomic 的评论和 this 的帮助下链接我找到了解决方案:

import matplotlib.pyplot as plt
plt.style.use("default")
color = (0.4, 0.1, 0.9, 1.0)
color50 = (0.4, 0.1, 0.9, 0.5)

# A line
fig, ax = plt.subplots(1,1,figsize=(6,4))
ax.plot([1,2],[1,2], lw = 10, color =color50)

# Point A
ax.scatter(1.4,1.4, s = 500, color =color)
ax.text(1.38,1.47,"A",)

# Point B
ax.scatter(1.5,1.5, s = 500, color =color50,alpha = 1.0)
ax.text(1.48,1.57,"B",)

# Point C
ax.scatter(1.7,1.7, s = 500, color =color50,zorder=10)
ax.text(1.68,1.77,"C",)

# Point D
ax.scatter(1.7,1.3, s = 500, color =color50)
ax.text(1.68,1.37,"D")


# The solution based on
# https://stackoverflow.com/questions/25668828/how-to-create-colour-gradient-in-python
import matplotlib as mpl
import numpy as np

def colorFader(c1,c2,mix=0):
c1=np.array(mpl.colors.to_rgb(c1))
c2=np.array(mpl.colors.to_rgb(c2))
return mpl.colors.to_hex((1-mix)*c1 + mix*c2)

interpcolor = colorFader(color,(1.0,1.0,1.0),0.5)
# Point E
ax.scatter(1.8,1.8, s = 500, color =interpcolor)
ax.text(1.78,1.87,"E")

# Point F
ax.scatter(1.9,1.9, s = 500, color =interpcolor,zorder=3) # zorder>2
ax.text(1.88,1.97,"F")

我必须插入颜色更改zorder以使散点位于前景。 enter image description here

关于python - Matplotlib 透明线上方的透明点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60374042/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com