为Polar Scatter图数据点添加颜色 [英] Adding color to Polar Scatter plot data points

查看:383
本文介绍了为Polar Scatter图数据点添加颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python绘制极坐标图,到目前为止,我已经取得了一些成功

I'm trying to make a polar plot with Python, of which I've been somewhat successful so far

极地散射图示例

我确实有一些我希望得到一些想法/建议的问题:

I did have a few questions for which I was hoping to get some ideas/suggestions:

  1. 是否可以将圆圈的颜色设置为特定值(例如,下面的示例代码中的"n")?如果可以,我可以设置特定的颜色范围吗?例如:0-30:红色; 31-40:黄色; 41-60:绿色

注意:请遵循中的示例在R 中的值上,我尝试了ax.scatter(ra,dec,c = ifelse(n < 30,’red','green'), pch = 19 )但没有成功=(

Note: following the examples from Plot with conditional colors based on values in R, I tried ax.scatter(ra,dec,c = ifelse(n < 30,’red','green'), pch = 19 ) without success =(

  1. 我怎样才能使数据圈大一点?

  1. how can I make the data circles a little bit bigger?

我可以移动"90"标签,以便图形标题不重叠吗?我试过了: x.set_rlabel_position(-22.5),但出现错误("AttributeError:'PolarAxes'对象没有属性'set_rlabel_position'")

can I move the "90" label so that the graph title does not overlap? I tried: x.set_rlabel_position(-22.5) but I get an error ("AttributeError: 'PolarAxes' object has no attribute 'set_rlabel_position'")

是否只能显示0,30和60高程标签?这些可以水平放置吗(例如:沿着0方位线)?

Is it possible to only show the 0,30, and 60 elevation labels? Can these be oriented horizontally (e.g.: along the 0 azimuth line)?

非常感谢!期待听到您的建议=)

Thank you so much! Looking forward to hearing your suggestions =)

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()

推荐答案

1.为点着色
使用scatterc自变量可以使点着色.因此,您可以为其提供n数组,并根据颜色图选择颜色.色彩图将由不同的颜色组成(红色31倍,黄色10倍,绿色20倍). 使用颜色图的优点是可以轻松使用颜色栏.

1. colorize points
Using the c argument of scatter allows to colorize the points. In thei caase you may supply the n array to it and let the color be chosen accoring to a colormap. The colormap would consist of the different colors (31 times red, 10 times yellow, 20 times green). The advantage of using a colormap is that it allows to easily use a colorbar.

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

2. making circles bigger can be done using the s argument.

3.在标签和标题之间添加空间,最好使用set_title参数y将标题稍微向上移动.为了使标题不超出图的范围,我们可以使用subplots_adjust方法并将top缩小一些. (请注意,这仅在通过子图创建轴时有效.)

3. adding space between label and title This would best be done by moving the title a bit upwards, using the y argument to set_title. In order for the title not to go outside the figure, we can use the subplots_adjust method and make top a little smaller. (Note that this works only if the axes are created via subplots.)

4.仅显示某些滴答声可以通过设置来完成 勾号为ax.set_yticks([0,30,60])并沿ax.set_rlabel_position(0)完成ylabel沿水平线的定向. (请注意,set_rlabel_position从版本1.4开始可用,如果您使用的是较早的版本,请考虑更新).

4. Only show certain ticks can be accomplished by setting the ticks as ax.set_yticks([0,30,60]) and orienting ylabels along a horizontal line is done by ax.set_rlabel_position(0). (Note that set_rlabel_position is available from version 1.4 on, if you have an earlier version, consider updating).

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()

这篇关于为Polar Scatter图数据点添加颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆