簡體   English   中英

為Polar Scatter圖數據點添加顏色

[英]Adding color to Polar Scatter plot data points

我正在嘗試使用Python繪制極坐標圖,到目前為止我已經取得了一些成功

極譜圖示例

我確實有一些我希望得到一些想法/建議的問題:

  1. 是否可以將圓圈的顏色設置為特定值(例如,下面的示例代碼中的“ n”)? 如果可以,我可以設置特定的顏色范圍嗎? 例如:0-30:紅色; 31-40:黃色; 41-60:綠色

注意:按照基於R中值的條件顏色使用Plot的示例,我嘗試了ax.scatter(ra,dec,c = ifelse(n < 30,'red','green'), pch = 19 )但沒有成功= (

  1. 如何使數據圈更大?

  2. 如何移動“ 90”標簽,以使圖形標題不重疊? 我嘗試了: x.set_rlabel_position(-22.5)但出現錯誤(“ AttributeError:'PolarAxes'對象沒有屬性'set_rlabel_position'“)

  3. 是否可以僅顯示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()

1.着色點
使用scatterc參數可以使點着色。 因此,您可以為其提供第n數組,並根據顏色圖選擇顏色。 顏色圖將包含不同的顏色(紅色31倍,黃色10倍,綠色20倍)。 使用顏色圖的優點是可以輕松使用顏色條。

2.使用s參數可以使圓變大

3.在標簽和標題之間添加空間這最好通過使用set_titley參數將標題稍微向上移動來set_title 為了使標題不會超出圖形范圍,我們可以使用subplots_adjust方法並將top縮小一些。 (請注意,這僅在通過子圖創建軸時有效。)

4.僅顯示某些刻度可以通過將刻度設置為ax.set_yticks([0,30,60])來實現,並且將ylabel沿水平線定向是通過ax.set_rlabel_position(0) (請注意, 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()

對於顏色,您可以使用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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM