seaborn pointplot above swarmplot - python

I try to plot group wise median values using seaborn's pointlot on top of a swarmplot. Even though I call pointPlot second, the point plot ends up behind the swarmplot. How can I change the 'layer order' such that the point plot is in front of the swarmplot?
datDf=pd.DataFrame({'values':np.random.randint(0,100,100)})
datDf['group']=np.random.randint(0,5,100)
sns.swarmplot(data=datDf,x='group',y='values')
sns.pointplot(data=datDf,x='group',y='values',estimator=np.median,join=False)

Use zorder property to set proper drawing order.
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pylab as plt
datDf=pd.DataFrame({'values':np.random.randint(0,100,100)})
datDf['group']=np.random.randint(0,5,100)
sns.swarmplot(data=datDf,x='group',y='values',zorder=1)
sns.pointplot(data=datDf,x='group',y='values',estimator=np.median,join=False, zorder=100)
plt.show()

Related

Changing color and marker of dataset using seaborn jointplot

I want to add an additional variable to the plot listed below. At the moment I have a different colour of marker corresponding to a different metal. But for every metal, there is a different geometry, so I would like to add a marker for every colour (e.g. red dot and red square). When I add "style=POM" I get this error message:
ValueError: Could not interpret value POM for parameter style
Any help on this would be appreciated!
Example .csv file:
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pyplot as plt
dfdata=pd.read_csv('C:/Users/2586376t/conda/BOTH.csv')
sns.jointplot(data=dfdata, x="MO", y="ORB", hue="METAL")
plt.show()
style=POM tries to set the parameter style to POM. In seaborn jointplot, the style parameter does not exist
Ref : https://seaborn.pydata.org/generated/seaborn.jointplot.html
If you want 1 plot with different colors corresponding to different metals and different markers corresponding to different POM values then your solution is to use a scatterplot like this :
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pyplot as plt
dfdata=pd.read_csv('C:/Users/2586376t/conda/BOTH.csv')
plot = sns.scatterplot(data=dfdata, x="MO", y="ORB", hue="METAL" , style=dfdata['POM'])
plt.show()

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

Negative values in joy plot from non-negative data set

I am creating a joyplot using joypy.
All my data is between[0,1].
But I get a big range of negative values in the graph:
import joypy
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import cm
import matplotlib.ticker as ticker
import matplotlib
matplotlib.use('TkAgg')
iris = pd.read_csv("1_5.csv")
fig, axes = joypy.joyplot(iris)
x = [0,0.25,0.5,0.75,1]
plt.xticks(x)
plt.show()
It isn't clear that your xticks are in any way tied to the actual joyplot itself (ie, you've created arbitrary x-ticks and placed them on the plot).
Are tick marks not represented on the plot originally (similar plots I've seen all have them by default)?

Only show round numbers on x-axis in point plot

If I use the following code I end up with an overcrowded x-axis. I would like to show only every 10th number on the x axis. Meaning [0,10,...].
Any idea how to do this?
import pandas as pd
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt
a = pd.DataFrame({'y':np.random.randn(100)})
a['time']=a.index
ax = sns.pointplot(x='time', y="y", data=a)
plt.show()
You may decide not to use a pointplot at all. A usual lineplot seems to suffice.
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
a = pd.DataFrame({'y':np.random.randn(100)})
plt.plot(a.index, a.y)
plt.show()
Now this gives ticks at steps of 20. The easiest option here would be to use
plt.xticks(range(0,101,10))
to get the steps of 10. Or equally possible,
plt.gca().locator_params(nbins=11)
to devide the axis into 11 bins.
Of course the use of an appropriate locator would be equally possible.

Joining points in multi-series seaborn pointplot

i am having some trouble with a seaborn pointplot.
I am to plot the Temperature vs. growth rate of four kinds of bacteria, so that each type has its own graph, but all four are in the same plot. The thing is, i cannot connect the individual points, i can only get the individual points.
My code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats, integrate
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import seaborn as sns
dataSorted=data.sort_values(['Temperature','Growth_rate'],ascending=[True,True])
plt.subplots()
ax2=sns.pointplot(x='Temperature',y='Growth_rate', hue='Bacteria' ,data=dataSorted,scale=0.7,join=True)
axes2=ax2.axes
axes2.set_xlim(10,60)
axes2.set_ylim(0,1.5)
axes2.set_xticks(np.arange(1,7)*10)
axes2.set_xticklabels(np.arange(1,7)*10)
The output is exactly as specified, apart from the lines between points:
My plot - without lines
I have no idea how to fix this, i have even set the "join" parameter manually, even though it is set as True by default.

Categories