将编号等高线添加到 2D 热图

问题描述 投票:0回答:1

我想将标记的轮廓线添加到 2D 热图中。颜色代表数据的值,数据都是浮点数。灰色区域并不重要,由于其他原因这些区域已设置为 NaN。这是我生成图表的代码:


import seaborn as sns
import pandas as pd
from scipy import ndimage
from matplotlib import pyplot as plt

fig, ax = plt.subplots()
sns.set_theme(style='dark')
im = ax.imshow(data,cmap='turbo') 
cbar = ax.figure.colorbar(im, ax = ax,cmap='turbo')
cbar.ax.set_ylabel("Cost ($B)", rotation = -90, va = "bottom")
ax. grid(True)
ax.set_facecolor('dimgrey')

我尝试使用, seaborn 热图中的轮廓 (iso-z) 或阈值线 但这没有用。

我试着看看这个, 在 imshow 上叠加等高线图 但我的数据是浮点数的二维数组,而不是单独的 x、y、z 值。

python pandas numpy heatmap contour
1个回答
0
投票

如果您愿意使用高级可视化包(plotly)而不是基本的 matplotlib。它可能更容易实施。 仅供参考

import plotly.graph_objects as go

feature_x = np.arange(0, 50, 2)
feature_y = np.arange(0, 50, 3)

# Creating 2-D grid of features
[X, Y] = np.meshgrid(feature_x, feature_y)

Z = np.cos(X / 2) + np.sin(Y / 4)

fig = go.Figure(data =
    go.Contour(
        x = feature_x, 
        y = feature_y, 
        z = Z, 
        contour=dict(
            start=1,
            end=5,
            size=.2, showlabels=True
            )))

fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.