forked from DanielMartinezMiravete/Axion_Limits_Memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXPlotter.py
198 lines (174 loc) · 7.5 KB
/
XPlotter.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# ================================XPlotter.py==================================#
# ================================XPlotter.py==================================#
# Igor G. Irastorza 2020
#
# Description:
# Translation to python of core plotter routines to be used to generate nice
# figures of sensitivity plots, etc`
# ==============================================================================#
# ==============================================================================#
import os
from numpy import *
from numpy.random import *
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.gridspec as gridspec
from matplotlib.colors import ListedColormap
from matplotlib import colors
import matplotlib.ticker as mticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.cm as cm
import pickle
plotpdfdir = "./plots/"
plotpngdir = plotpdfdir + "pngs/"
# ==============================================================================#
# == class representing a generic sensitivity plot
# ==============================================================================#
# ==============================================================================#
class BasePlot:
"""Base class to host a generic sensitivity plot"""
# ==============================================================================#
# the class is initialized with info on the limits, axis, etc
def __init__(
self,
xlab="x axis",
ylab="y axis",
figsizex=6.5,
figsizey=5,
y_min=1.0e-18,
y_max=1.0e-4,
x_min=1.0e-11,
x_max=1.0e9,
ticksopt_x="normal",
ticksopt_y="normal",
labelfontsize=14,
):
plt.rc("text", usetex=True)
plt.rc("font", family="times", size=labelfontsize)
self.fig = plt.figure(figsize=(figsizex, figsizey))
self.plot = self.fig.add_subplot(111)
# axis and labels
self.plot.set_xlabel(
xlab, fontsize=labelfontsize, horizontalalignment="right", x=1
)
self.plot.set_ylabel(
ylab, fontsize=labelfontsize, horizontalalignment="right", y=1
)
self.plot.tick_params(
which="major", direction="in", right=True, top=True, width=0.8, length=5
)
self.plot.tick_params(
which="minor", direction="in", right=True, top=True, width=0.4, length=3
)
# ,right=TrueopAndRightTicks,top=TopAndRightTicks,pad=7)
# self.plot.tick_params(which='minor',direction='in',width=1,length=10)
# ,right=TopAndRightTicks,top=TopAndRightTicks)
self.plot.set_yscale("log")
self.plot.set_xscale("log")
self.plot.set_xlim([x_min, x_max])
self.plot.set_ylim([y_min, y_max])
locmaj = mpl.ticker.LogLocator(base=10.0, subs=(1.0,), numticks=100)
locmin = mpl.ticker.LogLocator(
base=10.0, subs=arange(2, 10) * 0.1, numticks=100
)
if ticksopt_x == "dense":
locmaj = mpl.ticker.LogLocator(base=100.0, subs=(1.0,), numticks=100)
locmin = mpl.ticker.LogLocator(base=10.0, subs=(1.0,), numticks=100)
self.plot.xaxis.set_major_locator(locmaj)
self.plot.xaxis.set_minor_locator(locmin)
self.plot.xaxis.set_minor_formatter(mpl.ticker.NullFormatter())
locmaj = mpl.ticker.LogLocator(base=10.0, subs=(1.0,), numticks=100)
locmin = mpl.ticker.LogLocator(
base=10.0, subs=arange(2, 10) * 0.1, numticks=100
)
if ticksopt_x == "dense":
locmin = mpl.ticker.LogLocator(base=10.0, subs=(1.0,), numticks=100)
self.plot.yaxis.set_major_locator(locmaj)
self.plot.yaxis.set_minor_locator(locmin)
self.plot.yaxis.set_minor_formatter(mpl.ticker.NullFormatter())
self.zorder = -100
# ==============================================================================#
# will draw a new exclusion line to the plot, no to be filled
#
def AddPlotItem(self, typeitem, linename, data, **kwargs):
y2 = self.plot.get_ylim()[1]
kwargs["zorder"] = self.zorder
if typeitem not in ["band", "line", "region"]:
print("ERROR: item type" + typeitem + "not known")
exit()
if typeitem == "band":
self.plot.fill_between(data[:, 0], data[:, 1], y2=y2, **kwargs)
if typeitem == "region":
plt.fill(data[:, 0], data[:, 1], **kwargs)
if typeitem == "line":
self.plot.plot(data[:, 0], data[:, 1], **kwargs)
self.zorder += 1
def onclick(self, event):
print(
"%s click: button=%d, x=%d, y=%d, xdata=%g, ydata=%gf"
% (
"double" if event.dblclick else "single",
event.button,
event.x,
event.y,
event.xdata,
event.ydata,
)
)
# ==============================================================================#
# switch to interactive mode and shows the plot on screen
#
def ShowPlot(self):
cid = self.fig.canvas.mpl_connect("button_press_event", self.onclick)
plt.ioff()
plt.show()
self.fig.canvas.mpl_disconnect(cid)
# ==============================================================================#
# saves the plot on a file
#
def SavePlot(
self, plotname, pngsave=False, svgsave=False, openpdf=False, picklesave=False
):
filename = "plots/" + plotname
if not (plotname.endswith(".pdf")):
filename = filename + ".pdf"
self.fig.savefig(filename, bbox_inches="tight")
print(filename + " saved.")
if pngsave:
self.fig.savefig(plotname + ".png", bbox_inches="tight")
print(filename.replace(".pdf", ".png") + ".png saved.")
if svgsave:
self.fig.savefig(plotpngdir + plotname + ".svg", bbox_inches="tight")
print(filename + ".svg saved.")
if openpdf:
os.startfile(filename)
print(filename)
if picklesave:
fil = open(plotpdfdir + plotname + ".pickle", "wb")
pickle.dump(self.fig, fil)
print(plotpdfdir + plotname + ".pickle saved.")
# ==============================================================================#
# class representing an physics item (i.e. excluded region)
# to be drawn on plot
# useful to create the object but decide later if to be plotted or not
# ==============================================================================#
# ==============================================================================#
class ExPltItem:
# ==============================================================================#
# the object just contains the same info that I would pass when plotting...
#
def __init__(self, name, typeitem, filename, **kwargs):
self.name = name
self.typeitem = typeitem
self.filename = filename
self.drawopt = kwargs
if typeitem not in ["band", "region", "line"]:
print("ERROR: unknown plot item " + typeitem)
self.data = loadtxt(filename)
def DrawItem(self, plot):
print(self.name, self.filename, self.drawopt)
plot.AddPlotItem(self.typeitem, self.name, self.data, **self.drawopt)
# ==============================================================================#
# ==============================================================================#
# ==============================================================================#