1

In my python program I have three families of curves to each I want to assign a seaborn colour palette and plot them all into one single plot.

My solution so far is the following code.

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()

However, the plot always ends up being rendered in the first palette 'Blues_d'. How can I change the code, so that the first family of curves is plotted with 'Blues_d', the second with 'BuGn_d' and the third with 'Oranges_d' but in the same figure?

2 Answers2

2

I realize this doesn't directly answer your question (in part because your code is not quite a minimum working example), but if you're going to be using seaborn then it is probably in your best interest to start handling your data with pandas. A lot of seaborn is written with pandas in mind and so the two jive really well. Plotting with multiple color palettes is one such example,

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

lanery
  • 5,222
  • 3
  • 29
  • 43
0

Maybe How to use multiple colormaps in seaborn on same plot will be usefull. Try answer from 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)

That is, extract the palette and pass it to the plotting function. Hope this is useful.

Community
  • 1
  • 1
Gustav Rasmussen
  • 3,720
  • 4
  • 23
  • 53