Numpy Matplotlib对数雷达图-删除0.5以下的所有值并显示最后一个ytick
如果您查看下面的对数雷达图,如果有人知道正确的编码方式,我希望有两个更改: 1) 显示最大值(51.81)的ytick标签,因为它当前给出的最大值为31.62 2) 一种将所有值设置为0.1到0以下的方法,不会导致被零除的错误Numpy Matplotlib对数雷达图-删除0.5以下的所有值并显示最后一个ytick,numpy,matplotlib,logarithm,radar-chart,Numpy,Matplotlib,Logarithm,Radar Chart,如果您查看下面的对数雷达图,如果有人知道正确的编码方式,我希望有两个更改: 1) 显示最大值(51.81)的ytick标签,因为它当前给出的最大值为31.62 2) 一种将所有值设置为0.1到0以下的方法,不会导致被零除的错误 fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, polar=True) np.seterr(divide = 'warn') sample = samplelistmalshare get_mag
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111, polar=True)
np.seterr(divide = 'warn')
sample = samplelistmalshare
get_mag = lambda x: 10**min(np.floor(np.log10(x)))
init_mag = get_mag(sample)
print("init_mag")
print(init_mag)
print("gm")
print(get_mag)
sample = np.array(sample) / get_mag(sample)
N = len(sample)
theta = np.arange(0, 2 * np.pi, 2 * np.pi / N)
bars = ax.bar(theta, np.log10(sample), width=0.4, color = '#003F5C')
ax.set_xticks(theta)
ax.set_xticklabels([' Delayed\n Execution', ' File\n Opening', 'Firewall\nModification', 'Permission \nModification ', 'Persistence ', 'Proxied \nExecution ', 'Reconnaissance ', ' Registry\n Modification', ' Task\n Stopping'], visible=False)
dat = np.log10(sample)
print(max(dat))
#exit()
ax.set_ylim(0,max(dat))
ax.xaxis.grid(False)
ax.yaxis.grid(True)
precision = 2 # Change to your desired decimal precision
ax.set_yticklabels([str(round((10 ** x) * init_mag, precision)) for x in ax.get_yticks()])
for test in ax.get_yticks():
print(test)
for test in ax.get_ymajorticklabels():
print(test)
ax.set_rlabel_position(50)
plt.savefig('radarchartingmalshare.pdf',bbox_inches='tight')
fig.clf()
plt.clf()
一种解决方案是手动设置
yticks
和yticklebels
right_end = 51.81
ax.set_ylim(0,np.log10(right_end / init_mag))
y_ticks = np.linspace(0,np.log10(right_end/init_mag),10)
ax.set_yticks(y_ticks)
y_ticklabels = ['%.2f' % (init_mag*10**x) if x !=0 else '0.00' for x in ax.get_yticks()]
ax.set_yticklabels(y_ticklabels)
使用此选项可以手动设置刻度和标签
import numpy as np
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(8, 8));
ax = fig.add_subplot(111, polar=True)
np.seterr(divide = 'warn')
sample = [35.417256011315416,0.028288543140028287,1.3578500707213579,3.3663366336633667,
0.8203677510608205,35.445544554455445,3.3946251768033946,19.46251768033946,0.7072135785007072,]
get_mag = lambda x: 10**min(np.floor(np.log10(x)))
init_mag = get_mag(sample)
sample = np.array(sample) / get_mag(sample)
dat = np.log10(sample)
N = len(sample)
theta = np.arange(0, 2 * np.pi, 2 * np.pi / N)
bars = ax.bar(theta, dat, width=0.4, color = 'deepskyblue')
ax.set_xticks(theta)
ax.xaxis.grid(False)
right_end = 51.81
ax.set_ylim(0,np.log10(right_end / init_mag))
ax.yaxis.grid(True)
y_ticks = np.linspace(0,np.log10(right_end/init_mag),10)
ax.set_yticks(y_ticks)
y_ticklabels = ['%.2f' % (init_mag*10**x) if x !=0 else '0.00' for x in ax.get_yticks()]
ax.set_yticklabels(y_ticklabels)
ax.tick_params(axis='y',colors='darkviolet')
plt.show()
似乎很合理,谢谢。@dipl0不客气:)