Python 具有用户定义颜色范围和静态颜色映射的二维彩色编码散点图

Python 具有用户定义颜色范围和静态颜色映射的二维彩色编码散点图,python,numpy,matplotlib,scatter-plot,Python,Numpy,Matplotlib,Scatter Plot,我有3个向量-x,y,vel,每个向量都有一些8k值。我还有很多包含这3个向量的文件。所有文件都有不同的x、y、vel。我希望在以下条件下获得多个散点图: 根据第三个变量(即vel)进行颜色编码 为颜色(第一个文件中的数据)设置范围后,所有剩余文件的颜色范围应保持不变。我不希望动态更改(颜色代码随每个新文件而更改) 要绘制颜色条 我非常感谢你的所有想法 我已经附上了一个文件的代码 import numpy as np import matplotlib.pyplot as plt # Crea

我有3个向量-
x
y
vel
,每个向量都有一些8k值。我还有很多包含这3个向量的文件。所有文件都有不同的x、y、vel。我希望在以下条件下获得多个散点图:

  • 根据第三个变量(即vel)进行颜色编码
  • 为颜色(第一个文件中的数据)设置范围后,所有剩余文件的颜色范围应保持不变。我不希望动态更改(颜色代码随每个新文件而更改)
  • 要绘制颜色条
  • 我非常感谢你的所有想法

    我已经附上了一个文件的代码

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Create Map
    cm = plt.cm.get_cmap('RdYlBu')
    x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
    vel = [cm(float(i)/(8000)) for i in xrange(8000)] # 8000 is the no. of values in each of x,y,vel vectors.
    
    # 2D Plot
    plt.scatter(x, y, s=27, c=vel, marker='o')
    plt.axis('equal')
    plt.savefig('testfig.png', dpi=300)
    plt.show()
    quit()
    

    您必须迭代所有数据文件以获得
    vel
    的最大值,我添加了几行代码(需要根据您的情况进行调整)来实现这一点

    因此,您的
    colorbar
    行已更改为使用
    max\u-vel
    ,允许您使用固定值
    8000
    删除该代码

    此外,我还随意删除了点周围的黑色边缘,因为我发现它们“模糊”了点的颜色

    最后,我添加并调整了绘图代码,以使用
    对象,该对象需要有颜色条

    import numpy as np
    import matplotlib.pyplot as plt
    # This is needed to iterate over your data files
    import glob 
    
    # Loop over all your data files to get the maximum value for 'vel'. 
    # You will have to adjust this for your code
    """max_vel = 0
    for i in glob.glob(<your files>,'r') as fr:
        # Iterate over all lines
        if <vel value> > max_vel:
            max_vel = <vel_value>"""
    
    # Create Map
    cm = plt.cm.get_cmap('RdYlBu')
    x,y,vel = np.loadtxt('finaldata_temp.txt', skiprows=0, unpack=True)
    
    # Plot the data
    fig=plt.figure()
    fig.patch.set_facecolor('white')
    # Here we switch to an axis object
    # Additionally, you can plot several of your files in the same figure using
    # the subplot option.
    ax=fig.add_subplot(111)
    s = ax.scatter(x,y,c=vel,edgecolor=''))
    # Here we assign the color bar to the axis object
    cb = plt.colorbar(mappable=s,ax=ax,cmap=cm)
    # Here we set the range of the color bar based on the maximum observed value
    # NOTE: This line only changes the calculated color and not the display 
    # 'range' of the legend next to the plot, for that we need to switch to 
    # ColorbarBase (see second code snippet).
    cb.setlim(0,max_vel)
    cb.set_label('Value of \'vel\'')
    plt.show()
    
    这将生成以下绘图


    有关如何使用
    colorbar
    的更多示例,特别是更灵活的
    colorbase
    ,我建议您查看文档->

    是的,添加vmax和vmin解决了问题:),但现在出现了另一个问题。默认情况下,此代码忽略我的颜色映射(此处为RdBu_r),每次都会获取不同的特定映射。蚂蚁的想法如何改变呢?导入numpy作为np导入matplotlib.pyplot作为plt创建映射cm=plt.cm.get_cmap('RdBu_r')#RdYlBu x,y,vel=np.loadtxt('finaldata.txt',skiprows=0,unpack=True)#二维绘图plt.scatter(x,y,s=27,c=vel,vmin=0,vmax=0.09,marker='o')plt.axis('equal')plt.colorbar()plt.savefig('testfig.png',dpi=400)这是因为在该代码段中,您没有将
    cm
    传递到
    colorbar
    ,请将
    plt.colorbar()
    行更改为
    plt.colorbar(cmap=cm)
    。我建议您从我的两个示例片段中选择一个,然后从那里开始,这样可以避免最后的错误。
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib as mpl
    
    cm = plt.cm.get_cmap('RdYlBu')
    x = [1,5,10]
    y = [2,6,9]
    vel = [7,2,1]
    
    # Plot the data
    fig=plt.figure()
    fig.patch.set_facecolor('white')
    ax=fig.add_subplot(111)
    s = ax.scatter(x,y,c=vel,edgecolor=''))
    norm = mpl.colors.Normalize(vmin=0, vmax=10)
    ax1 = fig.add_axes([0.95, 0.1, 0.01, 0.8])
    cb = mpl.colorbar.ColorbarBase(ax1,norm=norm,cmap=cm,orientation='vertical')
    cb.set_clim(vmin = 0, vmax = 10)
    cb.set_label('Value of \'vel\'')
    plt.show()