在雷达图matplotlib上反转r轴 [英] Reversing the r-axis on a radar chart matplotlib

查看:80
本文介绍了在雷达图matplotlib上反转r轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码制作雷达图:

I am trying to make a radar chart using this code :

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 注释下面所做的那样),数据也只是看起来更加倾斜,并且缺少内圈在一起.有没有一种直接的方法可以让我只取法线图并反转轴?

With this code, I'd like to be able to reverse the r-axis so that the outer most circle is 0 and the inner most circle is 1. I have no idea how to proceed about doing this. Even when I multiplied the entire data set by -1 and then graphed it (as I did below the experiment comment), the data only looked more skewed and it was missing the inner circles all together. Is there a straightforward way in which I can just take the normal graph and reverse the axis?

推荐答案

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

A starting point following your approach should be:

#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] 间隔(如果您不移动数据,则表示为 x 可能不在 tab1 中的数据点范围内.)

with the "experiment" being a multiplication by -1 and the addition of 1 to shift back the values to the [0,1] interval (if you don't shift the data, the tuple that you indicated as x might be out of the range of the datapoints in tab1).

您可能还想查看改进Matplotlib 2.1中引入的极坐标图

这篇关于在雷达图matplotlib上反转r轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆