问题描述
我有一个如下所示的数据框 df
:
I have a dataframe df
that looks like this:
df.head()
id feedback nlp_model similarity_score
0xijh4 1 tfidf 0.36
0sdnj7 -1 lda 0.89
kjh458 1 doc2vec 0.78
....
我想使用seaborn在 model
列中的每个唯一值上以箱形图的形式绘制 similairty_score
与反馈的关系图: tfidf
, lda
, doc2vec
.我的代码如下:
I want to plot similairty_score
versus feedback in a boxplot form using seaborn for each of the unique values in the model
column: tfidf
, lda
, doc2vec
. My code for this is as follows:
fig, ax = plt.subplots(figsize=(10,8))
ax = sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='tfidf'])
ax = sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='tfidf'], color="0.25")
fig, ax = plt.subplots(figsize=(10,8))
ax = sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='lda'])
ax = sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='lda'], color="0.25")
fig, ax = plt.subplots(figsize=(10,8))
ax = sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='doc2vec'])
ax = sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='doc2vec'], color="0.25")
plt.show()
问题是,这会在另一个之上创建3个地块.
The problem is this creates 3 plots one on top of the other.
如何生成相同的图,但全部生成在一条直线上,其中一个轴仅在最左侧的图上标记相似度得分",而在每个图的正下方标记反馈"轴标签?
How can I generate these same plots but all on a single line, with one axis marking "Similarity Score" on the left most plot only, and "Feedback" axis label directly below each plot?
推荐答案
每次绘制时,您都在创建新图形.因此,您可以删除对 plt.subplots()
You are creating new figures, each time you plot. So you can remove all but one of the calls to plt.subplots()
seaborn swarmplot()
和 boxplot()
接受 ax
参数,即您可以告诉它要绘制到的轴.因此,使用以下命令创建图形,子图形和轴:
The seaborn swarmplot()
and boxplot()
accept ax
arguments i.e. you can tell it which axes to plot to. Therefore, create your figure, subplots and axes using:
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
然后您可以执行以下操作:
Then you can do something like:
sns.boxplot(x="x_vals", y="y_vals", data=some_data, ax=ax1)
然后,您可以按照自己的意愿操作轴.例如,仅在某些子图等上删除y轴标签.
You can then manipulate the axes as you see fit. For example, removing the y axis labels only on certain subplots etc.
fig, (ax1, ax2, ax3) = plt.subplots(1,3,figsize=(10,8))
sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='tfidf'], ax=ax1)
sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='tfidf'], color="0.25", ax=ax1)
sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='lda'], ax=ax2)
sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='lda'], color="0.25", ax=ax2)
ax2.set_ylabel("") # remove y label, but keep ticks
sns.boxplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='doc2vec'], ax=ax3)
sns.swarmplot(x="feedback", y="similarity_score", data=df[df.nlp_model=='doc2vec'], color="0.25", ax=ax3)
ax3.set_ylabel("") # remove y label, but keep ticks
plt.show()
这篇关于如何使用Seaborn连续绘制多个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!