在matplotlib中添加径向轴标签

Mat*_*t T 6 python matplotlib

我正在使用matplotlib制作一个大学项目的极地散点图,我无法找到如何在径向轴上添加标签.这是我的代码(我遗漏了数据,因为它是从csv中读出来的)

import matplotlib.pyplot as plt

ax = plt.subplot(111, polar=True)
ax.set_rmax(1)
c = plt.scatter(theta, radii)
ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom')
ax.legend(['Supernova'])
plt.show()
Run Code Online (Sandbox Code Playgroud)

(我的情节看起来像这样.我似乎无法找到任何直接的方法来做到这一点.有没有人以前处理过这个并有任何建议?

tmd*_*son 6

我不知道有什么内置方法可以做到这一点,但您可以使用ax.text自己的方法。您可以使用 获取径向刻度标签的位置ax.get_rlabel_position(),并使用 获取径向轴的中点ax.get_rmax()/2.

例如,这是您的代码(带有一些随机数据):

import matplotlib.pyplot as plt
import numpy as np

theta=np.random.rand(40)*np.pi*2.
radii=np.random.rand(40)

ax = plt.subplot(111, polar=True)
ax.set_rmax(1)
c = plt.scatter(theta, radii)
ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom')
ax.legend(['Supernova'])

label_position=ax.get_rlabel_position()
ax.text(np.radians(label_position+10),ax.get_rmax()/2.,'My label',
        rotation=label_position,ha='center',va='center')

plt.show()
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此输入图像描述

我很想看看是否有更优雅的解决方案,但希望这对您有所帮助。