正如您可能已经发现的那样,关于 palplot 函数的文档很少,但我直接从Seaborn github 仓库 https://github.com/mwaskom/seaborn/blob/ceee0324f9b352c6d4aa9eb145d6ad3f5bcaad41/seaborn/miscplot.py here:
def palplot(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 _ in range(n)])
# The proper way to set no ticks
ax.yaxis.set_major_locator(ticker.NullLocator())
因此,它不返回任何轴或图形对象,也不允许您指定要写入的轴对象。您可以通过添加 ax 参数和条件(以防未提供)来创建自己的参数,如下所示。根据上下文,您可能还需要包含的导入。
def my_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 is None:
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 _ in range(n)])
# The proper way to set no ticks
ax.yaxis.set_major_locator(ticker.NullLocator())
当您包含“ax”参数时,此函数应该像您期望的那样工作。要在您的示例中实现这一点:
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 in enumerate(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)