python雷达图怎么做_雷达图Matplotlib Python:如何设置标签对齐

1586010002-jmsa.png

I am trying to align labels of my radar plot with axis and I face difficulties.

Without label alignment, my labels are centered and exceed the axis line CrX7S.png.

When I set for the first half of my labels (the right side of the figure) alignment to left and for the second half the alignment to the right I get a non "circular" alignment YBv2T.png

To do so I looped within xticklabels and set horizontal alignment.

# Draw one axe per variable + add labels labels yet

plt.xticks(angles, categories)

for label,i in zip(ax.get_xticklabels(),range(0,len(angles))):

if i

angle_text=angles[i]*(-180/pi)+90

#label.set_horizontalalignment('left')

else:

angle_text=angles[i]*(-180/pi)-90

#label.set_horizontalalignment('right')

label.set_rotation(angle_text)

I guess there is a proper way to do this and I can't figure it out, as I don't know if it should consider an offset, a translation or how to adapt the polar coordinate to do so.

Thanks for your help

Paul

Here follows the full code for more information

from math import pi

import matplotlib.pyplot as plt

%matplotlib inline

import pandas as pd

## Generate Data

labels_test=[]

for i in range(0,40):

labels_test.append("Fooooooooo"+str(i))

pd_radar=pd.DataFrame(data=np.random.randint(low=0, high=10, size=(2, 40)),columns=labels_test)

# number of variable

categories=list(pd_radar)

N = len(categories)

# 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)]

# Initialise the spider plot

fig=plt.figure(figsize=(20,10))

ax = plt.subplot(111, polar=True)

# If you want the first axis to be on top:

ax.set_theta_offset(pi / 2)

ax.set_theta_direction(-1)

# Draw one axe per variable + add labels labels yet

plt.xticks(angles, categories)

for label,i in zip(ax.get_xticklabels(),range(0,len(angles))):

if i

angle_text=angles[i]*(-180/pi)+90

label.set_horizontalalignment('left')

else:

angle_text=angles[i]*(-180/pi)-90

label.set_horizontalalignment('right')

label.set_rotation(angle_text)

# Draw ylabels

ax.set_rlabel_position(0)

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

# Plot each line of the data

# Ind1

values0=pd_radar.iloc[0].values.flatten().tolist()

ax.plot(angles, values0, linewidth=1, linestyle='solid', label="Label 1",color='yellow')

ax.fill(angles, values0, 'r', alpha=0.1,color='yellow')

# Ind2

values1=pd_radar.iloc[1].values.flatten().tolist()

ax.plot(angles, values1, linewidth=1, linestyle='solid', label="Label 2",color='deepskyblue')

ax.fill(angles, values1, 'r',color='deepskyblue', alpha=0.1)

# Add legend

plt.show()

解决方案

The code currently does not run with matplotlib 2.2. The following solution works however with version 2.0.2 that the OP is using. For a workaround using newer versions, see this question.

Texts in matplotlib have a rotation mode.

The rotation mode can be "default", in which case the text is first rotated and then aligned

NgZOa.png

or it can be "anchor", in which case the text is first aligned and then rotated.

sX28U.png

Therefore, in order to have the ticklabels pointing outwards on the polar plot, you would want to set the alignment of the text to "left", but then set the rotation mode to "anchor".

for label,rot in zip(ax.get_xticklabels(),ticks):

label.set_rotation(rot*180./np.pi)

label.set_horizontalalignment("left")

label.set_rotation_mode("anchor")

Full example:

import numpy as np

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(5,5))

ax = plt.subplot(111, polar=True)

ticks = np.linspace(0, 2*np.pi, 20, endpoint=False)

text = lambda : "".join(np.random.choice(list("manuladefil"), size=10))

labels = [text() for _ in range(len(ticks))]

plt.xticks(ticks, labels, size=16)

for label,rot in zip(ax.get_xticklabels(),ticks):

label.set_rotation(rot*180./np.pi)

label.set_horizontalalignment("left")

label.set_rotation_mode("anchor")

plt.tight_layout()

plt.show()

KJgWy.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值