Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Seaborn/Matplotlib中创建固定颜色贴图(最小值=红色,最大值=绿色)_Python_Matplotlib_Seaborn - Fatal编程技术网

Python 如何在Seaborn/Matplotlib中创建固定颜色贴图(最小值=红色,最大值=绿色)

Python 如何在Seaborn/Matplotlib中创建固定颜色贴图(最小值=红色,最大值=绿色),python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我正在尝试创建一个固定颜色的贴图,用作seaborn point绘图中的调色板。我目前的代码如下: my_cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("", ['red', 'orange', 'gold' ,'yellow', 'greenyellow', 'lime', 'green']) matplotlib.cm.register_cmap("myco

我正在尝试创建一个固定颜色的贴图,用作seaborn point绘图中的调色板。我目前的代码如下:

        my_cmap2 = matplotlib.colors.LinearSegmentedColormap.from_list("", ['red', 'orange', 'gold' ,'yellow', 'greenyellow', 'lime', 'green'])
        matplotlib.cm.register_cmap("mycolormap", my_cmap2)
        cpal = sns.color_palette("mycolormap", n_colors=10, desat=1)
        
        ax = sns.pointplot(x="Rank", y="Yld_Vol_Dr", data=df, hue="Rank", palette=cpal)
我需要将最小值(秩)始终固定为红色,将最大值(秩)固定为绿色。如果我最多有10个等级,我当前的代码就可以工作,但是如果我缺少任何数据(例如等级2),那么我就不能得到我所需要的完整色阶


如何将红色/绿色设置为固定的最小值/最大值?

色调顺序
可用于强制显示所有色调值(并设置其顺序)

从matplotlib导入pyplot作为plt
从matplotlib.colors导入LinearSegmentedColormap
导入seaborn作为sns
将numpy作为np导入
my_cmap2=LinearSegmentedColormap。来自_列表(“,[“红色”、“橙色”、“金色”、“黄色”、“绿黄色”、“青灰色”))
plt.cm.注册cmap(“mycolormap”,我的cmap2)
cpal=sns.color\u调色板(“mycolormap”,n\u colors=10,desat=1)
秩=np.随机选择([1,3,5,6,7,8,10],200)#缺失2,4和9的测试
y=np.随机.均匀(0,10,秩.形)
图(ax1,ax2)=plt.子批次(ncols=2,figsize=(12,4))
sns.pointplot(x=rank,y=y,色调=rank,调色板=cpal,ax=ax1)
ax1.设置标题(“无色调顺序”)
ax1.图例(bbox_至_锚=(1.01,1.05),loc='左上角')
sns.pointplot(x=rank,y=y,hue=rank,hue\u order=range(1,11),palete=cpal,ax=ax2)
ax2.设置标题(“使用色调顺序”)
ax2.图例(bbox_至_锚=(1.01,1.05),loc='左上角')
plt.紧_布局()
plt.show()

谢谢您的帮助!