Advertisement
DacCum

mathplotlib

May 23rd, 2022 (edited)
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #Завдання 1
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. x = np.linspace(0, 5, 500)
  6. y = -5 * np.cos(10 * x) * np.sin(3 * x) / (x**x)
  7.  
  8. plt.plot(x, y, color='#c8f04f', linestyle='-', linewidth=3, label='-5*cos(10*x)*sin(3*x)/(x^x)')
  9. plt.legend(loc='lower right')
  10. plt.xlabel('x')  # позначення вісі абсцис
  11. plt.ylabel('y')  # позначення е вісі ординат
  12. plt.title('My first normal plot')  # назва графіка
  13.  
  14. plt.show()
  15.  
  16.  
  17. №Завдання 2
  18. import csv
  19. import numpy as np
  20. import matplotlib.pyplot as plt
  21.  
  22.  
  23. def f(datafile_name: str, _country_name: str, start_years: int, end_years: int):
  24.     with open(datafile_name, newline='') as csvfile:
  25.         reader = csv.DictReader(csvfile)
  26.         tmp_y = np.array([], float)
  27.         for row in reader:
  28.             for i in range(start_years, end_years+1):
  29.                 s = str(i) + ' [YR' + str(i) + ']'
  30.                 if row['Country Name'] == _country_name:
  31.                     try:
  32.                         tmp_y = np.append(tmp_y, float(row[s]))
  33.                     except ValueError:
  34.                         tmp_y = np.append(tmp_y, 0)
  35.     return tmp_y
  36.  
  37.  
  38. y_ukr = f('Data.csv', 'Ukraine', 2002, 2020)
  39. y_can = f('Data.csv', 'Canada', 2002, 2020)
  40. x = np.arange(2002, 2021, 1)
  41.  
  42. fig1 = plt.figure()
  43. ax1 = fig1.add_subplot()
  44. ax1.plot(x, y_can, color='g', linestyle='-', linewidth=3, label='Canada')
  45. ax1.plot(x, y_ukr, color='r', linestyle='-', linewidth=3, label='Ukraine')
  46. ax1.legend(loc='lower right')
  47. ax1.set_xlabel('Years')  # позначення вісі абсцис
  48. ax1.set_ylabel('Life expectancy')  # позначення е вісі ординат
  49. ax1.set_title('Life expectancy at birth, total (years)')  # назва графіка
  50.  
  51. country_name = input("Enter name of country: ")
  52. fig2 = plt.figure()
  53. ax2 = fig2.add_subplot()
  54. y = f('Data.csv', country_name, 2002, 2020)
  55. ax2.bar(x, y, width=0.5)
  56.  
  57. ax2.set_xlabel('Years')  # позначення вісі абсцис
  58. ax2.set_ylabel('Life expectancy')  # позначення е вісі ординат
  59. ax2.set_title('Life expectancy at birth, total (years): ' + country_name)  # назва графіка
  60.  
  61. plt.show()
  62.  
  63.  
  64. №Завдання 3
  65. import json
  66. import numpy as np
  67. import matplotlib.pyplot as plt
  68.  
  69. with open('file_name.json', "r") as json_name:
  70.     y = np.array([], str)
  71.     x1 = np.array([], int)
  72.     x2 = np.array([], int)
  73.     for i, j in dict(json.load(json_name)).items():
  74.         y = np.append(y, i)
  75.         x1 = np.append(x1, j[0])
  76.         x2 = np.append(x2, j[1])
  77.  
  78. index_max1 = x1.argmax()
  79. index_max2 = x2.argmax()
  80. explode1 = [0] * len(y)
  81. explode1[index_max1] = 0.03
  82. explode2 = [0] * len(y)
  83. explode2[index_max2] = 0.03
  84.  
  85. fig1 = plt.figure()
  86. ax1 = fig1.add_subplot()
  87. ax1.pie(x1, labels=y, autopct='%1.1f%%', explode=explode1)
  88. ax1.set_title('Circulation')  # назва графіка
  89.  
  90. fig2 = plt.figure()
  91. ax2 = fig2.add_subplot()
  92. ax2.pie(x2, labels=y, autopct='%1.1f%%', explode=explode2)
  93. ax2.set_title('Price')  # назва графіка
  94.  
  95. plt.show()
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement