在雷达图表上反转r轴matplotlib

时间:2018-01-30 21:37:22

标签: python matplotlib plot

我正在尝试使用此代码制作radar chart

import matplotlib.pyplot as plt
import numpy as np
import sys
import pandas as pd
from math import pi
import seaborn as sns
import cv2


tab1 = np.random.rand(21)

#Experiment
tab1 =[(-10)*x for x in tab1]

# ------- PART 1: Create background

# number of variable
cell_lines = range(0,22)
N = len(range)
# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:0]

# Initialise the spider plot
ax = plt.subplot(111, polar=True)

# If you want the first axis to be on top:
ax.set_theta_offset(pi / 6)
ax.set_theta_direction(-1)

# Draw one axe per variable + add labels labels yet

plt.xticks(angles[0:], cell_lines, fontsize=20,fontname="Arial",fontweight='bold',linespacing=0)
x=(3,6,9)
plt.yticks(x,fontsize=20, fontweight='bold',fontname="Arial",linespacing=0)

# Draw ylabels
ax.set_rlabel_position(0)
#plt.ylabel("%", fontsize=16)
#plt.title("STUFF", fontsize=20, fontweight='bold', linespacing=2)

# ------- PART 2: Add plots

# Plot each individual = each line of the data
# I don't do a loop, because plotting more than 3 groups makes the chart unreadable
#print(control)
# Ind1
#values=df.loc[0].drop('control').values.flatten().tolist()
#values += values[:1]
#ax.plot(angles, control, linewidth=5, linestyle='solid', label="control")
#ax.fill(angles, control, 'b', alpha=0.1)

ax.plot(angles, tab1, linewidth=5, linestyle='solid', label="tab1", color='blue')
ax.fill(angles, tab1, 'b', alpha=0.1)


# Add legend
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1), fontsize=20)
plt.show()

使用此代码,我希望能够反转r轴,使最外圈为0,最内圈为1.我不知道如何继续这样做。即使我将整个数据集乘以-1然后绘制它(就像我在experiment注释下面所做的那样),数据看起来更偏斜,并且它们一起错过了内圈。有一种简单的方法可以让我采用法线图并反转轴吗?

1 个答案:

答案 0 :(得分:1)

遵循您的方法的起点应该是:

#test with a more eye-friendly dataset
tab1 = np.linspace(0,1,11)

#Experiment
tab1 = -tab1 + 1

N = len(tab1)

angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:0]

# Initialise the spider plot
ax = plt.subplot(111, polar=True)

ax.set_theta_offset(pi / 6)
ax.set_theta_direction(-1)

plt.xticks(angles[0:], tab1)
r=(0.3, 0.6, 0.9)
plt.yticks(r)

ax.set_rlabel_position(0)
ax.plot(angles, tab1, linewidth=5, linestyle='solid', color='blue')

plt.show()

将“实验”乘以-1并添加1以将值移回[0,1]间隔(如果不移位数据,则​​表示为{{ 1}}可能超出x)中数据点的范围。

编辑:您可能还想查看improvements to the polar plot introduced in Matplotlib 2.1