简体   繁体   English

为Polar Scatter图数据点添加颜色

[英]Adding color to Polar Scatter plot data points

I'm trying to make a polar plot with Python, of which I've been somewhat successful so far 我正在尝试使用Python绘制极坐标图,到目前为止我已经取得了一些成功

polar scatter plot example 极谱图示例

I did have a few questions for which I was hoping to get some ideas/suggestions: 我确实有一些我希望得到一些想法/建议的问题:

  1. is it possible set the color of the circles to a specific value (eg: "n" in the sample code below)? 是否可以将圆圈的颜色设置为特定值(例如,下面的示例代码中的“ n”)? If so, can I set specific color ranges? 如果可以,我可以设置特定的颜色范围吗? Eg: 0-30: red color, 31-40: yellow; 例如:0-30:红色; 31-40:黄色; 41-60: green 41-60:绿色

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 =( 注意:按照基于R中值的条件颜色使用Plot的示例,我尝试了ax.scatter(ra,dec,c = ifelse(n < 30,'red','green'), pch = 19 )但没有成功= (

  1. how can I make the data circles a little bit bigger? 如何使数据圈更大?

  2. can I move the "90" label so that the graph title does not overlap? 如何移动“ 90”标签,以使图形标题不重叠? I tried: x.set_rlabel_position(-22.5) but I get an error ("AttributeError: 'PolarAxes' object has no attribute 'set_rlabel_position'") 我尝试了: x.set_rlabel_position(-22.5)但出现错误(“ AttributeError:'PolarAxes'对象没有属性'set_rlabel_position'“)

  3. Is it possible to only show the 0,30, and 60 elevation labels? 是否可以仅显示0,30和60高程标签? Can these be oriented horizontally (eg: along the 0 azimuth line)? 它们可以水平定向吗(例如:沿0方位线)?

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. colorize points 1.着色点
Using the c argument of scatter allows to colorize the points. 使用scatterc参数可以使点着色。 In thei caase you may supply the n array to it and let the color be chosen accoring to a colormap. 因此,您可以为其提供第n数组,并根据颜色图选择颜色。 The colormap would consist of the different colors (31 times red, 10 times yellow, 20 times green). 颜色图将包含不同的颜色(红色31倍,黄色10倍,绿色20倍)。 The advantage of using a colormap is that it allows to easily use a colorbar. 使用颜色图的优点是可以轻松使用颜色条。

2. making circles bigger can be done using the s argument. 2.使用s参数可以使圆变大

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 . 3.在标签和标题之间添加空间这最好通过使用set_titley参数将标题稍微向上移动来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. 为了使标题不会超出图形范围,我们可以使用subplots_adjust方法并将top缩小一些。 (Note that this works only if the axes are created via subplots.) (请注意,这仅在通过子图创建轴时有效。)

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) . 4.仅显示某些刻度可以通过将刻度设置为ax.set_yticks([0,30,60])来实现,并且将ylabel沿水平线定向是通过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). (请注意, set_rlabel_position从版本1.4开始可用,如果您使用的是较早版本,请考虑更新)。

在此处输入图片说明

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

for color you can use c=[list of colors for each element], for size s= like here: 对于颜色,您可以使用c = [每个元素的颜色列表],对于大小s =,如下所示:

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

for the title and axis I would recommend changing the position of the title: 对于标题和轴,我建议更改标题的位置:

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

you may have to adjust the size of the figure to show the title correctly: 您可能需要调整图的大小以正确显示标题:

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

for visibility of ticks you can use: 为了显示刻度,您可以使用:

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

overall: 总体:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM