Skip to content

Commit 813e2ef

Browse files
Merge pull request PaNOSC-ViNYL#100 from PaNOSC-ViNYL/return_fig_ax
Allows make_plot and make_sub_plot to return fig and ax object
2 parents 06abd4e + 720a87a commit 813e2ef

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

mcstasscript/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.77"
1+
__version__ = "0.0.78"

mcstasscript/helper/plot_helper.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from matplotlib.ticker import MaxNLocator
88
from matplotlib.colors import BoundaryNorm
9+
import matplotlib.ticker as mticker
910

1011
from mcstasscript.data.data import McStasData
1112
from mcstasscript.data.data import McStasDataEvent
@@ -42,6 +43,18 @@ def _fmt(x, pos):
4243
return r'${}\cdot 10^{{{}}}$'.format(a, b)
4344

4445

46+
def _fmt(x, pos):
47+
a, b = f"{x:.2e}".split("e")
48+
b = int(b)
49+
50+
if abs(float(a) - 1) < 0.01:
51+
return rf"10^{{{b}}}"
52+
else:
53+
return rf"{a}\cdot 10^{{{b}}}"
54+
55+
formatter = mticker.FuncFormatter(lambda x, pos: rf"${_fmt(x, pos)}$")
56+
57+
4558
def _find_min_max_I(data):
4659
"""
4760
Returns minimum and maximum intensity to plot given dataset
@@ -209,8 +222,13 @@ def _plot_fig_ax(data, fig, ax, **kwargs):
209222
if "colorbar_axes" in kwargs:
210223
cax = kwargs["colorbar_axes"]
211224

212-
colorbar = fig.colorbar(im, ax=ax, cax=cax,
213-
format=matplotlib.ticker.FuncFormatter(_fmt))
225+
colorbar = fig.colorbar(im, ax=ax, cax=cax)
226+
227+
# If your colorbar is logarithmic:
228+
#colorbar.locator = mticker.LogLocator(base=10)
229+
#colorbar.formatter = mticker.LogFormatterMathtext(base=10)
230+
231+
#colorbar.update_ticks()
214232

215233
if data.metadata.zlabel is not None:
216234
colorbar.set_label(data.metadata.zlabel)

mcstasscript/interface/plotter.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ def make_plot(data_list, **kwargs):
3131
figsize = (13, 7)
3232

3333
for data in data_list:
34+
#fig, ax0 = plt.subplots(figsize=figsize, constrained_layout=True)
3435
fig, ax0 = plt.subplots(figsize=figsize, tight_layout=True)
3536
_plot_fig_ax(data, fig, ax0, **kwargs)
3637

38+
if "return_fig_ax" in kwargs and len(data_list) == 1:
39+
if kwargs["return_fig_ax"]:
40+
return fig, ax0
41+
3742
if "filename" in kwargs:
38-
fig.tight_layout()
43+
#fig.tight_layout()
3944
fig.savefig(kwargs["filename"])
45+
plt.close()
4046
else:
4147
plt.show()
4248

@@ -96,6 +102,9 @@ def make_sub_plot(data_list, **kwargs):
96102

97103
fig.tight_layout()
98104

105+
if "return_fig_ax" in kwargs and kwargs["return_fig_ax"]:
106+
return fig, ax
107+
99108
if "filename" in kwargs:
100109
fig.tight_layout()
101110
fig.savefig(kwargs["filename"])

0 commit comments

Comments
 (0)