Advertisement
Korotkodul

Задача 1. Распределения на любой вкус

Mar 21st, 2025 (edited)
580
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. plt.style.use("ggplot")
  4. from typing import Any
  5.  
  6. class ShapeMismatchError(Exception):
  7.   pass
  8.  
  9. def visualize_diagrams(
  10.     abscissa: np.ndarray,
  11.     ordinates: np.ndarray,
  12.     diagram_type: Any,
  13. ) -> None:
  14.     if abscissa.shape != ordinates.shape:
  15.       raise ShapeMismatchError
  16.     if diagram_type not in ['hist', 'violin', 'box']:
  17.       raise ValueError
  18.  
  19.     figure = plt.figure(figsize=(8, 8))
  20.     grid = plt.GridSpec(4, 4, wspace=space, hspace=space)
  21.     axis_scatter = figure.add_subplot(grid[:-1, 1:])
  22.     axis_scatter.scatter(abscissa, ordinates, color="cornflowerblue", alpha=0.5)
  23.  
  24.     axis_hist_vert = figure.add_subplot(
  25.         grid[:-1, 0],
  26.         sharey=axis_scatter,
  27.     )
  28.     axis_hist_hor = figure.add_subplot(
  29.         grid[-1, 1:],
  30.         sharex=axis_scatter,
  31.     )
  32.  
  33.     def hist() -> None:
  34.       nonlocal axis_hist_vert
  35.       nonlocal axis_hist_hor
  36.       nonlocal abscissa
  37.       nonlocal ordinates
  38.       axis_hist_hor.hist(
  39.           abscissa,
  40.           bins=50,
  41.           color="cornflowerblue",
  42.           density=True,
  43.           alpha=0.5,
  44.       )
  45.       axis_hist_vert.hist(
  46.           ordinates,
  47.           bins=50,
  48.           color="cornflowerblue",
  49.           orientation="horizontal",
  50.           density=True,
  51.           alpha=0.5,
  52.       )
  53.       axis_hist_hor.invert_yaxis()
  54.       axis_hist_vert.invert_xaxis()
  55.       return None
  56.  
  57.     def violin() -> None:
  58.       nonlocal axis_hist_vert
  59.       nonlocal axis_hist_hor
  60.       nonlocal abscissa
  61.       nonlocal ordinates
  62.       violin_parts_x = axis_hist_hor.violinplot(
  63.           abscissa,
  64.           vert=False,
  65.           showmedians=True,
  66.       )
  67.       for body in violin_parts_x["bodies"]:
  68.           body.set_facecolor("cornflowerblue")
  69.           body.set_edgecolor("blue")
  70.       for part in violin_parts_x:
  71.           if part == "bodies":
  72.               continue
  73.           violin_parts_x[part].set_edgecolor("cornflowerblue")
  74.  
  75.       violin_parts_y = axis_hist_vert.violinplot(
  76.           ordinates,
  77.           vert=True,
  78.           showmedians=True,
  79.       )
  80.       for body in violin_parts_y["bodies"]:
  81.           body.set_facecolor("cornflowerblue")
  82.           body.set_edgecolor("blue")
  83.       for part in violin_parts_y:
  84.           if part == "bodies":
  85.               continue
  86.           violin_parts_y[part].set_edgecolor("cornflowerblue")
  87.      
  88.       axis.set_yticks([])
  89.       return None
  90.  
  91.     def box() -> None:
  92.       nonlocal axis_hist_vert
  93.       nonlocal axis_hist_hor
  94.       nonlocal abscissa
  95.       nonlocal ordinates
  96.       axis_hist_hor.boxplot(
  97.           abscissa,
  98.           vert=False,
  99.           patch_artist=True,
  100.           boxprops=dict(facecolor="lightsteelblue"),
  101.           medianprops=dict(color="k"),
  102.       )
  103.       axis_hist_vert.boxplot(
  104.           ordinates,
  105.           vert=True,
  106.           patch_artist=True,
  107.           boxprops=dict(facecolor="lightsteelblue"),
  108.           medianprops=dict(color="k"),
  109.       )
  110.       return None
  111.  
  112.     if diagram_type == 'hist':
  113.       hist()
  114.     elif diagram_type == 'violin':
  115.       violin()
  116.     elif diagram_type == 'box':
  117.       box()
  118.  
  119.     plt.show()
  120.     return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement