【问题标题】:pyplot heatmap with text带有文本的 pyplot 热图
【发布时间】:2020-10-12 23:32:04
【问题描述】:

我正在尝试使用 MxN 矩阵绘制热图。

x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
y = [5, 15, 25, 35]
z = [[0.31, 0.77, 0.88, 0.90, 0.79, 0.70, 0.50],
     [0.32, 0.70, 0.81, 0.85, 0.75, 0.61, 0.44],
     [0.21, 0.45, 0.64, 0.77, 0.70, 0.55, 0.23],
     [0.12, 0.30, 0.41, 0.53, 0.44, 0.41, 0.11]]

它应该像下图一样形成(带颜色条)

谢谢。

【问题讨论】:

    标签: matplotlib heatmap


    【解决方案1】:
    import numpy as np
    import matplotlib
    import matplotlib.pyplot as plt
    
    x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
    y = [5, 15, 25, 35]
    z = [[0.31, 0.77, 0.88, 0.90, 0.79, 0.70, 0.50],
         [0.32, 0.70, 0.81, 0.85, 0.75, 0.61, 0.44],
         [0.21, 0.45, 0.64, 0.77, 0.70, 0.55, 0.23],
         [0.12, 0.30, 0.41, 0.53, 0.44, 0.41, 0.11]]
    
    fig, ax = plt.subplots()
    im = ax.imshow(z,origin='lower',cmap='Blues')
    ax.set_xticks(range(len(x)))
    ax.set_yticks(range(len(y)))
    ax.set_xticklabels(x)
    ax.set_yticklabels(y)
    
    for i in range(len(x)):
        for j in range(len(y)-1,-1,-1):
            text = ax.text(i, j, f'{z[j][i]:.2f}', ha='center', va='center', color='black')
           
    ax.set_title('X-Y HEATMAP')
    ax.set_xlabel('X-VALUE')
    ax.set_ylabel('Y-VALUE')
    

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-27
      • 2017-07-14
      • 2023-04-04
      • 2016-02-24
      • 1970-01-01
      相关资源
      最近更新 更多