Во-первых, преобразование в hsv, как в mcolors.rgb_to_hsv(color)
, не выглядит очень полезным.
Теперь основная проблема, по-видимому, заключается в передаче цвета в виде списка или массива numpy ([1, 0, 0]
) смущает sns.distplot(..., color=color)
. Многие функции seaborn допускают использование одного цвета или списка цветов и не различают guish цвет, передаваемый как значения RGB, и массив. Обходной путь - преобразовать список в кортеж: sns.distplot(..., color=tuple(color))
.
Вот минимальный пример:
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
num_colors = 5
table_colors = plt.cm.BuPu(np.linspace(0, 0.5, num_colors))
fig, (ax_plot, ax_table) = plt.subplots(nrows=2)
for i in range(num_colors):
color = table_colors[i, 0:3]
# sns.distplot(np.random.normal(0, 1, 100), hist=False, color=color) # gives an error
sns.distplot(np.random.normal(0, 1, 100), hist=False, color=tuple(color), ax=ax_plot)
columns = list('abcdef')
num_columns = len(columns)
ax_table.table(cellText=np.random.randint(1, 1000, size=(num_colors, num_columns)) / 100,
colLabels=columns, loc='center', fontsize=20,
cellColours=np.repeat(table_colors, num_columns, axis=0).reshape(num_colors, num_columns, -1))
ax_table.axis('off')
plt.tight_layout()
plt.show()
![example plot](https://i.stack.imgur.com/CAbzg.png)
To change the color of the text, you can loop through the cells of the table. As these particular colors are not very visible on a white background, the cell background could be set to black.
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
num_colors = 5
table_colors = plt.cm.BuPu(np.linspace(0, 0.5, num_colors))
fig, (ax_plot, ax_table) = plt.subplots(nrows=2)
for i in range(num_colors):
color = table_colors[i, :]
# sns.distplot(np.random.normal(0, 1, 100), hist=False, color=color) # gives an error
sns.distplot(np.random.normal(0, 1, 100), hist=False, color=tuple(color), ax=ax_plot)
columns = list('abcdef')
num_columns = len(columns)
table = ax_table.table(cellText=np.random.randint(1, 1000, size=(num_colors, num_columns)) / 100,
colLabels=columns, loc='center', fontsize=20)
for i in range(num_colors):
for j in range(num_columns):
table[(i+1, j)].set_color('black') # +1: skip the table header
table[(i+1, j)].get_text().set_color(table_colors[i, :])
ax_table.axis('off')
plt.tight_layout()
plt.show()
изменение цвета текста