Python 向极坐标散点图数据点添加颜色
我正在尝试用Python制作一个极坐标图,到目前为止,我已经取得了一些成功 极坐标散点图示例 我确实有一些问题,我希望得到一些想法/建议:Python 向极坐标散点图数据点添加颜色,python,numpy,matplotlib,plot,scatter,Python,Numpy,Matplotlib,Plot,Scatter,我正在尝试用Python制作一个极坐标图,到目前为止,我已经取得了一些成功 极坐标散点图示例 我确实有一些问题,我希望得到一些想法/建议: 是否可以将圆圈的颜色设置为特定值(例如:下面的示例代码中的“n”)?如果是,我可以设置特定的颜色范围吗?例如:0-30:红色,31-40:黄色;41-60:绿色 注意:按照中的示例,我尝试了ax.scatter(ra,dec,c=ifelse(n
ax.scatter(ra,dec,c=ifelse(n<30,'red','green'),pch=19)
,但没有成功=(
x.set\rlabel\u position(-22.5)
但我得到一个错误(“AttributeError:'PolarAxes'对象没有属性'set\rlabel\u position'”)对于颜色,您可以使用c=[每个元素的颜色列表],对于大小s= 就像这里:
ax.scatter(ra,dec,c =['r' if a < 31 else 'yellow' if a < 41 else 'green' for a in n], s =40)
您可能需要调整图形的大小以正确显示标题:
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()
导入numpy
将matplotlib.pyplot导入为pyplot
dec=[10,20,30,40,50,60,70,80,90,80,70,60,50,40,30,20,10]
ra=[22525225,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,适用于ra中的x]
fig=pyplot.图(figsize=(7,8))
ax=图中的添加轴([0.1,0.1,0.8,0.8],极轴=真)
ax.set_ylim(0,90)
斧头套件(努比阿兰奇(0,90,10))
最大散射(ra,dec,c=['r'如果a<31,则为“黄色”,如果a<41,则为“绿色”,则为n中的a],s=40)
ax.set_title(“此处的图形标题”,va='bottom',y=1.08)
对于ax.yaxis.get_ticklabels()中的标签:
label.set_可见(False)
对于ax.yaxis.get_ticklabels()[::3]中的标签:
label.set_可见(True)
pyplot.show()
1。给点上色使用
scatter
的c
参数可以为点着色。在此情况下,您可以向其提供n
数组,并根据颜色映射选择颜色。彩色地图由不同的颜色组成(31倍红色,10倍黄色,20倍绿色)。
使用colormap的优点是它允许轻松使用colorbar
2。可以使用s
参数使圆变大
3。在标签和标题之间添加空格最好将标题向上移动一点,将y
参数设置为set\u title
。为了使标题不超出图中的范围,我们可以使用subplot\u adjust
方法,将top
缩小一点。(请注意,只有通过子地块创建轴时,此操作才有效。)
4。设置只能显示某些记号
标记为ax.set\yticks([0,30,60])
并通过ax.set\rlabel\u位置(0)
沿水平线定向标签。(注意<代码> SETHYRAPELLION位置< /代码>可从1.4版开始,如果您有一个早期版本,请考虑更新)
非常感谢您的详细解释!这是s-u-p-e-r help=)我在阅读代码时意识到我错误地读取了“圆”的值。实际上,这些值应该是相反的(例如:中心应该是90,最外面的圆应该是0),因为它们代表仰角(当观察者位于图表的中心时)。六羟甲基三聚氰胺六甲醚。。。有没有办法纠正这个问题?只是一个快速更新:我学会了如何更改图形的方向,使“0”指向正上方(即:北)。我刚刚添加了:ax.set_theta_zero_location('N')ax.set_theta_direction(-1),它成功了=)明白了!!!=)解决办法是:1。更改ylim的顺序:ax.set_ylim(90,0)2。每个dec值减去90:dec[:]=[90-x代表dec中的x]效果非常好=)再次感谢大家的帮助!!
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()
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()