How to use nautical dot and violin drawing in the same drawing? (change arrows and pointplot marker)

I am trying to create violins that show confidence intervals for the mean. I thought an easy way to do this would be to build a superscript plan on top of the scripting, but that doesn't work as they seem to use different indices for the xaxis, as in this example:

import matplotlib.pyplot as plt
import seaborn as sns   

titanic = sns.load_dataset("titanic")
titanic.dropna(inplace=True)
fig, (ax1,ax2,ax3) = plt.subplots(1,3, sharey=True, figsize=(12,4))
#ax1
sns.pointplot("who", "age", data=titanic, join=False,n_boot=10, ax=ax1)
#ax2
sns.violinplot(titanic.age, groupby=titanic.who, ax=ax2)
#ax3
sns.pointplot("who", "age", data=titanic, join=False, n_boot=10, ax=ax3)
sns.violinplot(titanic.age, groupby=titanic.who, ax=ax3)
ax3.set_xlim([-0.5,4])

      

enter image description here

print(ax1.get_xticks(), ax2.get_xticks())

      

gives: [0 1 2] [1 2 3]

Why don't these plots assign the same number of letters to the "who" variable and is there a way to change this?

I also wonder if there is anyway I can change the marker for the pointplot, because as you can see in the picture, the point is so large that it covers the entire confidence interval. I would like to just have a horizontal line if possible.

+3


source to share


2 answers


violinplot

accepts an argument positions

that you can use to place the violins somewhere else (they currently just inherit the default matplotlib boxplot positions).



pointplot

takes an argument markers

that you can use to change the way the point is rendered.

+3


source


I am posting my final solution here. The reason I wanted to start this plot was to display information about the shape of the distribution, the shift in funds, and the emissions in the same figure. With mwaskom pointers and some other tricks, I finally got what I was looking for. enter image description here The left shape on the left is a comparison with all data points plotted as lines, and the right one is my last shape. The thick gray line in the middle of the violin is the loaded 99% confidence interval of the mean, which is a white horizontal line like from a pointplot. The three dashed lines are the standard 25th, 50th and 75th percentile, and the lines outside are the square box mustache caps that I have built on top of the violin plot. Individual data points appear as lines outside of those points, as my data usually has a few extreme ones that I need to manually remove, like the two points on the fiddle below.

enter image description here

I am currently going to continue creating histograms and boxes in addition to these reinforced violins, but I hope to find that all information is accurately captured in the violin and that I can start and rely on it as my main starting point for data mining. Here is the final code for generating graphs in case someone finds them useful (or finds something that could be improved). Many settings to the box.



import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns  

#change the linewidth which to get a thicker confidence interval line
mpl.rc("lines", linewidth=3)
df = sns.load_dataset("titanic")
df.dropna(inplace=True)
x = 'who'
y = 'age'
fig, (ax1,ax2) = plt.subplots(1,2, sharey=True, figsize=(12,6))
#Left hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax1, inner='stick')
#Right hand plot
sns.violinplot(df[y], groupby=df[x], ax=ax2, positions=0)
sns.pointplot(df[x],df[y], join=False, ci=99, n_boot=1000, ax=ax2, color=[0.3,0.3,0.3], markers=' ')
df.boxplot(y, by=x, sym='_', ax=ax2, showbox=False, showmeans=True, whiskerprops={'linewidth':0},
    medianprops={'linewidth':0}, flierprops={'markeredgecolor':'k', 'markeredgewidth':1},
    meanprops={'marker':'_', 'color':'w', 'markersize':6, 'markeredgewidth':1.5},
    capprops={'linewidth':1, 'color':[0.3,0.3,0.3]}, positions=[0,1,2])
#One could argue that this is not beautiful
labels = [item.get_text() + '\nn=' + str(df.groupby(x).size().loc[item.get_text()]) for item in ax2.get_xticklabels()]
ax2.set_xticklabels(labels)
#Clean up
fig.suptitle('')
ax2.set_title('')
fig.set_facecolor('w')

      

Edit: Added 'n ='

+5


source







All Articles