Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color of seaborn plot line

I cant change color of a 2d line in seaborn. I have 2 lines in my plot and I want to assign different colors for both of them.

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, color='red', linewidth=2.5)
sns_plot = sns.lineplot(data=data2, linewidth=2.5)
sns_plot.figure.savefig("size_percentage_increase.png")

But color='red' does not change the color, why?

like image 200
Jonas Palačionis Avatar asked Oct 17 '19 12:10

Jonas Palačionis


People also ask

How do I change colors in seaborn?

You can build color palettes using the function sns. color_palette() . This function can take any of the Seaborn built-in palettes (see below). You can also build your own palettes by passing in a list of colors in any valid Matplotlib format, including RGB tuples, hex color codes, or HTML color names.

How do I change my line style in seaborn?

Customize line style by passing a list to “dashes” “dashes” parameter is used along with “style” parameter. You can pass a list of values to the “dashes” parameter. The number of elements in this list should match the number of categories in the column passed to “style” parameter.

How do you change the color of a Lineplot?

You can use the palette parameter to change the color of the lines for a multi-line line chart. Remember: in the section above about the hue parameter, I noted that you can create a multi-line lineplot by mapping a categorical variable to hue .


1 Answers

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')
like image 93
busybear Avatar answered Sep 27 '22 20:09

busybear