0

I have what seems like a fairly simple question that likely has a more complex solution. How would I shade gradationally between two lines? For example. If I had two vertical lines at x=10 and x=20, how would I start with blue at x=10 and fade to white at x=20? I know for a solid colour I could just use fill between, but I do not know how to make it gradational.

Update:

So far I have the following working code

import matplotlib as plt

gradmax=20 # value where white starts
gradmin=10 # value where brown starts
grad_num=10 # how many vertical profiles I use, increase for smoothness
axstep=(gradmax-gradmin)/grad_num
alpha_max=0.5
alpha_min=0
alphastep=(alpha_max-alpha_min)/grad_num

fig = plt.figure()
ax=fig.add_subplot()

for i in range(grad_num):
    minplot = gradmin+i*axstep
    maxplot = gradmin+(i+1)*axstep
    alphaplot = alpha_max-alphastep*(i+1)
    ax.axvspan(minplot, maxplot, color='brown', edgecolor="None", alpha=alphaplot)

fig.show()

The problem I have is that there are vertical stripes where the lines overlap. Any ideas on how to resolve this? I already tried applying tiny increment of offset, but that didn't work.

js16
  • 43
  • 5

1 Answers1

1

There might be a better way to do this but here's something in the meantime:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(figsize=(3, 3))
ax.scatter(np.linspace(10, 20, 20000),
           np.random.rand(20000),
           c=np.linspace(10, 20, 20000),
           cmap='PuBu', marker='s', s=10)

enter image description here

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143