【问题标题】:How to plot scatter data points using colormap after comparing two numpy arrays?比较两个 numpy 数组后如何使用颜色图绘制散点数据点?
【发布时间】:2019-09-05 12:49:26
【问题描述】:

在下面的代码中,我想在比较两个 numpy 数组 ab 后绘制散点图。当a 中的某个值较低时,则为相应的值b 分配一个明亮的颜色。例如,当a 为零时,将亮色分配给最终图形上的值 2(在 b 中)。经过这样的比较,我从未用颜色绘制数据。请指导我如何做到这一点?

a = np.array([6,2,7,0,1])
b= np.array([-3,-2,0,2,3])
c=np.array([1/3,1/3,1/3,1/3,1/3])
print("lengths:",len(a),len(b),len(c))
fig=plt.figure()
ax= fig.add_subplot(111)
ax.scatter(b,c,marker='.')
ax.set_xlim(-3,3)
ax.set_ylim(-1/2,1/2)
plt.savefig("./Colormap")`

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    您可以使用np.where 创建一个颜色数组并提供您的条件。像这样的

    low_thresh = 1
    colors = np.where(a < low_thresh, 'r', 'g')
    
    plt.scatter(a,b,c=colors)
    

    【讨论】:

      【解决方案2】:

      如果我理解正确,你可以这样做:

      for el_a, el_b, el_c in zip(a,b,c):
          if el_a < el_b:
              ax.scatter(el_b, el_c , c='lightblue', marker='.')
          else: ax.scatter(el_b, el_c , c='blue', marker='.')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多