-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_draw_violinplot.py
91 lines (68 loc) · 2.31 KB
/
plot_draw_violinplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## -- Script Meta Data --
## Author : julien
## Created : 2018-01-15 10:16:35.712913
## Comment : Create fancy violinplots
##
## ------------------------------
from pathlib import Path
import sys, os, re, json, math
import numpy as np
import pandas as pd
from calendar import month_abbr as months
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
from hydrodiy.io import iutils
from hydrodiy.plot import putils, violinplot
#----------------------------------------------------------------------
# Config
#----------------------------------------------------------------------
# Configure data generation
nvar = 5
nval = 1000
# Configure times series data generation
nens = 1000
nts = 50
putils.set_mpl(font_size=10)
#----------------------------------------------------------------------
# Folders
#----------------------------------------------------------------------
source_file = Path(__file__).resolve()
froot = source_file.parent
fimg = froot / "images" / "violinplots"
fimg.mkdir(exist_ok=True, parents=True)
#----------------------------------------------------------------------
# Logging
#----------------------------------------------------------------------
basename = source_file.stem
LOGGER = iutils.get_logger(basename)
#----------------------------------------------------------------------
# Get data
#----------------------------------------------------------------------
# Create a random set of data
data = np.random.normal(size=(nval, nvar))
data = data + np.linspace(0, 3, nvar)[None, :]
data = pd.DataFrame(data, columns=['V{0}'.format(i) for i in range(nvar)])
#----------------------------------------------------------------------
# Process
#----------------------------------------------------------------------
# Instanciate the violinplot object
vl = violinplot.Violin(data)
# Show violinplot statistics
print("median values:")
print(vl.stat_median)
# Plot it
plt.close('all')
fig, ax = plt.subplots(layout="tight")
vl.draw(ax)
fp = os.path.join(fimg, 'violinplot_default.png')
fig.savefig(fp)
# Same violinplot with different colors
vl = violinplot.Violin(data, crm="brown", cro="tab:orange")
fig, ax = plt.subplots(layout="tight")
vl.draw(ax)
fp = os.path.join(fimg, 'violinplot_colors.png')
fig.savefig(fp)
LOGGER.info('Process completed')