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

标签 python matplotlib

我想在连续线上绘制散点。我选择的颜色的值是 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/

相关文章:

python - 如何在 Tkinter 窗口中刷新 matplotlib 图?

python - 构建轮失败,当我尝试使用 pip 安装时

python - 我需要假设此代码中的 python 为空序列

Python,支持unicode的最佳方法?

python - python 3 是否有任何 linux 发行版?

python - 强制 matplotlib 修复绘图区域

python - 在 matplotlib 中合并两个已经存在的图(png 文件)

python - 如何缩放图像的一部分并在 matplotlib 的同一图中插入

python - 绘制 x 和 y 次轴时缺少一个次级标签

python - PyQt5 QLabel 超链接工具提示/悬停文本