-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_charts.py
126 lines (102 loc) · 4.97 KB
/
export_charts.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Export Accelerator Timeline
***************************
This is an example script to generate static plots of the accelerator data via
matplotlib.
To run the script, make sure your environment has the requirements
of `requirements_export_charts.txt` installed.
"""
import os
from pathlib import Path
import matplotlib as mpl
import matplotlib.ticker as plticker
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib.figure import Figure
from utilities.csv_reader import Column, import_collider_data
from utilities.plot_helper import (PARTICLE_TYPES, PLOTLY_MPL_SYMBOL_MAP, EnergyConfiguration,
LuminosityConfiguration, LuminosityOverEnergyConfiguration,
PlotConfiguration, assign_textposition, check_all_types_accounted_for)
from utilities.sphinx_helper import get_gallery_dir, is_sphinx_build
def plot(data: pd.DataFrame, configuration: PlotConfiguration) -> Figure:
"""Generate interactive plots with matplotlib, based on the given configuration,
which defines the columns to use, labels and the text positions.
Args:
data (pd.DataFrame): DataFrame containing the (modified) accelerator timeline data
configuration (PlotConfiguration): See :class:`utilities.plot_helper.PlotConfiguration`
Returns:
Figure: Matplotlib figure
"""
fig, ax = plt.subplots()
pad = mpl.rcParams["lines.markersize"]/3
vmap = {"top": pad, "middle": 0, "bottom": -pad}
hmap = {"left": -pad*2, "center": 0, "right": pad*2}
alignment_map = {
"left": "right", "center": "center", "right": "left",
"top": "bottom", "middle": "center", "bottom": "top"
}
for particle_type in PARTICLE_TYPES:
mask = data[Column.TYPE] == particle_type.shorthand
marker = PLOTLY_MPL_SYMBOL_MAP[particle_type.symbol]
for has_been_built in (True, False):
if has_been_built:
builtmask, fillstyle, legend_prefix = data[Column.BUILT], "full", ""
else:
builtmask, fillstyle, legend_prefix = ~data[Column.BUILT], "none", "_"
ax.plot(
data.loc[mask & builtmask, configuration.xcolumn],
data.loc[mask & builtmask, configuration.ycolumn],
linestyle="none",
marker=marker, fillstyle=fillstyle,
color=particle_type.color,
label=f"{legend_prefix}{particle_type.latex}",
)
for x, y, text, textposition in zip(data.loc[mask, configuration.xcolumn],
data.loc[mask, configuration.ycolumn],
data.loc[mask, Column.NAME],
data.loc[mask, configuration.textposition]):
v, h = textposition.split(" ")
ax.annotate(text, xy=(x, y),
xytext=(hmap[h], vmap[v]),
textcoords="offset pixels",
ha=alignment_map[h], va=alignment_map[v]
)
ax.set_xlabel(configuration.xlabel)
ax.set_ylabel(configuration.ylabel)
for axis in ("x", "y"):
if axis in configuration.logscale:
getattr(ax, f"set_{axis}scale")("log")
lim = getattr(ax, f"get_{axis}lim")()
numticks = int(np.log10(lim[1]/lim[0])) + 1
getattr(ax, f"{axis}axis").set_major_locator(plticker.LogLocator(base=10.0, numticks=numticks))
getattr(ax, f"{axis}axis").set_minor_locator(plticker.LogLocator(base=10.0, subs=np.arange(2, 10)))
getattr(ax, f"{axis}axis").set_minor_formatter(plticker.NullFormatter())
else:
getattr(ax, f"set_{axis}scale")("linear")
getattr(ax, f"{axis}axis").set_major_locator(plticker.MultipleLocator(base=10.0))
getattr(ax, f"{axis}axis").set_minor_locator(plticker.MultipleLocator(base=1.0))
ax.legend(loc='upper left', bbox_to_anchor=(1, 1), borderaxespad=0., title='Particles', ncol=1)
return fig
if __name__ == "__main__":
if is_sphinx_build():
MAIN_DIR = Path()
output_dir = get_gallery_dir()
else:
MAIN_DIR = Path(__file__).parent
output_dir = MAIN_DIR / "images"
plt.style.use(MAIN_DIR / "utilities" / "chart.mplstyle")
data = import_collider_data()
data = assign_textposition(data)
check_all_types_accounted_for(data)
fig_com = plot(data, EnergyConfiguration)
fig_com.savefig(output_dir / "energy.pdf")
fig_com.savefig(output_dir / "energy.png")
fig_lumi = plot(data, LuminosityConfiguration)
fig_lumi.savefig(output_dir / "luminosity.pdf")
fig_lumi.savefig(output_dir / "luminosity.png")
fig_lumi_vs_com = plot(data, LuminosityOverEnergyConfiguration)
fig_lumi_vs_com.savefig(output_dir / "luminosity-vs-energy.pdf")
fig_lumi_vs_com.savefig(output_dir / "luminosity-vs-energy.png")
# plt.show()
# sphinx_gallery_thumbnail_path = 'gallery/luminosity-vs-energy.png'