Skip to content

Commit 2e987d9

Browse files
ColCarrolltwiecki
authored andcommitted
Remove matplotlib as a requirement (#2542)
* Remove matplotlib as a requirement * Install matplotlib in travis * Update some pylint * Fix test to work * Fix funny import error * Remove namespace changes * Just wrap matplotlib in try... except * Rebase, remove import test * Add bash script to confirm matplotlib is optional
1 parent db5fd81 commit 2e987d9

15 files changed

+66
-21
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ env:
3030
- PYTHON_VERSION=3.6 FLOATX='float64' TESTCMD="--durations=10 --cov-append pymc3/tests/test_variational_inference.py pymc3/tests/test_updates.py"
3131
script:
3232
- . ./scripts/test.sh $TESTCMD
33+
- . ./scripts/confirm_mpl_optional.sh
3334

3435
after_success:
3536
- coveralls

pymc3/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from .backends.tracetab import *
2323

2424
from .plots import *
25-
2625
from .tests import test
2726

2827
from .data import *

pymc3/plots/autocorrplot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import itertools
2-
import matplotlib.pyplot as plt
32
import numpy as np
43

4+
try:
5+
import matplotlib.pyplot as plt
6+
except ImportError: # mpl is optional
7+
pass
8+
59
from .utils import get_default_varnames, get_axis
610

711

pymc3/plots/compareplot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import matplotlib.pyplot as plt
21
import numpy as np
2+
try:
3+
import matplotlib.pyplot as plt
4+
except ImportError: # mpl is optional
5+
pass
36

47

58
def compareplot(comp_df, insample_dev=True, se=True, dse=True, ax=None,

pymc3/plots/energyplot.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import warnings
22

3-
import matplotlib.pyplot as plt
43
import numpy as np
4+
try:
5+
import matplotlib.pyplot as plt
6+
except ImportError: # mpl is optional
7+
pass
58
from .kdeplot import kdeplot
69

710

@@ -35,6 +38,9 @@ def energyplot(trace, kind='kde', figsize=None, ax=None, legend=True,
3538
ax : matplotlib axes
3639
"""
3740

41+
if ax is None:
42+
_, ax = plt.subplots(figsize=figsize)
43+
3844
try:
3945
energy = trace['energy']
4046
except KeyError:

pymc3/plots/forestplot.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from matplotlib import gridspec
2-
import matplotlib.pyplot as plt
1+
try:
2+
import matplotlib.pyplot as plt
3+
from matplotlib import gridspec
4+
except ImportError: # mpl is optional
5+
pass
36
import numpy as np
47
from pymc3.diagnostics import gelman_rubin
58
from pymc3.stats import quantiles, hpd
@@ -102,18 +105,18 @@ def _plot_tree(ax, y, ntiles, show_quartiles, plot_kwargs):
102105
if show_quartiles:
103106
# Plot median
104107
ax.plot(ntiles[2], y, color=plot_kwargs.get('color', 'blue'),
105-
marker=plot_kwargs.get('marker', 'o'),
106-
markersize=plot_kwargs.get('markersize', 4))
108+
marker=plot_kwargs.get('marker', 'o'),
109+
markersize=plot_kwargs.get('markersize', 4))
107110
# Plot quartile interval
108111
ax.errorbar(x=(ntiles[1], ntiles[3]), y=(y, y),
109-
linewidth=plot_kwargs.get('linewidth', 2),
110-
color=plot_kwargs.get('color', 'blue'))
112+
linewidth=plot_kwargs.get('linewidth', 2),
113+
color=plot_kwargs.get('color', 'blue'))
111114

112115
else:
113116
# Plot median
114117
ax.plot(ntiles[1], y, marker=plot_kwargs.get('marker', 'o'),
115-
color=plot_kwargs.get('color', 'blue'),
116-
markersize=plot_kwargs.get('markersize', 4))
118+
color=plot_kwargs.get('color', 'blue'),
119+
markersize=plot_kwargs.get('markersize', 4))
117120

118121
# Plot outer interval
119122
ax.errorbar(x=(ntiles[0], ntiles[-1]), y=(y, y),

pymc3/plots/kdeplot.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import matplotlib.pyplot as plt
21
import numpy as np
32
from scipy.signal import gaussian, convolve
43

4+
try:
5+
import matplotlib.pyplot as plt
6+
except ImportError: # mpl is optional
7+
pass
8+
59

610
def kdeplot(values, label=None, shade=0, ax=None, kwargs_shade=None, **kwargs):
711
"""

pymc3/plots/posteriorplot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from collections import OrderedDict
2-
import matplotlib.pyplot as plt
2+
3+
try:
4+
import matplotlib.pyplot as plt
5+
except ImportError: # mpl is optional
6+
pass
37
import numpy as np
48

59
from .artists import plot_posterior_op
@@ -88,7 +92,8 @@ def get_trace_dict(tr, varnames):
8892
fig, ax = plt.subplots(figsize=figsize)
8993
plot_posterior_op(transform(trace), ax=ax, kde_plot=kde_plot,
9094
point_estimate=point_estimate, round_to=round_to,
91-
alpha_level=alpha_level, ref_val=ref_val, rope=rope, text_size=text_size, **kwargs)
95+
alpha_level=alpha_level, ref_val=ref_val, rope=rope,
96+
text_size=text_size, **kwargs)
9297
else:
9398
if varnames is None:
9499
varnames = get_default_varnames(trace.varnames, plot_transformed)

pymc3/plots/traceplot.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import matplotlib.pyplot as plt
1+
try:
2+
import matplotlib.pyplot as plt
3+
except ImportError: # mpl is optional
4+
pass
25
import numpy as np
36

47
from .artists import histplot_op, kdeplot_op

pymc3/plots/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import matplotlib.pyplot as plt
1+
try:
2+
import matplotlib.pyplot as plt
3+
except ImportError: # mpl is optional
4+
pass
25
import numpy as np
36
# plotting utilities can all be in this namespace
47
from ..util import get_default_varnames # pylint: disable=unused-import

0 commit comments

Comments
 (0)