2017-01-23 25 views
1

在我的python程序中,我有三個曲線家族,每個我想指定一個seaborn調色板並將它們全部繪製成一個單獨的繪圖。在一個地塊中使用不同的seaborn調色板的曲線家族

我的解決方案到目前爲止是下面的代碼。

sns.set_style('whitegrid') 
volt = np.zeros(N) 
states = np.zeros([6,N]) 

for k in range(1,4): 
    if k == 1: 
     sns.set_palette('Blues_d',6) 
    if k == 2: 
     sns.set_palette('BuGn_d',6) 
    if k == 3: 
     sns.set_palette('Oranges_d',6) 

    for i in range(N): 
     j = -1 + 2*float(i)/N 
     volt[i] = j*(mu[1]-mu[0]) 
     state = evolve(s, ss, PP, beta, float(k) * D/3, B, j * mu, tmax) 

     for j in range(6): 
      states[j,i] = state[j] 

    for i in range(6): 
     y = np.zeros(N) 

     for j in range(N): 
      y[j] = states[i,j] 
     plt.plot(volt,y) 

plt.show() 

但是,情節總是最終在第一個調色板'Blues_d'中呈現。我該如何更改代碼,以便第一個曲線族用'Blues_d'繪製,第二個用'BuGn_d'繪製,第三個用'Oranges_d'繪製,但是在同一個圖中?

回答

2

我意識到這並不直接回答你的問題(部分原因是因爲你的代碼不是一個最小工作示例),但如果你打算使用seaborn那麼開始處理它可能是最有利的您的數據爲pandas。有很多seabornpandas一起編寫,所以這兩個jive真的很好。有多個調色板繪圖就是這樣一個例子,

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
import seaborn as sns; sns.set_style('whitegrid') 

x = np.linspace(0, 3, 31) 

U_df = pd.DataFrame({ 
    'u1': 2.6*x**2 - 4.5, 
    'u2': 2.5*x**2 - 4.9, 
    'u3': 2.3*x**2 - 4.7}, index=x) 

V_df = pd.DataFrame({ 
    'v1': -5.1*x + 11, 
    'v2': -4.9*x + 10, 
    'v3': -5.5*x + 12}, index=x) 

W_df = pd.DataFrame({ 
    'w1': -6.5*(x-1.6)**2 + 9.1, 
    'w2': -6.2*(x-1.8)**2 + 9.5, 
    'w3': -6.1*(x-1.5)**2 + 9.7}, index=x) 

fig, ax = plt.subplots() 

U_df.plot(ax=ax, color=sns.color_palette('Blues_d', 3)) 
V_df.plot(ax=ax, color=sns.color_palette('BuGn_d', 3)) 
W_df.plot(ax=ax, color=sns.color_palette('Oranges_d', 3)) 

ax.legend(ncol=3, loc='best') 

enter image description here

+0

謝謝你啊!我現在已經找到了解決我的問題的方法。 –

+0

@ThomasWening太棒了!如果它解決了您的問題,您可以考慮將我的答案標記爲正確和/或提高答案。 – lanery

0

也許How to use multiple colormaps in seaborn on same plot將是有用的。從mwaskom試試回答:

pal1 = sns.color_palette('rainbow', sum(ix1)) 
pal2 = sns.color_palette('coolwarm', sum(ix2)) 

fig, ax = plt.subplots(figsize=(4, 4)) 
ax.scatter(x_data[ix1], y[ix1], c=pal1, s=60, label="smaller") 
ax.scatter(x_data[ix2], y[ix2], c=pal2, s=60, label="larger") 
ax.legend(loc="lower right", scatterpoints=5) 

也就是說, 提取調色板,並把它傳遞給繪圖功能。 希望這是有用的。

相關問題