Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import numpy as np
- plt.style.use("ggplot")
- from typing import Any
- class ShapeMismatchError(Exception):
- pass
- def visualize_diagrams(
- abscissa: np.ndarray,
- ordinates: np.ndarray,
- diagram_type: Any,
- ) -> None:
- if abscissa.shape != ordinates.shape:
- raise ShapeMismatchError
- if diagram_type not in ['hist', 'violin', 'box']:
- raise ValueError
- figure = plt.figure(figsize=(8, 8))
- grid = plt.GridSpec(4, 4, wspace=space, hspace=space)
- axis_scatter = figure.add_subplot(grid[:-1, 1:])
- axis_scatter.scatter(abscissa, ordinates, color="cornflowerblue", alpha=0.5)
- axis_hist_vert = figure.add_subplot(
- grid[:-1, 0],
- sharey=axis_scatter,
- )
- axis_hist_hor = figure.add_subplot(
- grid[-1, 1:],
- sharex=axis_scatter,
- )
- def hist() -> None:
- nonlocal axis_hist_vert
- nonlocal axis_hist_hor
- nonlocal abscissa
- nonlocal ordinates
- axis_hist_hor.hist(
- abscissa,
- bins=50,
- color="cornflowerblue",
- density=True,
- alpha=0.5,
- )
- axis_hist_vert.hist(
- ordinates,
- bins=50,
- color="cornflowerblue",
- orientation="horizontal",
- density=True,
- alpha=0.5,
- )
- axis_hist_hor.invert_yaxis()
- axis_hist_vert.invert_xaxis()
- return None
- def violin() -> None:
- nonlocal axis_hist_vert
- nonlocal axis_hist_hor
- nonlocal abscissa
- nonlocal ordinates
- violin_parts_x = axis_hist_hor.violinplot(
- abscissa,
- vert=False,
- showmedians=True,
- )
- for body in violin_parts_x["bodies"]:
- body.set_facecolor("cornflowerblue")
- body.set_edgecolor("blue")
- for part in violin_parts_x:
- if part == "bodies":
- continue
- violin_parts_x[part].set_edgecolor("cornflowerblue")
- violin_parts_y = axis_hist_vert.violinplot(
- ordinates,
- vert=True,
- showmedians=True,
- )
- for body in violin_parts_y["bodies"]:
- body.set_facecolor("cornflowerblue")
- body.set_edgecolor("blue")
- for part in violin_parts_y:
- if part == "bodies":
- continue
- violin_parts_y[part].set_edgecolor("cornflowerblue")
- axis.set_yticks([])
- return None
- def box() -> None:
- nonlocal axis_hist_vert
- nonlocal axis_hist_hor
- nonlocal abscissa
- nonlocal ordinates
- axis_hist_hor.boxplot(
- abscissa,
- vert=False,
- patch_artist=True,
- boxprops=dict(facecolor="lightsteelblue"),
- medianprops=dict(color="k"),
- )
- axis_hist_vert.boxplot(
- ordinates,
- vert=True,
- patch_artist=True,
- boxprops=dict(facecolor="lightsteelblue"),
- medianprops=dict(color="k"),
- )
- return None
- if diagram_type == 'hist':
- hist()
- elif diagram_type == 'violin':
- violin()
- elif diagram_type == 'box':
- box()
- plt.show()
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement