Advertisement
tuomasvaltanen

Untitled

Mar 23rd, 2021 (edited)
634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.07 KB | None | 0 0
  1. # lecture 5, seaborn basics
  2.  
  3. import seaborn as sns
  4. import pandas as pd
  5. import matplotlib.pyplot as plt
  6.  
  7. tips = sns.load_dataset('tips')
  8.  
  9. tips['total_cost'] = tips['total_bill'] + tips['tip']
  10.  
  11. # reset plotting image
  12. plt.clf()
  13.  
  14. # create the actual plot, bins controls the number
  15. # of categories (bars) in the plot!
  16. sns.distplot(tips['total_cost'], bins=30)
  17.  
  18. # show the plot to user
  19. plt.figure()
  20.  
  21.  
  22. # draw second plot to its own image
  23. plt.clf()
  24.  
  25. # if you don't want the kernel density estimation line,
  26. # you can disable it!
  27. sns.distplot(tips['tip'], kde=False)
  28. plt.figure()
  29.  
  30. # first clear, then plot, then show figure
  31. plt.clf()
  32. sns.countplot(x='smoker', data=tips)
  33. plt.figure()
  34.  
  35. # exact numbers of smokers and non-smokers
  36. print(tips.groupby('smoker').count())
  37.  
  38. plt.clf()
  39. sns.jointplot(x='total_bill', y='tip', data=tips)
  40. plt.figure()
  41.  
  42. plt.clf()
  43. sns.jointplot(x='total_bill', y='tip', data=tips, kind="reg")
  44. plt.figure()
  45.  
  46.  
  47. # NEW FILE
  48.  
  49. import seaborn as sns
  50. import pandas as pd
  51. import matplotlib.pyplot as plt
  52. import numpy as np
  53.  
  54.  
  55. tips = sns.load_dataset('tips')
  56. flights = sns.load_dataset('flights')
  57.  
  58. plt.clf()
  59. sns.pairplot(tips)
  60. plt.figure()
  61.  
  62. tips_correlations = tips.corr()
  63.  
  64. plt.clf()
  65. sns.pairplot(flights)
  66. plt.figure()
  67.  
  68. flights_correlations = flights.corr()
  69.  
  70. plt.clf()
  71. # by default, barplot shows the average for selected category
  72. sns.barplot(x='smoker', y='total_bill', data=tips, estimator=np.median)
  73. plt.figure()
  74.  
  75. print(tips['smoker'].value_counts())
  76. print(tips['day'].value_counts())
  77. print(tips['time'].value_counts())
  78.  
  79. plt.clf()
  80. sns.boxplot(x='time', y='total_bill', data=tips, hue='day')
  81. plt.figure()
  82.  
  83. plt.clf()
  84. sns.violinplot(x='day', y='tip', data=tips, hue='smoker', split=True)
  85. plt.figure()
  86.  
  87. # NEW FILE
  88.  
  89. import seaborn as sns
  90. import pandas as pd
  91. import matplotlib.pyplot as plt
  92. import numpy as np
  93.  
  94. tips = sns.load_dataset('tips')
  95.  
  96. plt.clf()
  97.  
  98. sns.stripplot(x='day', y='total_bill',
  99.               data=tips, jitter=True, hue='smoker', split=True)
  100.  
  101. plt.figure()
  102.  
  103.  
  104. plt.clf()
  105. sns.swarmplot(x='day', y='total_bill', data=tips)
  106. plt.figure()
  107.  
  108. plt.clf()
  109.  
  110. sns.violinplot(x='day', y='total_bill', data=tips)
  111. sns.swarmplot(x='day', y='total_bill', data=tips, color='purple')
  112.  
  113. plt.figure()
  114.  
  115. # NEW FILE
  116.  
  117. import seaborn as sns
  118. import pandas as pd
  119. import matplotlib.pyplot as plt
  120. import numpy as np
  121.  
  122. flights = sns.load_dataset('flights')
  123. tips = sns.load_dataset('tips')
  124.  
  125. flights_correlations = flights.corr()
  126. tips_correlations = tips.corr()
  127.  
  128.  
  129. plt.clf()
  130. sns.heatmap(tips_correlations, annot=True, cmap="magma")
  131. plt.figure()
  132.  
  133.  
  134. flights_pivot = flights.pivot_table(index='month', columns='year',
  135.                                     values='passengers')
  136.  
  137. plt.clf()
  138. sns.heatmap(flights_pivot, linecolor='black',
  139.             linewidths=1)
  140.  
  141. plt.figure()
  142.  
  143. plt.clf()
  144. sns.clustermap(flights_pivot, cmap='coolwarm', standard_scale=1)
  145. plt.figure()
  146.  
  147. plt.clf()
  148. sns.lmplot(x='total_bill', y='tip',
  149.            data=tips, hue='smoker', row="sex",
  150.            markers=['o', 'v'], scatter_kws={'s':100})
  151. plt.figure()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement