【问题标题】:Adding color to Polar Scatter plot data points为极地散点图数据点添加颜色
【发布时间】:2017-08-23 21:05:33
【问题描述】:

我正在尝试用 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 )没有成功=(

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

【问题讨论】:

    标签: python numpy matplotlib plot scatter


    【解决方案1】:

    1.为点着色
    使用scatterc 参数可以对点进行着色。在这种情况下,您可以为其提供n 数组,并根据颜色图选择颜色。颜色图将由不同的颜色组成(31 倍红色,10 倍黄色,20 倍绿色)。 使用颜色图的优点是它允许轻松使用颜色条。

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

    3。在标签和标题之间添加空格 最好将标题向上移动一点,使用y 参数到set_title。为了标题不出图,我们可以使用subplots_adjust的方法,把top变小一点。 (请注意,这仅适用于通过子图创建轴的情况。)

    4.只显示某些刻度可以通过设置来完成 ax.set_yticks([0,30,60]) 的刻度和沿水平线定向 ylabels 由 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()
    

    【讨论】:

    • 非常感谢您提供令人难以置信的详细解释!这对 s-u-p-e-r 很有帮助 =)
    • 我在浏览代码时意识到我错误地读取了“圆圈”的值。实际上,这些值应该是相反的(例如:中心应该是 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 for x in dec] 工作方式类似魅力 =) 再次感谢大家的帮助!!
    【解决方案2】:

    对于颜色,您可以使用 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()
    

    【讨论】:

      猜你喜欢
      • 2019-05-14
      • 2013-06-13
      • 1970-01-01
      • 2020-02-15
      • 2020-11-03
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多