如何使用sns.kdeplot从热图中删除白色阴影/颜色?我只想要红色

问题描述

from mplsoccer.pitch import Pitch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import kde
from scipy.ndimage import gaussian_filter
from copy import copy
np.random.seed(19680801)

plt.style.use('dark_background')

fields = ['id','minute','result','X1','Y','xG','h_a','situation','season','shottype','X']
df=pd.read_csv('shots.csv',skipinitialspace=True,usecols=fields)
fig,ax = pitch.draw()
sns.kdeplot(df.Y,df.X,shade=True,ax=ax,cmap='Reds',shade_lowest=False,levels=20,kernel='gau',gridsize=50,bw='scott',cut=0,cbar=True,)
ax.set_xlim(ax.get_xlim()[::-1])        # invert the axis
ax.yaxis.tick_right()  
plt.axis('off')
plt.show()

值df.X和df.Y在(0,1)之间。 我只想要红色部分。在matplotlib中,我使用vmin参数消除了白色部分。在这里可以做什么?

编辑:再三考虑,如果颜色图从图中的12开始,则将消除白色区域。因此,颜色图从红色开始,以深红色结束。那就是我想要的。

Output Heatmap

解决方法

我会使用这种方法从您的颜色表中挑选颜色:

from matplotlib import cm
from matplotlib.colors import ListedColormap

# select 42 colours from the "Reds" cmap
red_selection = cm.get_cmap("Reds",42)

# select half of the colours,closest to Red and assign to a new colormap
red_cmap = ListedColormap(red_selection(range(42))[21:,:])

from mplsoccer.pitch import Pitch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import kde
from scipy.ndimage import gaussian_filter
from copy import copy
np.random.seed(19680801)

plt.style.use('dark_background')

fields = ['id','minute','result','X1','Y','xG','h_a','situation','season','shotType','X']
df=pd.read_csv('shots.csv',skipinitialspace=True,usecols=fields)
fig,ax = pitch.draw()
sns.kdeplot(df.Y,df.X,shade=True,ax=ax,cmap=red_cmap,shade_lowest=False,levels=20,kernel='gau',gridsize=50,bw='scott',cut=0,cbar=True,)
ax.set_xlim(ax.get_xlim()[::-1])        # invert the axis
ax.yaxis.tick_right()  
plt.axis('off')
plt.show()