Python:是否可以根据比例为箱线图点着色,而不是用于箱线图本身?
Python: Is it possible to colour boxplot points according to a scale, not used for the boxplot itself?
我想得到类似于我使用 python 附加的图片的东西:
变量 X1 数据的箱线图(分为 2 个簇,并希望根据另一个变量(在本例中为变量 z)为簇中的每个点着色。
我不需要关于如何做这样的事情的脚本......只是想知道它是否可以使用 seaborn、matplotlib 等。我在 Whosebug 中找不到我的问题的答案。
谢谢!
与Seaborn, you can plot a stripplot()
or a swarmplot
on top of a boxplot
:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
sns.set()
N = 200
df = pd.DataFrame({'X1': np.random.normal(10, 10, N),
'Cluster': np.random.randint(1, 3, N),
'Z': np.random.uniform(1, 10, N)})
df['X1'] = df['X1'] + 10 * df['Cluster']
ax = sns.boxplot(data=df, x='Cluster', y='X1', color='dodgerblue', showfliers=False)
# sns.swarmplot(data=df, x='Cluster', y='X1', hue='Z', palette='Reds', ax=ax)
sns.stripplot(data=df, x='Cluster', y='X1', hue='Z', palette='Reds', ax=ax)
ax.legend_.remove()
plt.show()
我想得到类似于我使用 python 附加的图片的东西: 变量 X1 数据的箱线图(分为 2 个簇,并希望根据另一个变量(在本例中为变量 z)为簇中的每个点着色。 我不需要关于如何做这样的事情的脚本......只是想知道它是否可以使用 seaborn、matplotlib 等。我在 Whosebug 中找不到我的问题的答案。
谢谢!
与Seaborn, you can plot a stripplot()
or a swarmplot
on top of a boxplot
:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
sns.set()
N = 200
df = pd.DataFrame({'X1': np.random.normal(10, 10, N),
'Cluster': np.random.randint(1, 3, N),
'Z': np.random.uniform(1, 10, N)})
df['X1'] = df['X1'] + 10 * df['Cluster']
ax = sns.boxplot(data=df, x='Cluster', y='X1', color='dodgerblue', showfliers=False)
# sns.swarmplot(data=df, x='Cluster', y='X1', hue='Z', palette='Reds', ax=ax)
sns.stripplot(data=df, x='Cluster', y='X1', hue='Z', palette='Reds', ax=ax)
ax.legend_.remove()
plt.show()