matplotlib 极坐标图上的渐变色背景

Gradient color background on matplotlib polar plot

我在 matplotlib 中绘制蜘蛛图,希望背景为颜色渐变,但我只能看到如何将背景设置为纯色。我想要径向渐变(例如 this)。有没有办法只写 set_facecolor('name_of_colormap')?

MWE:

import numpy as np
import matplotlib.pyplot as ply

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
r = [2, 1.5, 4, 3, 0.5, 2.5]
theta = [(2*np.pi)/6*i for i in range(6)]
ax.set_theta_zero_location("N") # Put 0 at the top
ax.set_rticks([])
ax.set_thetagrids([i*180/np.pi for i in theta[:-1]])
ax.set_theta_direction(-1)      # Make angles go clockwise
ax.set_xticklabels(['one', 'two', 'three', 'four', 'five', 'six'])
ax.set_facecolor("green")
ax.plot(theta, r, 'k')

您可以使用 ax.pcolormesh 来实现。请注意,在下面的示例中,我应用了循环颜色图,colors 定义了颜色映射到颜色图的方式。

import numpy as np
import matplotlib.pyplot as ply
import matplotlib.cm as cm

fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
r = [2, 1.5, 4, 3, 0.5, 2.5]
theta = [(2*np.pi)/6*i for i in range(6)]
ax.set_theta_zero_location("N") # Put 0 at the top
ax.set_rticks([])
ax.set_thetagrids([i*180/np.pi for i in theta[:-1]])
ax.set_theta_direction(-1)      # Make angles go clockwise
# ax.set_xticklabels(['one', 'two', 'three', 'four', 'five', 'six'])
N = 500j
rr, tt = np.mgrid[0:4:N, 0:2*np.pi:N]
colors = rr / rr.max()
ax.pcolormesh(tt, rr, colors, cmap=cm.hsv, shading="nearest")
ax.plot(theta, r, 'k')