PagMax
PagMax

Reputation: 8588

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

enter image description here

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 enter image description here

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

P.S: 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:

enter image description here

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

Upvotes: 2

Views: 14863

Answers (1)

aglotero
aglotero

Reputation: 31

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()

enter image description here

Upvotes: 0

Related Questions