简体   繁体   中英

Overlaying box plot and line plot seaborn

I am trying to overlay a box plot (series of box plot based on another variable) and a line plot of medians of that variable, on the same box plot. A simple code like below works perfectly fine.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

dfx=pd.DataFrame({'S':np.random.randint(10,100,9)*10,'C': 
['X','X','X','Y','Y','Y','Z','Z','Z']})

fig,ax=plt.subplots()
mx=dfx.groupby('C')['S'].median()
sns.boxplot(y='S',x='C',data=dfx,ax=ax)
sns.lineplot(y=mx.values,x=mx.index,ax=ax)
plt.show()

which gives

在此处输入图片说明

However, when I use the same code for this data I am reading from csv file, I just cannot the line plot to appear with the box plot.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

df=pd.read_csv('test.csv')
fig,ax=plt.subplots()
m=df.groupby('Start Date')['Score'].median()
sns.boxplot(y='Score',x='Start Date',data=df,ax=ax)
sns.lineplot(y=m.values,x=m.index,ax=ax)
plt.show()

gives this在此处输入图片说明

It doesn't matter whether the lineplot command is before or after boxplot, only box plot is shown. I see the line only if boxplot line is commented out.

I do not understand what is different about this data I am reading from csv that I cannot overlay line and box

PS: I know a simple workaround is replace the seaborn lineplot line with matplotlib line command

ax.plot(m.values,'r-o',linewidth=4)

and it gives the desired result:

在此处输入图片说明

I am just curious why seaborn lineplot is behaving the way it is.

I was facing a similar problem, I "solved it" by transforming my datetime column to string.

df_median.date = df_median.date.astype(str)
df_aux.date = df_aux.date.astype(str)

sns.set()


ax = sns.stripplot('date',
                  'value',
                  data=df_aux)

ax = sns.lineplot('date',
                  'value',
                  data=df_median,
                  ax=ax)
plt.xlabel("month")
plt.ylabel("values")
labels = ax.axes.get_xticklabels()
ax.axes.set_xticklabels(labels, rotation=45)

plt.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM