Advertisement
Korotkodul

Plot

Sep 9th, 2024 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. """Draw a curve from the experimental data.
  4.  
  5. Use experimental points and a computed linear regression coefficients
  6. to make a plot. Save it in a PDF file.
  7. ФАЛТ, МФТИ— Программирование на языке C++
  8. 93
  9. ©Преподаватели, 2023–2024 гг.
  10. Приложение Рисование графиков в Python
  11. 7 """
  12.  
  13. import argparse
  14. import matplotlib.pyplot as plt
  15. import pathlib
  16.  
  17.  
  18. def read_data(infile):
  19.     """Read an experimental data.
  20.  
  21.    Expected `infile` format is:
  22.    x1 y1
  23.    ...
  24.    xN yN
  25.    """
  26.  
  27.  
  28.     with infile.open() as f:
  29.         data = [[float(item) for item in line.split(maxsplit=1)]
  30.             for line in f.readlines() if not line.isspace()]
  31.         xx, yy = zip(*data)
  32.         return xx, yy
  33.     raise RuntimeError(f"Failed to read data from file ('{infile}')")
  34.  
  35.  
  36. def compute_curve(xx, args):
  37.     """Compute curve points using linear regression model."""
  38.  
  39.  
  40.     def y(x):
  41.         return args.a + args.b * x
  42.  
  43.  
  44.     xe = [xx[0], xx[-1]]
  45.     ye = [y(x) for x in xe]
  46.     return xe, ye
  47.  
  48.  
  49. def make_plot(points, curve, args):
  50.     """Compose a figure."""
  51.     plt.plot(*curve, "k")
  52.     plt.plot(*points, "k+", markersize=8)
  53.     plt.grid(True, which="both")
  54.     plt.title(f"y = ({args.a} ± {args.da}) + ({args.b} ± {args.db})*x")
  55.  
  56.  
  57. if __name__ == "__main__":
  58.     parser = argparse.ArgumentParser(
  59.         description="Make a plot of the linear regression y=a+b*x"
  60.                     "built from the data of an experiment")
  61.  
  62.     parser.add_argument("file", type=pathlib.Path,
  63.                         help="file with experiment data")
  64.  
  65.     parser.add_argument("a", type=float,
  66.                         help="estimate of the constant coefficient")
  67.     parser.add_argument("da", type=float,
  68.                         help="confidence band for `a`")
  69.  
  70.     parser.add_argument("b", type=float,
  71.                         help="estimate of the linear coefficient")
  72.     parser.add_argument("db", type=float,
  73.                         help="confidence band for `b`")
  74.  
  75.     args = parser.parse_args()
  76.  
  77.     xx, yy = read_data(args.file)
  78.     xe, ye = compute_curve(xx, args)
  79.     make_plot((xx, yy), (xe, ye), args)
  80.  
  81.     plt.savefig(args.file.with_suffix(".pdf").name, bbox_inches="tight")
  82. """
  83. C:\projects\lsm line_approx.txt | xargs python3 C:\Usersедор Евдокимов\PycharmProjects\sem\plot.py
  84. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement