【问题标题】:Create a common colorbar for multiple subplots in python在 python 中为多个子图创建一个公共颜色栏
【发布时间】:2019-12-20 09:26:11
【问题描述】:

我已将 pandas 数据框划分为多个子图,如下面的代码所述。每个子图都有一个特定的颜色条。我想为所有子图制作一个公共颜色条。数据框直接来自 exel csv 文件。

Table_1_pos=df_pos.iloc[0:4,0:13]
Table_2_pos=df_pos.iloc[4:8,0:13]
Table_3_pos=df_pos.iloc[8:12,0:13]        
Table_4_pos=df_pos.iloc[12:16,0:13]        
[![enter image description here][1]][1]fig, axs = plt.subplots(nrows=4, gridspec_kw=dict(width_ratios=[4]),figsize=(15,8))  

ax1=sns.heatmap(Table_1_neg, annot=True, yticklabels=True, xticklabels=False, cbar=True, ax=axs[0], linewidths=1)
ax1.set_yticklabels(ax1.get_yticklabels(), rotation=0)
ax1.tick_params(right=True, left=False, labelright=True, labelleft=False)
bottom_1, top_1 = ax1.get_ylim()
ax1.set_ylim(bottom_1 + 0.5, top_1 - 0.5)
ax1.set_ylabel('Table 4')
ax1.set_ylabel(ax1.get_ylabel(),labelpad=20, rotation=0)        


ax2=sns.heatmap(Table_2_neg, annot=True, yticklabels=True, xticklabels=False, cbar=True, ax=axs[1], linewidths=1)
ax2.set_yticklabels(ax2.get_yticklabels(), rotation=0)
ax2.tick_params(right=True, left=False, labelright=True, labelleft=False)
bottom_2, top_2 = ax2.get_ylim()
ax2.set_ylim(bottom_2 + 0.5, top_2 - 0.5)
ax2.set_ylabel('Table 3')
ax2.set_ylabel(ax2.get_ylabel(),labelpad=20, rotation=0)         

ax3=sns.heatmap(Table_3_neg, annot=True, yticklabels=True, xticklabels=False, cbar=True, ax=axs[2], linewidths=1)
ax3.set_yticklabels(ax3.get_yticklabels(), rotation=0)
ax3.tick_params(right=True, left=False, labelright=True, labelleft=False)
bottom_3, top_3 = ax3.get_ylim()
ax3.set_ylim(bottom_3 + 0.5, top_3 - 0.5)
ax3.set_ylabel('Table 2')
ax3.set_ylabel(ax3.get_ylabel(),labelpad=20, rotation=0) 

ax4=sns.heatmap(Table_4_neg, annot=True, yticklabels=True, xticklabels=True, cbar=True, ax=axs[3], linewidths=1)
ax4.set_yticklabels(ax4.get_yticklabels(), rotation=0)
ax4.tick_params(right=True, left=False, labelright=True, labelleft=False)
bottom_4, top_4 = ax4.get_ylim()
ax4.set_ylim(bottom_1 + 0.5, top_1 - 0.5)        
ax4.set_ylabel('Table 1')
ax4.set_ylabel(ax4.get_ylabel(),labelpad=20, rotation=0) 


cbaxes = fig.add_axes([0.95, 0.1, 0.01, 0.8])     
mappable = axs.get_children()[0]         
plt.colorbar(mappable, ax = [ax1,ax2,ax3,ax4],orientation = 'vertical',cax = cbaxes)

plt.title("Tilt = {tilt}    -     WindDir = {winddir} (neg)  -   Cpnet,comparison".format(tilt=x, winddir=y),horizontalalignment='right',x=-30,y=1, verticalalignment='top')

out_fp_neg_1 = os.path.join(image_dirn, outpattern_neg.format(tilt=x, dir=y))

【问题讨论】:

    标签: python colorbar


    【解决方案1】:

    如果您的所有子图都使用相同范围的值,您可以使用Joe Kington 提供的解决方案。

    但是,从图像来看,似乎所有子图的范围都不相同。 希望seaborn 允许您定义热图的最小值和最大值,以及绘制颜色条的轴。

    这是一个工作示例:

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    def main():
        df_pos = pd.DataFrame(
            np.random.random((16,13)) * 2 - 1
        )
        vmin = df_pos.min().min()
        vmax = df_pos.max().max()
    
        fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(15,8))
        fig.subplots_adjust(right=0.8)
        cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
        for i, ax in enumerate(axes.flat):
            sns.heatmap(
                data=df_pos.iloc[4*i:4*(i+1), 0:13], ax=ax, vmin=vmin, vmax=vmax, cbar_ax=cbar_ax,
                xticklabels=False, yticklabels=False, annot=True, linewidths=1)
    
        plt.show()
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

    • 嗨,Alexis,您提出的代码非常适合我的情况,但不幸的是我无法使用它,因为无法在 numpy 数组中转换 13*16 数据帧。我确实尝试了很多次,但总是出现错误
    • @pantelis 我在使用 DataFrame 时没有发现任何问题。我使用来自数据框而不是 numpy 数组的数据进行了更新。
    • 非常感谢
    猜你喜欢
    • 2021-05-22
    • 2016-08-14
    • 2014-12-29
    • 2016-05-24
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多