Changing color of seaborn plot line

You have a couple of options here. You can tweak your use of the color parameter, or you can use the palette parameter.


palette

Using palette would arguably be the cleaner approach. Ideally, you would make only one call to lineplot when using palette–with the use of hue parameter:

np.random.seed(42)
y0 = pd.DataFrame(np.random.random(20), columns=['value'])
y1 = pd.DataFrame(np.random.random(20), columns=['value'])
y = pd.concat([y0, y1], axis=0, keys=['y0', 'y1']).reset_index()
y = y.rename(columns={'level_0': 'group', 'level_1': 'x'})

sns.lineplot(data=y, x='x', y='value', hue="group", palette=['r', 'g'], linewidth=2.5)

enter image description here

But you could call lineplot for each line similar to how you did in your post:

sns.set(style="whitegrid")
data = pd.DataFrame(result_prices, columns=['Size percentage increase'])
data2 = pd.DataFrame(result_sizes, columns=['Size percentage increase'])
sns_plot = sns.lineplot(data=data, palette=['red'], linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")

color

Using the color parameter only appears to work with Series objects. This
would be most useful when plotting a single line, rather than when coloring multiple lines in a plot.

Since your dataframes seem to only be one column, you could (A) convert them to Series objects or (B) define x and y parameters when calling lineplot.

Documentation has an example toward the end.

A) Convert to Series

Using your code, it would look something like:

sns.set(style="whitegrid")
data = pd.Series(result_prices)
data2 = pd.Series(result_sizes)
sns_plot = sns.lineplot(data=data, color="red", linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")

As a minimal example:

np.random.seed(42)
y0 = pd.DataFrame(np.random.random(20), columns=['value'])
y1 = pd.DataFrame(np.random.random(20), columns=['value'])
sns.lineplot(data=y0['value'], color="r")
sns.lineplot(data=y1['value'])

B) Define x and y parameters

sns.lineplot(data=y0, x=y0.index, y='value', color="r")
sns.lineplot(data=y1, x=y0.index, y='value')

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)