Create a dark, modified color palette in Siborn

I am creating a shape containing multiple plots using a consistent palette:

import matplotlib.pyplot as plt import seaborn as sns import math figure = plt.figure(1) x = range(1, 200) n_plots = 10 with sns.color_palette('Blues_d', n_colors=n_plots): for offset in range(n_plots): plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))]) figure.show() 

However, I would like to change the color palette. The tutorial states that I can add '_r' to the palette name to undo it, and '_d' to make it dark. But it looks like I can't do this together: '_r_d' , '_d_r' , '_rd' and '_dr' all produce errors. How to create a dark, reverse palette?

+7
python seaborn
source share
1 answer

I am answering my question to post details and explanations of the solution you are using, because the mwaskom suggestion required customization. Using

 with reversed(sns.color_palette('Blues_d', n_colors=n_plots)): 

throws AttributeError: __exit__ , I believe, because the with statement requires an object with the __enter__ and __exit__ methods, which the reversed iterator does not satisfy. If I use sns.set_palette(reversed(palette)) instead of the with operator, the number of colors on the graph is ignored (the default value is 6 - I don’t know why), even if the color scheme is executed. To solve this problem, I use the list.reverse() method:

 figure = plt.figure(1) x = range(1, 200) n_plots = 10 palette = sns.color_palette("Blues_d", n_colors=n_plots) palette.reverse() with palette: for offset in range(n_plots): plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))]) figure.show() 

Edit: I found that the reason the n_colors argument was ignored when calling set_palette was because the n_colors argument should also be specified in this call. Another solution:

 figure = plt.figure(1) x = range(1, 200) n_plots = 10 sns.set_palette(reversed(sns.color_palette("Blues_d", n_plots)), n_plots) for offset in range(n_plots): plt.plot(x, [offset + math.sin(float(i) / 10) for i in range(len(x))]) figure.show() 
+5
source share

All Articles