Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 为seaborn热图创建自定义cmap_Python 3.x_Matplotlib_Seaborn - Fatal编程技术网

Python 3.x 为seaborn热图创建自定义cmap

Python 3.x 为seaborn热图创建自定义cmap,python-3.x,matplotlib,seaborn,Python 3.x,Matplotlib,Seaborn,这是一张很棒的热图: 我正在尝试在Seaborn中做类似的事情,我想知道是否可以使用自定义调色板,比如将其转换为cmap,然后使用center使其倾斜 import seaborn as sns from matplotlib import pyplot as plt mypalette = ['#e8f0ff', '#51abff', '#12b74c', '#fffb16', '#ffc044', '#ff9400', '#ff6a00', '#c13300', '#b20c0c'] sn

这是一张很棒的热图: 我正在尝试在Seaborn中做类似的事情,我想知道是否可以使用自定义调色板,比如将其转换为cmap,然后使用center使其倾斜

import seaborn as sns
from matplotlib import pyplot as plt

mypalette = ['#e8f0ff', '#51abff', '#12b74c', '#fffb16', '#ffc044', '#ff9400', '#ff6a00', '#c13300', '#b20c0c']
sns.palplot(sns.color_palette(mypalette))
我能得到的最接近的东西是

plt.figure('hmap')
flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")
ax = sns.heatmap(flights, cmap = mypalette, center = 350)

当然,这个调色板缺少渐变。为数据集道歉,我意识到它不是歪斜的,但似乎使用起来很简单。

使用Matplotlib中列出的颜色或线性分段颜色映射,例如,像在这个问题中():

您可以使用RGB或十六进制格式指定颜色。但是,seaborn提供了许多预定义的选项板,请参见

myColors = ((0.8, 0.0, 0.0, 1.0), (0.0, 0.8, 0.0, 1.0), (0.0, 0.0, 0.8, 1.0))

cmap = LinearSegmentedColormap.from_list('Custom', myColors, len(myColors))