Python:在两条垂直线之间渐变填充

时间:2020-03-12 17:53:17

标签: python matplotlib plot

我有一个似乎很简单的问题,可能有一个更复杂的解决方案。如何在两行之间渐变阴影?例如。如果我在x=10x=20处有两条垂直线,我该如何从x=10处的蓝色开始并在x=20处褪成白色?我知道我可以在中间使用填充色,但是我不知道如何使它渐变。

更新:

到目前为止,我有以下工作代码

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()

我的问题是线条重叠的地方有垂直条纹。有关如何解决此问题的任何想法?我已经尝试过应用微小的偏移量增量,但这没用。

1 个答案:

答案 0 :(得分:1)

也许有更好的方法可以做到这一点,但是与此同时:

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