在 Matplotlib 雷达图上设置对数刻度

Set Log Scale on Matplotlib Radar Chart

我想知道如何使用对数的 yscale 绘制下图,因为 'sample' 列表中的数字之间存在巨大差异。

ax.set_yscale('log') 似乎只会导致错误。

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8,8))
ax = fig.add_subplot(111,polar=True)

sample = np.random.uniform(low=0.5, high=13.3, size=(15,))
sample = [35.417256011315416, 0.028288543140028287, 1.3578500707213579, 3.3663366336633667, 0.8203677510608205, 35.445544554455445, 3.3946251768033946, 19.46251768033946, 0.7072135785007072]

N = len(sample) 

theta = np.arange(0, 2*np.pi, 2*np.pi/N) 
bars = ax.bar(theta, sample, width=0.4)
#ax.set_yscale('log')
ax.set_xticks(theta)
ax.set_xticklabels(range(1, len(theta)+1))
ax.yaxis.grid(True)
plt.show()

对数据调用 np.log10 很简单。但这会生成位于该对数域中的刻度。您可以通过反转对数将它们转换回数据的原始域,即调用 10 ** i,其中 i 是新的刻度位置。关键是您只是在更新 tick label。刻度本身没有移动。

下面的代码应该可以解决问题:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, polar=True)

sample = np.random.uniform(low=0.5, high=13.3, size=(15,))
sample = [
    35.417256011315416,
    0.028288543140028287,
    1.3578500707213579,
    3.3663366336633667,
    0.8203677510608205,
    35.445544554455445,
    3.3946251768033946,
    19.46251768033946,
    0.7072135785007072,
]

N = len(sample)

theta = np.arange(0, 2 * np.pi, 2 * np.pi / N)
bars = ax.bar(theta, np.log10(sample), width=0.4)
ax.set_xticks(theta)
ax.set_xticklabels(range(1, len(theta) + 1))
ax.yaxis.grid(True)
precision = 2  # Change to your desired decimal precision
ax.set_yticklabels([str(round(10 ** x, precision)) for x in ax.get_yticks()])
plt.ioff()
plt.show()

这将生成下图。对数数据,带有正确的刻度标签。

只需使用 ax.set_yscale('symlog', linthresh=0.01)ax.set_rscale('symlog', linthresh=0.01)(在极坐标图中相同)而不是 ax.set_yscale('log')

一天前,我在 GitHub 上传了一个 jupyter-notebook 以获取更多详细信息。

Your radar chart with symlog