我正在使用seabornand 和
由于我在每個子圖中繪制了資料集的一個子集,因此我希望每個類的顏色保持一致,無論它出現在哪個圖中。對于類, 無論它出現在哪個子圖中blue的模式的顏色,它出現的任何圖的模式的顏色,等等......carblackbus
就像現在一樣,foot在 中是紅色的subplot(2, 2, 1),并且也car被讀入,subplot(2, 2, 2)盡管其余部分是一致的。
uj5u.com熱心網友回復:
對于這個用例,seaborn 允許使用字典作為調色板。字典將為每個色調值分配一種顏色。
以下是如何為您的資料創建此類字典的示例:
from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
df1 = pd.DataFrame({'tsne_one': np.random.randn(10),
'tsne-two': np.random.randn(10),
'mode': np.random.choice(['foot', 'metro', 'bike'], 10)})
df2 = pd.DataFrame({'tsne_one': np.random.randn(10),
'tsne-two': np.random.randn(10),
'mode': np.random.choice(['car', 'metro', 'bike'], 10)})
df3 = pd.DataFrame({'tsne_one': np.random.randn(10),
'tsne-two': np.random.randn(10),
'mode': np.random.choice(['foot', 'bus', 'metro', 'bike'], 10)})
df4 = pd.DataFrame({'tsne_one': np.random.randn(10),
'tsne-two': np.random.randn(10),
'mode': np.random.choice(['car', 'bus', 'metro', 'bike'], 10)})
modes = pd.concat([df['mode'] for df in (df1, df2, df3, df4)], ignore_index=True).unique()
colors = sns.color_palette('hls', len(modes))
palette = {mode: color for mode, color in zip(modes, colors)}
fig, axs = plt.subplots(2, 2, figsize=(12,6))
for df, ax in zip((df1, df2, df3, df4), axs.flatten()):
sns.scatterplot(x='tsne_one', y='tsne-two', hue='mode', data=df, palette=palette, legend='full', alpha=0.7, ax=ax)
plt.tight_layout()
plt.show()

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440770.html
標籤:Python matplotlib 海运 数据可视化 特纳
