【问题标题】:Reversing the r-axis on a radar chart matplotlib在雷达图 matplotlib 上反转 r 轴
【发布时间】:2018-01-30 21:37:21
【问题描述】:

我正在尝试使用此代码创建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 评论下方所做的那样),数据看起来也更加偏斜,并且缺少所有内圈。有没有一种简单的方法可以让我只取法线图并反转轴?

【问题讨论】:

  • 您是否尝试过像这样反转您的角度列表:“angles.reverse()”?
  • 注意cell_lines 使代码无法重现。
  • 糟糕,我已对其进行了编辑,以便重现。

标签: python matplotlib plot


【解决方案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] 区间(如果不移动数据,则表示为 @987654323 的元组@ 可能超出了tab1 中的数据点范围。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多