在python中为多个子图创建通用颜色条

时间:2019-12-20 09:26:11

标签: python colorbar

我已将熊猫数据帧分为几个子图,如以下代码中所述。每个子图都有一个特定的颜色条。我想为所有子图制作一个通用的颜色条。数据框直接来自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))

Subplots with multiple colorbars

1 个答案:

答案 0 :(得分: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()

enter image description here