matplotlib画3维图z轴标签方向调整不了

img


画出来的3维图,z轴标签为带下标的符号时,标签方向怎么朝着坐标轴?不管怎么调都是朝着外边,求解答

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7763505
  • 你也可以参考下这篇文章:matplotlib画3D图形时设置z轴尺寸
  • 除此之外, 这篇博客: 【matplotlib复杂热图绘制】自定义元素注释,对数渐变色标,不显示色标,去掉留白,LZW压缩中的 对数色标:大范围数据-颜色区分度大 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 尝试了很多种办法:
    - colorbar官网文档中的cm.ScalarMappable(norm=norm, cmap=cmap):似乎关联不上数据了……
    - 轮廓覆盖和对数色标:这个我生成出来的不能覆盖整个图,周围一圈像是相框一样的留白。而且颜色不是连续的,是分块的。
    - 自己写log函数,然后关联ax.colorbar:颜色不是连续的,是分块的。(抄的别人的csdn,找不到了o(╥﹏╥)o)
    - 自定义颜色条colorbarBase:这个是关联不上数据的。

    最终解决方案:对数色标+连续渐变性
    准备三份数据shape=(x,y):要求互相的元素是一一对应的
    假设有112个元素,最终我要生成一个形状为14*8的heatmap

    1. 原始数据:
      • HLA是元素name,count是元素value
      • counts:用于画heatmap的元素values
    2. 注释数据HLA_count:HLA+count
    3. 对原始数据手动进行log等变换。注意,sklearn里那些normarlizer啥的都是对整个数据进行缩放,是不会对色标产生影响的。要想让范围很大的数据的heatmap颜色区分度大,要使用对数色标,log2比log10更有区分力。
    4. 最终需要的是下述的counts_log和HLA_count来进行画heatmap和注释。
      • 如果不取对数的话,就用counts画图
      • 注释中的count是原始数值,而非对数值哦
    def HLA_counts_to_heatmap(data_pos = None, n = 8): # 112/8=14
        HLA_count = dict(Counter(data_pos.HLA))
        HLA_count = sorted(HLA_count.items(),key=lambda x:x[1],reverse=True)
    
        HLAs, counts = [], []
        HLA_temp, count_temp = [], []
        i = 0
        for item in HLA_count:
            i += 1
    
            HLA_temp.append(item[0][4:])
            count_temp.append(item[1])
            if i == len(HLA_count) or i % n == 0:
                print(i, count_temp)
                HLAs.append(HLA_temp)
                counts.append(count_temp)
                if i != 0: HLA_temp, count_temp = [], []
    
        HLAs, counts = np.array(HLAs), np.array(counts)
        HLA_count_min = counts[0][0]
        HLA_count_max = counts[-1][-1]
        assert HLAs.shape == counts.shape
        print(HLAs.shape, HLA_count_min, HLA_count_max, [len(m) for m in HLAs], sum([len(m) for m in counts]))
        
        # 获得注释数据
        HLA_count = np.zeros((HLAs.shape[0], HLAs.shape[1])).astype(str)
        for i in range(HLAs.shape[0]):
            for j in range(HLAs.shape[1]):
                HLA_count[i, j] = HLAs[i, j] + '\n' + str(counts[i, j])
        
        # 数据log2缩放,使得画图层次更清晰
        # from sklearn.preprocessing import StandardScaler, MinMaxScaler, Normalizer, scale
        # counts_scaled = scale(counts.reshape(1, -1), axis = 1).reshape(counts.shape)
        # counts_scaled = Normalizer().fit_transform(counts.reshape(1, -1)).reshape(counts.shape)
        counts_log = np.zeros(counts.shape)
        for i in range(counts.shape[0]):
            for j in range(counts.shape[1]):
                counts_log[i, j] = math.log(counts[i, j], 2)
        sum_counts = np.sum(counts) # 所有元素值的和
        return HLAs, counts, HLA_count, counts_log, sum_counts
    
  • 您还可以看一下 董付国老师的Python可以这样学(第四季:数据分析与科学计算可视化)课程中的 补充:matplotlib可视化图例样式设置小节, 巩固相关知识点
  • 以下回答由chatgpt基于相关博客总结生成:

    由于参考资料中的内容与问题不符,无法提供具体的解决方案。推荐使用Stack Overflow等技术论坛进行查找相关解决方法。