【问题标题】:How to combine heatmap with contour plot?如何将热图与等高线图结合起来?
【发布时间】:2021-07-22 23:50:36
【问题描述】:

我有两个相似的三维但独立的数据集(在不同的 CSV 文件中),其中 α 和 δ 是自变量,而 φ(第一个数据集)或百分比值(第二个数据集)是因变量。数据集类似于数据透视表。

我已经设法绘制了第一个数据集的热图:

现在我想将第二个数据集的百分比值添加为等高线。我在下面的例子中手动完成了:

如何使用 Seaborn 或 Matplotlib 实现这一目标? 我目前的代码如下:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns; sns.set_theme()

df = pd.read_csv("data.csv", sep=";").transpose()
sns.heatmap(
    df,
    cmap="Reds",
    cbar_kws={"label": r'$\phi$'},
    vmin=0.0, vmax=1.2).invert_yaxis()
plt.xlabel(r'$\alpha$', fontsize=20)
plt.ylabel(r'$\delta$', fontsize=20)
plt.tight_layout()
plt.show()

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    试试contour:

    x,y = np.meshgrid(np.arange(df2.shape[0])+0.5, 
                      np.arange(df2.shape[1])+0.5, 
                      indexing='ij'
                     )
    
    ax=sns.heatmap(df1)
    ax.contour(x,y, df2, levels=[40,80,100])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2020-09-24
      • 2022-10-18
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多