Advertisement
elena1234

histogram, lineplot and countplot in Python

Apr 28th, 2022 (edited)
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | Source Code | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import seaborn as sns
  5.  
  6. #Lineplot for every row
  7. transposed_df_liabilities = balance_df_liabilities.T
  8. transposed_df_liabilities_percentage = transposed_df_liabilities.div(transposed_df_liabilities.sum(axis=1).replace(0, 1), axis=0) * 100
  9. transposed_df_liabilities_percentage
  10.  
  11. transposed_df_liabilities.plot(subplots= True, figsize=(10,12), sharex= False, sharey=False)
  12. # OR in percentage
  13. ax = transposed_df_liabilities_percentage.plot(kind='line', subplots=True,figsize=(10,12), sharex= False, sharey=False, layout=(len(transposed_df_liabilities_percentage.columns), 1), legend=True)
  14. for subplot in ax:
  15.     for a in subplot:
  16.         a.yaxis.set_major_formatter(mtick.PercentFormatter(xmax =100))
  17. plt.tight_layout()  # Adjusts plot spacing
  18. plt.show()
  19.  
  20.  
  21. #Histograms with two different columns
  22. plt.figure(figsize = (20,25))
  23. sns.set(font_scale=2.5)
  24. fig, ax = plt.subplots()
  25. ax.bar(month_quantity_2017['TransactionDate'], month_quantity_2017['Quantity'])
  26.  
  27. # How to set the size
  28. sns.set(font_scale=8.7)
  29. fig, ax = plt.subplots(figsize=(80, 40))
  30. ax.bar(category_quantity_2017['ACM Application L2 Ver2'], category_quantity_2017['Quantity'])
  31. plt.xlabel('Product Category')
  32. plt.xticks(rotation=90)
  33. plt.ylabel('Quantity')
  34. plt.title('Sold Quantity from each category (ACM Application L2 Ver2) in 2017')
  35. plt.show()
  36.  
  37. #########################################################
  38. df_diesel["bins"] = pd.cut(df_diesel['Price BGN'], [0,5000,10000,15000,20000,2000000])
  39. ax = df_diesel["bins"].value_counts(normalize=True).plot.bar()
  40. ax.bar_label(ax.containers[0], label_type='edge')
  41. plt.xlabel('Price BGN')
  42. plt.ylabel('Percentages')
  43. plt.title("Diesel engines: Price Distribution")
  44. plt.show()
  45.  
  46. #####################################################
  47. # Count plot
  48. plt.figure(figsize=(12,8))
  49. df_actors_last.Actor.value_counts().head(20).plot(kind = 'bar', fontsize = 15)
  50. plt.show()
  51.  
  52. df.sort_values(by=['age'])
  53. plt.figure(figsize=(20,15), dpi = 200)
  54. sns.countplot(data = df.sort_values(by=['age']), x = 'age', palette = 'magma' )
  55. plt.show()
  56.  
  57. plt.figure(figsize=(12,8))
  58. sns.set(font_scale=1.1, palette="viridis")
  59. sns.countplot(data = listings.loc[listings.IPO_Year > 2000], x = "IPO_Year", hue = "Exchange")
  60. plt.legend(loc= "upper left")
  61. plt.savefig("list_countpl")
  62.  
  63. ####################################################
  64. sns.set(rc={'figure.figsize':(10.7,8.7)})
  65. plt.xticks(rotation=90)
  66. sns.set(style='darkgrid')
  67. plt.figure(figsize=(10,5), dpi = 200)
  68. sns.histplot(data=df, x = 'age', color = 'blue', linewidth = 2)
  69. plt.show()
  70.  
  71. #####################################################
  72. plt.style.available
  73. plt.style.use("seaborn) # how to choose the style
  74.  
  75. ######################################################
  76. titanic.age.hist(figsize(12,8), bins=80, xlabelsize=15, ylabelsize=15)
  77. plt.show()
  78.  
  79. ######################################################
  80. #  Create histogram
  81. sns.set(style='darkgrid')
  82. plt.figure(figsize=(5,8), dpi = 200)
  83. distribution = sns.histplot(data=df, x = 'salary', bins = 10, color = 'red', edgecolor = 'blue', linewidth = 4, kde = True)
  84. print(distribution)
  85.  
  86. ################################################## How to plot value counts
  87. plt.figure(figsize=(20,12))
  88. fandago['YEAR'].value_counts().plot(kind='barh')
  89. plt.show()
  90.  
  91. ##################################################
  92. plt.figure(figsize=(20,15), dpi = 200)
  93. sns.countplot(data = fandago, x = 'STARS_DIFF', palette = 'magma' )
  94. plt.title('Number of times a difference occurs')
  95. plt.show()
  96.  
  97. ##################################################
  98. xticks = [x for x in range(0,901, 50)]
  99. yticks = [y for y in range(0, 81, 5)]
  100. plt.style.use("classic")
  101.  
  102. titanic.age.plot(figsize = (12,8), fontsize= 13, c = "r", linestyle = "-",
  103.                 xlim = (0,900), ylim = (0,80), xticks = xticks, yticks = yticks, rot = 45)
  104. plt.title("Titanic - Ages", fontsize = 15)
  105. plt.legend(loc = 3, fontsize = 15)
  106. plt.xlabel("Passenger No", fontsize = 13)
  107. plt.ylabel("Age", fontsize = 13)
  108. #plt.grid()
  109. plt.show()
  110.  
  111. #########################################################
  112. plt.style.use("ggplot")
  113. plt.figure(figsize=(12, 8))
  114. plt.bar(df_converted['Year'], df_converted['Dividends'], color = 'b' )
  115. plt.xlabel('Year')
  116. plt.ylabel('Total Dividends')
  117. plt.title('Dividends by Year')
  118. plt.show()
  119.  
  120.  
  121. plt.figure(figsize=(12,8))
  122. sns.set(font_scale=1.0)
  123. sns.barplot(data = listings , x = 'Exchange', y = "Market_Cap", dodge = True)
  124. plt.show()
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement