首页 > 解决方案 > 如何在散点图中添加自定义六进制颜色作为 cmap?

问题描述

我在四个不同的子图中有四个数据集。这些数据分为 15 组,但在这个片段中我只放了 3 组。我希望每个子图中的数据应根据以下散点图中自定义定义的十六进制颜色图着色。我无法将“mycolors”变量用作 cmap。任何帮助将不胜感激。

import numpy as np
import matplotlib.pyplot as plt
from colormap import Color
import matplotlib as mpl
import matplotlib.cm as cm
from collections import OrderedDict

cmaps=OrderedDict()
#cmaps['mycolors']=mycolors
mycolors=[#1f77b4, #ff7f0e, #279e68]

x0=np.random.rand(10,2)
x1=np.random.rand(10,2)
x2=np.random.rand(10,2)
x3=np.random.rand(10,2)
z=np.random.randint(3,size=10)
print(z)

fig,ax=plt.subplots(2,2,figsize=(8,7))
l0=ax[0,0].scatter(x0[:,0],x0[:,1],c=z,cmap=mpl.rcParams["image.cmap"])
l1=ax[0,1].scatter(x1[:,0],x1[:,1],c=z,cmap=mpl.rcParams["image.cmap"])#,s=8,marker='o')
l2=ax[1,0].scatter(x2[:,0],x2[:,1],c=z,cmap=mpl.rcParams["image.cmap"])#,s=8,marker='o')
l3=ax[1,1].scatter(x3[:,0],x3[:,1],c=z,cmap=mpl.rcParams["image.cmap"])#,s=8,marker='o')


 #ax[0].legend()
  legend1 = ax[0,1].legend(*l1.legend_elements(),
                ncol=1,  bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0. , prop={"size":6},title="legend name")#bbox_to_anchor=(1, 0.5)
 ax[0,1].add_artist(legend1)


 ax[0,0].set_title('original')
 ax[0,1].set_title('avg')
 ax[1,0].set_title('sum')
 ax[1,1].set_title('normalized')

 #plt.axis('off')
 for i in range(2):
    for j in range(2):
        ax[i,j].set_xticks([])
        ax[i,j].set_yticks([])
        ax[1,j].set_xlabel('x name')
        ax[i,0].set_ylabel('y name')


 plt.tight_layout()
 fig.savefig('figure.png',bbox_inches='tight')

在此处输入图像描述

标签: scatter-plotcolormap

解决方案


推荐阅读