Add Seaborn.palplot Axes To Existing Figure For Visualisation Of Different Color Palettes
Adding seaborn figures to subplots is usually done by passing 'ax' when creating the figure. For instance: sns.kdeplot(x, y, cmap=cmap, shade=True, cut=5, ax=ax) This method, howe
Solution 1:
As you've probably already found, there's little documentation for the palplot function, but I've lifted directly from the seaborn github repo here:
defpalplot(pal, size=1):
"""Plot the values in a color palette as a horizontal array.
Parameters
----------
pal : sequence of matplotlib colors
colors, i.e. as returned by seaborn.color_palette()
size :
scaling factor for size of plot
"""
n = len(pal)
f, ax = plt.subplots(1, 1, figsize=(n * size, size))
ax.imshow(np.arange(n).reshape(1, n),
cmap=mpl.colors.ListedColormap(list(pal)),
interpolation="nearest", aspect="auto")
ax.set_xticks(np.arange(n) - .5)
ax.set_yticks([-.5, .5])
# Ensure nice border between colors
ax.set_xticklabels([""for _ inrange(n)])
# The proper way to set no ticks
ax.yaxis.set_major_locator(ticker.NullLocator())
So, it doesn't return any axes or figure objects, or allow you to specify an axes object to write into. You could make your own, as follows, by adding the ax argument and a conditional in case it's not provided. Depending on context, you may need the included imports as well.
defmy_palplot(pal, size=1, ax=None):
"""Plot the values in a color palette as a horizontal array.
Parameters
----------
pal : sequence of matplotlib colors
colors, i.e. as returned by seaborn.color_palette()
size :
scaling factor for size of plot
ax :
an existing axes to use
"""import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
n = len(pal)
if ax isNone:
f, ax = plt.subplots(1, 1, figsize=(n * size, size))
ax.imshow(np.arange(n).reshape(1, n),
cmap=mpl.colors.ListedColormap(list(pal)),
interpolation="nearest", aspect="auto")
ax.set_xticks(np.arange(n) - .5)
ax.set_yticks([-.5, .5])
# Ensure nice border between colors
ax.set_xticklabels([""for _ inrange(n)])
# The proper way to set no ticks
ax.yaxis.set_major_locator(ticker.NullLocator())
This function should work like you expect when you include the 'ax' argument. To implement this in your example:
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
fig1 = plt.figure()
length, n_colors = 12, 50# amount of subplots and colors per subplot
start_colors = np.linspace(0, 3, length)
for i, start_color inenumerate(start_colors):
ax = fig1.add_subplot(length, 1, i + 1)
colors = sns.cubehelix_palette(
n_colors=n_colors, start=start_color, rot=0, light=0.4, dark=0.8
)
my_palplot(colors, ax=ax)
plt.show(fig1)
Post a Comment for "Add Seaborn.palplot Axes To Existing Figure For Visualisation Of Different Color Palettes"