在matplotlib中添加径向轴标签

9

我正在使用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()

我的图表看起来像这样。我似乎找不到直接的方法来做到这一点。有人以前处理过这个问题并有任何建议吗?

3个回答

5

我不知道有没有内置的方法来实现,但是你可以使用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()

以下是输出结果:

在这里输入图像描述

我很想看看是否有更优雅的解决方案,但希望这可以帮到您。


3
from pylab import *

N = 150
r = 2*rand(N)
theta = 2*pi*rand(N)
area = 200*r**2*rand(N)
colors = theta
ax = subplot(111, polar=True)
c = scatter(theta, r, c=colors, s=area, cmap=cm.hsv)
c.set_alpha(0.75)
ax.set_ylabel('Radius', rotation=45, size=11)

show()

enter image description here


0

这是一种与@tom稍有不同的方法。它直接使用了plt.legend选项。

示例:

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,label='Supernova')
ax.set_title("Spread of Abell Cluster Supernova Events as a Function of Fractional Radius", va='bottom')
ax.legend(loc='lower right', scatterpoints=1)
plt.show()

您可以将lower right更改为upper right,甚至更改为best,以保持图例在matplotlib中的对齐方式。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接