有没有办法设置颜色条比例以登录seaborn热图图表?
我正在使用pandas的数据透视表输出作为调用的输入
sns.heatmap(df_pivot_mirror,annot=False,xticklabels=256,yticklabels=128,cmap=plt.cm.YlOrRd_r)
Run Code Online (Sandbox Code Playgroud)
谢谢.
Tho*_* G. 23
简答:
from matplotlib.colors import LogNorm
sns.heatmap(df, norm=LogNorm())
Run Code Online (Sandbox Code Playgroud)
cph*_*wis 15
是的,但是seaborn已经为色条硬编码了一个线性刻度定位器,所以结果可能不是你想要的:
# http://matplotlib.org/examples/pylab_examples/pcolor_log.html
# modified to use seaborn
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import numpy as np
from matplotlib.mlab import bivariate_normal
import seaborn as sns; sns.set()
N = 20
X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
# A low hump with a spike coming out of the top right.
# Needs to have z/colour axis on a log scale so we see both hump and spike.
# linear scale only shows the spike.
Z1 = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
fig, axs = plt.subplots(ncols=2)
sns.heatmap(Z1, ax = axs[0])
sns.heatmap(Z1, ax = axs[1],
#cbar_kws={'ticks':[2,3]}, #Can't specify because seaborn does
norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()))
axs[0].set_title('Linear norm colorbar, seaborn')
axs[1].set_title('Log norm colorbar, seaborn')
plt.show()
Run Code Online (Sandbox Code Playgroud)
请参阅pylab示例,该示例以pylab版本开头,自动获取colorbar tick标签(尽管不是很漂亮).
您可以编辑seaborn代码使其工作:如果您更改plot()/seaborn/matrix.py(版本0.7.0)中的函数:
# Possibly add a colorbar
if self.cbar:
ticker = mpl.ticker.MaxNLocator(6)
if 'norm' in kws.keys():
if type(kws['norm']) is mpl.colors.LogNorm:
ticker = mpl.ticker.LogLocator(numticks=8)
Run Code Online (Sandbox Code Playgroud)
你得到:
我会建议在seaborn github上,但如果你想早点,那就是它.
您可以使用matplotlib.colors.LogNorm标准化颜色栏上的值。我还必须手动将标签设置为seaborn,并最终得到以下代码:
#!/usr/bin/env python3
import math
import numpy as np
import seaborn as sn
from matplotlib.colors import LogNorm
data = np.random.rand(20, 20)
log_norm = LogNorm(vmin=data.min().min(), vmax=data.max().max())
cbar_ticks = [math.pow(10, i) for i in range(math.floor(math.log10(data.min().min())), 1+math.ceil(math.log10(data.max().max())))]
sn.heatmap(
data,
norm=log_norm,
cbar_kws={"ticks": cbar_ticks}
)
Run Code Online (Sandbox Code Playgroud)

小智 5
响应 cphlewis (我没有足够的声誉),我使用cbar_kws;解决了这个问题 正如我在这里看到的:seaborn clustermap:设置颜色条刻度线。
例如cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]}。
from matplotlib.colors import LogNorm
s=np.random.rand(20,20)
sns.heatmap(s, norm=LogNorm(s.min(),s.max()),
cbar_kws={"ticks":[0,1,10,1e2,1e3,1e4,1e5]},
vmin = 0.001, vmax=10000)
plt.show()
Run Code Online (Sandbox Code Playgroud)
祝你今天过得愉快。
| 归档时间: |
|
| 查看次数: |
12769 次 |
| 最近记录: |