gpt4 book ai didi

python - 带有对数刻度颜色条的 Seaborn 热图

转载 作者:行者123 更新时间:2023-12-03 14:46:30 25 4
gpt4 key购买 nike

有没有办法设置颜色条比例以登录seaborn热图?
我正在使用 Pandas 的数据透视表输出作为调用的输入

 sns.heatmap(df_pivot_mirror,annot=False,xticklabels=256,yticklabels=128,cmap=plt.cm.YlOrRd_r)

谢谢你。

最佳答案

如果您当前安装了 seaborn,norm=LogNorm()在调用 heatmap现在工作。 (在评论中指出——谢谢。)将其添加到其中一个 seaborn 示例中:

import numpy as np
import seaborn as sns; sns.set_theme(style='white')
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm, Normalize
from matplotlib.ticker import MaxNLocator

flights = sns.load_dataset("flights")
flights = flights.pivot("month", "year", "passengers")


f3, ax5 = plt.subplots(1,1)
sns.heatmap(flights, square=True, norm=LogNorm())
Heatmap with lognorm colorbar, four tick labels
您可以在 seaborn 中将颜色条参数作为关键字传递包装,但它们有时会与 seaborn 选择发生冲突:
sns.heatmap(flights, square=True, norm=LogNorm(), cbar_kws={'ticks':MaxNLocator(2), 'format':'%.e'})
Heatmap with lognorm colorbar, four tick labels with inconsistent numerical formatting
为了比较,这是 matplotlib没有 seaborn 改进的热图——颜色条参数都被应用了:
f5, ax6 = plt.subplots(1,1)
im6 = plt.imshow(flights, norm=LogNorm())
cbar6 = ax.figure.colorbar(im6, ax=ax6, ticks=MaxNLocator(2), format='%.e')
Heatmap with lognorm colorbar, two tick labels, scientific number formatting
如果您必须使用较旧的安装和 LogNorm在 seaborn 中不起作用,请参阅此答案的先前版本以获取解决方法。

关于python - 带有对数刻度颜色条的 Seaborn 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36898008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com