Skip to content

Commit b25d3e2

Browse files
authored
Merge pull request #14 from AngelFP/feature/lazy_matplotlib
Lazy import matplotlib
2 parents e38b939 + 3dfff3b commit b25d3e2

4 files changed

Lines changed: 24 additions & 14 deletions

File tree

aptools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .data_analysis import beam_diagnostics
22

3-
__version__ = "0.2.4"
3+
__version__ = "0.2.5"
44
__all__ = ['beam_diagnostics', '__version__']

aptools/plotting/plot_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" Contains methods for making different types of plots """
22

3-
import matplotlib.pyplot as plt
43
import numpy as np
54

65

@@ -9,6 +8,7 @@ def scatter_histogram(x, y, bins=[300, 300], range=None, weights=None,
98
"""
109
Does a scatter plot from the histogram of a particle distribution.
1110
"""
11+
import matplotlib.pyplot as plt
1212
counts, xedges, yedges = np.histogram2d(x, y, bins=bins, range=range,
1313
weights=weights)
1414
x_grid = xedges[1:] - np.abs(xedges[1]-xedges[0])/2

aptools/plotting/quick_diagnostics.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
diagnostics"""
33

44
import numpy as np
5-
import matplotlib
6-
import matplotlib.pyplot as plt
7-
import matplotlib.gridspec as gridspec
8-
import matplotlib.patheffects as path_effects
95
import scipy.constants as ct
106

117
import aptools.data_analysis.beam_diagnostics as bd
@@ -28,6 +24,8 @@ def phase_space_overview_from_file(
2824

2925
def phase_space_overview(x, y, z, px, py, pz, q, rasterized_scatter=None,
3026
show=True):
27+
import matplotlib.pyplot as plt
28+
import matplotlib.patheffects as path_effects
3129
em_x = bd.normalized_transverse_rms_emittance(x, px, w=q) * 1e6
3230
em_y = bd.normalized_transverse_rms_emittance(y, py, w=q) * 1e6
3331
a_x, b_x, g_x = bd.twiss_parameters(x, px, pz, w=q)
@@ -127,6 +125,9 @@ def slice_analysis(x, y, z, px, py, pz, q, n_slices=50, len_slice=None,
127125
ene_bins=50, left=0.125, right=0.875, top=0.98, bottom=0.13,
128126
xlim=None, ylim=None, add_labels=False, include_twiss=False,
129127
fig=None, rasterized_scatter=None, show=True):
128+
import matplotlib.pyplot as plt
129+
import matplotlib.gridspec as gridspec
130+
from matplotlib.colorbar import Colorbar
130131
# analyze beam
131132
current_prof, z_edges = bd.current_profile(z, q, n_slices=n_slices,
132133
len_slice=len_slice)
@@ -283,7 +284,7 @@ def slice_analysis(x, y, z, px, py, pz, q, n_slices=50, len_slice=None,
283284

284285
# colorbar
285286
ax = plt.subplot(gs[1])
286-
matplotlib.colorbar.Colorbar(ax, pscatt, label='Q [fC]')
287+
Colorbar(ax, pscatt, label='Q [fC]')
287288

288289
# slice parameters plot
289290
plt.subplot(gs[2])
@@ -360,6 +361,9 @@ def energy_vs_z(
360361
xlim=None, ylim=None, show_text=True, x_proj=True, y_proj=True,
361362
cbar=True, cbar_width=0.02, left=0.125, right=0.875, top=0.98,
362363
bottom=0.13, fig=None, rasterized_scatter=None, show=True):
364+
import matplotlib.pyplot as plt
365+
import matplotlib.gridspec as gridspec
366+
from matplotlib.colorbar import Colorbar
363367
# analyze beam
364368
current_prof, z_edges = bd.current_profile(z, q, n_slices=n_slices,
365369
len_slice=len_slice)
@@ -488,13 +492,16 @@ def energy_vs_z(
488492
# colorbar
489493
if cbar:
490494
ax = plt.subplot(gs[1])
491-
matplotlib.colorbar.Colorbar(ax, pscatt, label='Q [fC]')
495+
Colorbar(ax, pscatt, label='Q [fC]')
492496

493497
if show:
494498
plt.show()
495499

496500

497501
def full_phase_space(x, y, z, px, py, pz, q, show=True, **kwargs):
502+
import matplotlib.pyplot as plt
503+
import matplotlib.gridspec as gridspec
504+
498505
fig = plt.figure(figsize=(12, 3))
499506
grid = gridspec.GridSpec(1, 3, figure=fig, wspace=0.55)
500507
hor_phase_space(
@@ -654,6 +661,8 @@ def phase_space_plot(
654661
s=1, cmap='plasma', center_lines=False,
655662
text=None, cbar=True, cbar_ticks=3, cbar_width=0.05,
656663
subplot_spec=None, fig=None, tight_layout=False, show=True):
664+
import matplotlib.pyplot as plt
665+
import matplotlib.gridspec as gridspec
657666

658667
if cbar:
659668
n_cols = 2

aptools/plotting/utils.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import numpy as np
2-
import matplotlib.pyplot as plt
3-
import matplotlib.gridspec as gs
4-
from matplotlib import ticker
5-
from matplotlib.colorbar import Colorbar
6-
import matplotlib.patheffects as path_effects
7-
from matplotlib import colors
82

93

104
def add_projection(
115
x, bins, main_ax, subplot_spec, fig, orientation='horizontal'):
6+
import matplotlib.gridspec as gs
127
x_proj, x_bins = np.histogram(x, bins=bins)
138
x_pos = x_bins[1:] - (x_bins[1]-x_bins[0])
149

@@ -44,6 +39,10 @@ def add_projection(
4439

4540
def create_vertical_colorbars(
4641
images, labels, subplot_spec, fig=None, n_ticks=3, **kwargs):
42+
import matplotlib.pyplot as plt
43+
import matplotlib.gridspec as gs
44+
from matplotlib import ticker
45+
from matplotlib.colorbar import Colorbar
4746
if not isinstance(images, list):
4847
images = [images]
4948
if not isinstance(labels, list):
@@ -60,6 +59,8 @@ def create_vertical_colorbars(
6059

6160

6261
def add_text(ax, x, y, text, **kwargs):
62+
import matplotlib.patheffects as path_effects
63+
from matplotlib import colors
6364
fc = colors.to_rgba('white')
6465
# fc[:-1] + (0.7,)
6566
ec = colors.to_rgba('tab:gray')

0 commit comments

Comments
 (0)