【问题标题】:How to fill a circle with a gradient?如何用渐变填充圆形?
【发布时间】:2021-01-14 22:52:09
【问题描述】:

有没有办法填充由 :

创建的圆圈
ax.add_patch(ptc.Circle(.....)

使用颜色图的垂直渐变:

grad = cm.get_cmap('plasma', 100)

预期输出:

我不知道怎么做,但是根据图片有人用imshow()得到它。

谢谢

【问题讨论】:

    标签: python matplotlib gradient patch imshow


    【解决方案1】:

    您可以绘制图像并设置剪辑路径:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x, y, r = 0, 35, 25
    fig, ax = plt.subplots()
    img = ax.imshow(np.linspace(0, 1, 256).reshape(-1, 1), cmap='plasma',
                    extent=[x - r, x + r, y - r, y + r], origin='lower')
    circle = plt.Circle((x, y), r, transform=ax.transData)
    img.set_clip_path(circle)
    ax.use_sticky_edges = False
    ax.margins(x=0.05, y=0.05)
    plt.show()
    

    【讨论】: