I want to plot scatter points over a continuous line. The colors I chose have some value of alpha<1. When I draw the point on top of the line, the result becomes more opaque (which is expected). Here's a picture:

And here's the relevant source code:
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")
The result I desire would look somewhat like point D on the line without the line being more opaque (as it is the case for C).
With the help of scleronomic's comment and this link I found the solution:
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")
I had to interpolate the color and change the zorder such that the scatter point is in the foreground.

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With