代码之家  ›  专栏  ›  技术社区  ›  Soerendip

如何在同一图形的子图中使用不同的选项板绘图?

  •  0
  • Soerendip  · 技术社区  · 7 年前

    如何在同一图形的子图中使用不同的调色板进行打印?在下面的示例中,绘图的生成被延迟。因此,只有最后一个调色板用于生成绘图。虽然我需要用不同的调色板生成子图。

    x = np.arange(10)
    pal = sns.color_palette("rainbow", 12)
    sns.set_palette(pal)
    
    subplot(2, 1, 1)
    for i in range(4):
        plot(x, i*np.sin(x))
    
    subplot(2, 1, 2)
    
    pal = sns.color_palette("Set1", 12)
    sns.set_palette(pal)
    
    for i in range(4):
        plot(x, i*np.cos(x))
    
    tight_layout()
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   Sheldore    7 年前

    你需要设置调色板 之前 创建子地块。这是因为颜色循环是轴的属性。轴将从中获取当前属性循环器 rcParams 在创作时。所以 set_palette ,它会更改属性循环器,需要提前调用。

    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    x = np.arange(10)
    
    # First subplot
    pal1 = sns.color_palette("rainbow", 12)
    sns.set_palette(pal1)
    
    plt.subplot(2, 1, 1)
    for i in range(4):
        plt.plot(x, i*np.sin(x))
    
    # Second subplot
    pal2 = sns.color_palette("Set1", 12)
    sns.set_palette(pal2)
    
    plt.subplot(2, 1, 2)
    for i in range(4):
        plt.plot(x, i*np.cos(x))
    
    plt.tight_layout()
    plt.show()
    

    enter image description here