【问题标题】:Python Scatterplot: Changing color based on both X and Y valuesPython 散点图:根据 X 和 Y 值更改颜色
【发布时间】:2019-02-11 04:51:53
【问题描述】:

我尝试使用 cmaps 以及 if/else 语句重新创建附加的图像。

我目前的尝试是基于this thread中给出的建议

我尝试使用 1.8

下面是我当前的代码:

import numpy as np
import matplotlib.pyplot as plt
N = 500
# center, variation, number of points
x = np.random.normal(2,0.2,N)
y = np.random.normal(2,0.2,N)
colors = np.where(x<=2.2,'r',np.where(y<=2.2,'b','b'))
plt.scatter(x , y, c=colors)
plt.colorbar()
plt.show()

【问题讨论】:

    标签: python matplotlib plot scatter-plot colormap


    【解决方案1】:

    要制作该图,您需要传递一个包含每个点颜色的数组。在这种情况下,颜色是到点 (2, 2) 的距离,因为分布以该点为中心。

    import numpy as np
    import matplotlib.pyplot as plt
    
    N = 500
    # center, variation, number of points
    x = np.random.normal(2,0.2,N)
    y = np.random.normal(2,0.2,N)
    
    # we calculate the distance to (2, 2).
    # This we are going to use to give it the color.
    color = np.sqrt((x-2)**2 + (y-2)**2)
    
    plt.scatter(x , y, c=color, cmap='plasma', alpha=0.7)
    # we set a alpha
    # it is what gives the transparency to the points.
    # if they suppose themselves, the colors are added.
    
    plt.show()
    

    【讨论】:

    • 非常感谢卢卡斯。当我使用反向颜色图时,我认为它已经足够接近了。
    猜你喜欢
    • 2020-10-10
    • 2017-09-14
    • 2021-01-25
    • 2015-09-18
    • 1970-01-01
    • 2016-08-06
    • 2022-06-10
    • 2013-06-26
    • 2021-10-05
    相关资源
    最近更新 更多