Advertisement
elena1234

SEABORN plots - confidence interval, regression and correlation in Python

Mar 25th, 2023 (edited)
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.67 KB | None | 0 0
  1. # Count and Hist plot
  2. plt.figure(figsize = (12,8))
  3. sns.set(font_scale = 2, palette = "viridis")
  4. sns.countplot(data = titanic, x = "sex", hue = "pclass")
  5. plt.show()
  6.  
  7.  
  8. sns.histplot(data = penguins, x = "body_mass_g", kde = True)
  9.  
  10.  
  11. sns.histplot(data = penguins, x = "body_mass_g", kde = True, hue = "species")
  12.  
  13. #############################################################################################################################
  14. # Categorical plots
  15. plt.figure(figsize=(12,8))
  16. sns.set(font_scale=1.5)
  17. sns.stripplot(data = titanic, x = "sex", y = "age", jitter = True, hue = "pclass", dodge = True)
  18. plt.show()
  19.  
  20.  
  21. plt.figure(figsize=(12,8))
  22. sns.set(font_scale=1.5)
  23. sns.swarmplot(data = titanic, x = "sex", y = "age", hue = "pclass", dodge = True)
  24. plt.show() # the plot is the same as previous plot, but we can see the distribution
  25.  
  26.  
  27. plt.figure(figsize=(12,8))
  28. sns.set(font_scale=1.5)
  29. sns.violinplot(data = titanic, x = "sex", y = "age", hue = "pclass", dodge = True)
  30. sns.swarmplot(data = titanic, x = "sex", y = "age", hue = "pclass", dodge = True, color = "black")
  31. plt.show() # we can see the distribution
  32.  
  33.  
  34. plt.figure(figsize=(12,8))
  35. sns.set(font_scale=1.5)
  36. sns.violinplot(data = titanic, x = "pclass", y = "age", hue = "sex", dodge = True, split = True )
  37. plt.show()
  38.  
  39.  
  40. plt.figure(figsize=(12,8))
  41. sns.set(font_scale=1.5)
  42. sns.barplot(data = titanic, x = "pclass", y = "age", hue = "sex", dodge = True)
  43. plt.show() # with confidence intervals
  44.  
  45.  
  46. plt.figure(figsize=(12,8))
  47. sns.set(font_scale=1.5)
  48. sns.pointplot(data = titanic, x = "pclass", y = "age", hue = "sex", dodge = True)
  49. plt.show() # only CONFIDENCE INTERVALS for each groups
  50.  
  51. ##########################################################################################################################
  52. # Joint / Regression plot
  53. sns.set(font_scale=1.5)
  54. sns.regplot(data = df, x = 'total_spend', y = 'sales')
  55.  
  56. sns.jointplot(data = titanic, x = "age", y = "fare", height = 8, kind = "kde")
  57. plt.show()
  58.  
  59.  
  60. sns.set(font_scale=1.5)
  61. sns.jointplot(data = titanic, x = "age", y = "fare", height = 8, kind = "scatter")
  62. plt.show()
  63.  
  64.  
  65. sns.set(font_scale=1.5)
  66. sns.jointplot(data = titanic, x = "age", y = "fare", height = 8, kind = "hex")
  67. plt.show()
  68.  
  69.  
  70. sns.set(font_scale=1.5)
  71. sns.jointplot(data = titanic, x = "age", y = "fare", height = 8, kind = "reg")
  72. plt.show()
  73.  
  74.  
  75. sns.set_style('whitegrid')
  76. sns.lmplot(x ='total_bill', y ='tip', data = dataset,
  77.            hue ='sex', markers = ['o', 'v']) # LINEAR REGRESSION PLOT
  78.  
  79.  
  80. sns.set_style('whitegrid')
  81. sns.lmplot(x ='total_bill', y ='tip', data = dataset, hue ='sex',
  82.            markers =['o', 'v'], scatter_kws ={'s':100},
  83.            palette ='plasma')
  84.  
  85.  
  86. sns.set(font_scale=1.5)
  87. sns.lmplot(data = titanic, x = "age", y = "fare", aspect= 1, height=8, col = "sex")
  88. plt.show() # we are creating two subplots and actually two columns for male and female
  89.  
  90.  
  91. sns.set(font_scale=1.5)
  92. sns.lmplot(data = titanic, x = "age", y = "fare", aspect= 1, height=8, row = "sex")
  93. plt.show() # we are creating two subplots and actually two rows for male and female
  94.  
  95.  
  96. sns.lmplot(x ='total_bill', y ='tip', data = dataset,
  97.            col ='sex', row ='time', hue ='smoker')
  98.  
  99.  
  100. sns.lmplot(x ='total_bill', y ='tip', data = dataset, col ='sex',
  101.            row ='time', hue ='smoker', aspect = 0.6,
  102.            size = 4, palette ='coolwarm')
  103.  
  104.  
  105. sns.set(font_scale=1.5)
  106. sns.lmplot(data = titanic, x = "age", y = "survived", aspect= 1, height=8, col = "sex", logistic= True)
  107. plt.show() # plot for LOGISTIC REGRESSION
  108.  
  109.  
  110. ###########################################################################################################################
  111. # Heatmaps and Matrixplots
  112. pd.crosstab(titanic.sex, titanic.pclass)
  113. plt.figure(figsize=(12,8))
  114. sns.set(font_scale=1.4)
  115. sns.heatmap(pd.crosstab(titanic.sex, titanic.pclass), annot= True, fmt = "d", cmap = "Reds", vmax = 150)
  116. plt.show()
  117.  
  118.  
  119. pd.crosstab(titanic.sex, titanic.pclass, values= titanic.survived, aggfunc= "mean")
  120. plt.figure(figsize=(12,8))
  121. sns.set(font_scale=1.4)
  122. sns.heatmap(pd.crosstab(titanic.sex, titanic.pclass, values= titanic.survived, aggfunc= "mean"), annot= True, cmap = "Reds")
  123. plt.show()
  124.  
  125.  
  126. titanic.corr()
  127. plt.figure(figsize=(12,8))
  128. sns.set(font_scale=1.4)
  129. sns.heatmap(titanic.corr(), annot= True, cmap = "Reds")
  130. plt.show()
  131.  
  132. #########################################################################################################################
  133. # Seaborn pairplot
  134. plt.figure(figsize=(12,8))
  135. sns.set(font_scale=1.4)
  136. sns.pairplot(cars, kind = 'scatter')
  137. plt.show()
  138.  
  139.  
  140. sns.set(font_scale=1.5)
  141. sns.pairplot(data = cars[["gpm", "horsepower", "origin"]], kind = "scatter", hue = "origin", aspect = 2, height = 3)
  142. plt.show()
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement