为极地散点图数据点添加颜色

3
我正在尝试使用Python制作极坐标图,到目前为止我有些成功了。以下是我希望得到一些想法/建议的问题:
1. 是否可以将圆圈的颜色设置为特定值(例如:下面示例代码中的“n”)?如果可以,我可以设置特定的颜色范围吗?例如:0-30:红色,31-40:黄色;41-60:绿色。
注意:参考自Plot with conditional colors based on values in R的示例,我尝试使用ax.scatter(ra,dec,c = ifelse(n < 30,’red','green'), pch = 19 )但没有成功=(
2. 如何使数据圆圈变大一点?
3. 我可以移动“90”标签,以便图形标题不重叠吗?我尝试过:x.set_rlabel_position(-22.5)但出现错误("AttributeError:'PolarAxes'对象没有属性'set_rlabel_position'")
4. 是否可能仅显示0、30和60高度标签?是否可以将它们水平定向(例如:沿着0方位线)?
谢谢您!期待听到您的建议=)
import numpy
import matplotlib.pyplot as pyplot

dec = [10,20,30,40,50,60,70,80,90,80,70,60,50,40,30,20,10]
ra = [225,225,225,225,225,225,225,225,225,45,45,45,45,45,45,45,45]
n = [20,23,36,43,47,48,49,50,51,50,48,46,44,36,30,24,21]

ra = [x/180.0*3.141593 for x in ra]
fig = pyplot.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax.set_ylim(0,90)
ax.set_yticks(numpy.arange(0,90,10))
ax.scatter(ra,dec,c ='r')
ax.set_title("Graph Title here", va='bottom')
pyplot.show()
2个回答

3

1. 给点着色
使用scatter的 c 参数可以给点着色。在这种情况下,您可以提供n数组,并根据颜色映射选择颜色。颜色映射将由不同的颜色组成(31次红色,10次黄色,20次绿色)。 使用颜色映射的优点是可以轻松使用颜色条。

2. 让圆变大可以使用s 参数。

3. 在标签和标题之间添加空格最好通过向上移动标题来实现,使用set_title y 参数。为了使标题不超出图形,我们可以使用subplots_adjust 方法并使top 稍小一些。(请注意,仅在通过子图创建轴时才起作用。)

4. 仅显示某些刻度可以通过设置刻度为ax.set_yticks([0,30,60])并使y轴标签沿水平线排列来完成,方法是ax.set_rlabel_position(0)。(请注意,如果您拥有早期版本,则set_rlabel_position 在1.4版后可用,请考虑更新。)

enter image description here

import numpy as np
import matplotlib.pyplot as plt # don't use pylab
import matplotlib.colors
import matplotlib.cm

dec = [10,20,30,40,50,60,70,80,90,80,70,60,50,40,30,20,10]
ra = [225,225,225,225,225,225,225,225,225,45,45,45,45,45,45,45,45]
n = [20,23,36,43,47,48,49,50,51,50,48,46,44,36,30,24,21]

ra = [x/180.0*np.pi for x in ra]
fig = plt.figure()
ax = fig.add_subplot(111,polar=True)
ax.set_ylim(0,90)

# 4. only show 0,30, 60 ticks
ax.set_yticks([0,30,60])
# 4. orient ylabels along horizontal line
ax.set_rlabel_position(0)

# 1. prepare cmap and norm
colors= ["red"] * 31 + ["gold"] * 10 + ["limegreen"] * 20
cmap=matplotlib.colors.ListedColormap(colors)
norm = matplotlib.colors.Normalize(vmin=0, vmax=60)   
# 2. make circles bigger, using `s` argument
# 1. set different colors according to `n`
sc = ax.scatter(ra,dec,c =n, s=49, cmap=cmap, norm=norm, zorder=2)

# 1. make colorbar
cax = fig.add_axes([0.8,0.1,0.01,0.2])
fig.colorbar(sc, cax=cax, label="n", ticks=[0,30,40,60])
# 3. move title upwards, then adjust top spacing
ax.set_title("Graph Title here", va='bottom', y=1.1)
plt.subplots_adjust(top=0.8)

plt.show()

非常感谢您提供如此详细的解释!这对我非常有帮助 =) - luke
在查看代码时,我意识到我错误地读取了“圆形”的值。实际上这些值应该是相反的(例如:中心应该是90度,最外层的圆形应该是0度),因为它们代表高度角(当观察者位于图表中心时)。唔...有没有办法纠正这个错误呢? - luke
只是一个快速的更新:我学会了如何改变图表的方向,使“0”指向正上方(即:北)。我刚刚添加了:ax.set_theta_zero_location('N')和ax.set_theta_direction(-1),这就解决了问题 =) - luke
明白了!!! =) 解决方案如下:
  1. 更改 ylim 的顺序: ax.set_ylim(90,0)
  2. 对每个 dec 值减去 90: dec[:] = [90 - x for x in dec]
完美解决 =)再次感谢大家的帮助!!
- luke

1

对于颜色,您可以使用c=[每个元素的颜色列表],对于大小s=,就像这样:

ax.scatter(ra,dec,c =['r' if a < 31 else 'yellow' if a < 41 else 'green' for a in n], s =40)

对于标题和轴,我建议更改标题的位置:

ax.set_title("Graph Title here", va='bottom', y=1.08)

您可能需要调整图形的大小以正确显示标题:

fig = pyplot.figure(figsize=(7,8))

为了显示勾选框,您可以使用:

for label in ax.yaxis.get_ticklabels():
    label.set_visible(False)
for label in ax.yaxis.get_ticklabels()[::3]:
    label.set_visible(True)

总体而言:
import numpy
import matplotlib.pyplot as pyplot

dec = [10,20,30,40,50,60,70,80,90,80,70,60,50,40,30,20,10]
ra = [225,225,225,225,225,225,225,225,225,45,45,45,45,45,45,45,45]
n = [20,23,36,43,47,48,49,50,51,50,48,46,44,36,30,24,21]

ra = [x/180.0*3.141593 for x in ra]
fig = pyplot.figure(figsize=(7,8))
ax = fig.add_axes([0.1,0.1,0.8,0.8],polar=True)
ax.set_ylim(0,90)
ax.set_yticks(numpy.arange(0,90,10))

ax.scatter(ra,dec,c =['r' if a < 31 else 'yellow' if a < 41 else 'green' for a in n], s =40)
ax.set_title("Graph Title here", va='bottom', y=1.08)
for label in ax.yaxis.get_ticklabels():
    label.set_visible(False)
for label in ax.yaxis.get_ticklabels()[::3]:
    label.set_visible(True)
pyplot.show()

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