Skip to content

Commit

Permalink
Alternative option
Browse files Browse the repository at this point in the history
  • Loading branch information
mferrera committed Nov 22, 2023
1 parent 8976250 commit 98b0927
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 67 deletions.
10 changes: 2 additions & 8 deletions src/xtgeo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ def _xprint(msg):

from xtgeo._cxtgeo import XTGeoCLibError
from xtgeo.common import XTGeoDialog
from xtgeo.common.constants import (
UNDEF,
UNDEF_INT,
UNDEF_INT_LIMIT,
UNDEF_LIMIT,
XTG_BATCH,
)
from xtgeo.common.constants import UNDEF, UNDEF_INT, UNDEF_INT_LIMIT, UNDEF_LIMIT
from xtgeo.common.exceptions import (
BlockedWellsNotFoundError,
DateNotFoundError,
Expand Down Expand Up @@ -172,7 +166,7 @@ def _xprint(msg):
"Using non-interactive Agg backend for matplotlib"
)
_xprint("=" * 79)
os.environ[XTG_BATCH] = "1"
os.environ["MPLBACKEND"] = "Agg"


warnings.filterwarnings("default", category=DeprecationWarning, module="xtgeo")
Expand Down
29 changes: 0 additions & 29 deletions src/xtgeo/common/_import_mpl.py

This file was deleted.

2 changes: 0 additions & 2 deletions src/xtgeo/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@
# for XYZ data, restricted to float32 and int32
UNDEF_CONT = UNDEF
UNDEF_DISC = UNDEF_INT

XTG_BATCH = "XTG_BATCH"
27 changes: 17 additions & 10 deletions src/xtgeo/plot/baseplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from packaging.version import parse as versionparse

from xtgeo.common import XTGeoDialog, null_logger
from xtgeo.common._import_mpl import get_matplotlib, get_pyplot

from . import _colortables as _ctable

Expand All @@ -12,9 +11,11 @@

def _get_colormap(name):
"""For matplotlib compatibility."""
mpl = get_matplotlib()
import matplotlib as mpl

if versionparse(mpl.__version__) < versionparse("3.6"):
plt = get_pyplot()
import matplotlib.plt as plt

return plt.cm.get_cmap(name)
else:
return mpl.colormaps[name]
Expand Down Expand Up @@ -56,7 +57,8 @@ def colormap(self):

@colormap.setter
def colormap(self, cmap):
mpl = get_matplotlib()
import matplotlib as mpl

if isinstance(cmap, mpl.colors.LinearSegmentedColormap):
self._colormap = cmap
elif isinstance(cmap, str):
Expand Down Expand Up @@ -86,8 +88,9 @@ def define_any_colormap(cfile, colorlist=None):
from 0 index. Default is just keep the linear sequence as is.
"""
mpl = get_matplotlib()
plt = get_matplotlib()
import matplotlib as mpl
import matplotlib.pyplot as plt

valid_maps = sorted(m for m in plt.cm.datad)

logger.info("Valid color maps: %s", valid_maps)
Expand Down Expand Up @@ -205,7 +208,8 @@ def canvas(self, title=None, subtitle=None, infotext=None, figscaling=1.0):
"""
plt = get_pyplot()
import matplotlib.pyplot as plt

self._fig, self._ax = plt.subplots(
figsize=(11.69 * figscaling, 8.27 * figscaling)
)
Expand All @@ -227,8 +231,9 @@ def show(self):
self._fig.tight_layout()

if self._showok:
import matplotlib.pyplot as plt

logger.info("Calling plt show method...")
plt = get_pyplot()
plt.show()
return True

Expand All @@ -242,7 +247,8 @@ def close(self):
After close is called, no more operations can be performed on the plot.
"""
plt = get_pyplot()
import matplotlib.pyplot as plt

for fig in self._allfigs:
plt.close(fig)

Expand Down Expand Up @@ -272,7 +278,8 @@ def savefig(self, filename, fformat="png", last=True, **kwargs):
self._fig.tight_layout()

if self._showok:
plt = get_pyplot()
import matplotlib.pyplot as plt

plt.savefig(filename, format=fformat, **kwargs)
if last:
self.close()
Expand Down
7 changes: 4 additions & 3 deletions src/xtgeo/plot/grid3d_slice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module for 3D Grid slice plots, using matplotlib."""

from xtgeo.common import null_logger
from xtgeo.common._import_mpl import get_matplotlib, get_pyplot
from xtgeo.plot.baseplot import BasePlot

logger = null_logger(__name__)
Expand Down Expand Up @@ -122,7 +121,8 @@ def _plot_layer(self):

patches = []

mpl = get_matplotlib()
import matplotlib as mpl

for pos in range(len(ibn)):
nppol = xyc[pos, :, :]
if nppol.mean() > 0.0:
Expand Down Expand Up @@ -155,5 +155,6 @@ def _plot_layer(self):
self._ax.set_ylim((ymin, ymax))
self._fig.colorbar(im)

plt = get_pyplot()
import matplotlib.pyplot as plt

plt.gca().set_aspect("equal", adjustable="box")
18 changes: 11 additions & 7 deletions src/xtgeo/plot/xsection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from scipy.ndimage import gaussian_filter

from xtgeo.common import XTGeoDialog, null_logger
from xtgeo.common._import_mpl import get_matplotlib, get_pyplot
from xtgeo.well import Well
from xtgeo.xyz import Polygons

Expand Down Expand Up @@ -263,7 +262,7 @@ def canvas(self, title=None, subtitle=None, infotext=None, figscaling=1.0):
"""
# overriding the base class canvas
plt = get_pyplot()
import matplotlib.pyplot as plt

plt.rcParams["axes.xmargin"] = 0 # fill the plot margins

Expand Down Expand Up @@ -444,7 +443,8 @@ def set_xaxis_md(self, gridlines=False):
md_start_round = int(math.floor(md_start / 100.0)) * 100
md_start_delta = md_start - md_start_round

plt = get_pyplot()
import matplotlib.pyplot as plt

auto_ticks = plt.xticks()
auto_ticks_delta = auto_ticks[0][1] - auto_ticks[0][0]

Expand Down Expand Up @@ -566,7 +566,8 @@ def _plot_well_zlog(self, df, ax, bba, zonelogname, logwidth=4, legend=False):
df, idx_zshift, ctable, zonelogname, fillnavalue
)

mpl = get_matplotlib()
import matplotlib as mpl

lc = mpl.collections.LineCollection(
segments, colors=segments_colors, linewidth=logwidth, zorder=202
)
Expand Down Expand Up @@ -611,7 +612,8 @@ def _plot_well_faclog(self, df, ax, bba, facieslogname, logwidth=9, legend=True)
df, idx, ctable, facieslogname, fillnavalue
)

mpl = get_matplotlib()
import matplotlib as mpl

lc = mpl.collections.LineCollection(
segments, colors=segments_colors, linewidth=logwidth, zorder=201
)
Expand Down Expand Up @@ -658,7 +660,8 @@ def _plot_well_perflog(self, df, ax, bba, perflogname, logwidth=12, legend=True)
df, idx, ctable, perflogname, fillnavalue
)

mpl = get_matplotlib()
import matplotlib as mpl

lc = mpl.collections.LineCollection(
segments, colors=segments_colors, linewidth=logwidth, zorder=200
)
Expand Down Expand Up @@ -772,7 +775,8 @@ def _drawproxylegend(self, ax, bba, items, title=None):
proxies = []
labels = []

mpl = get_matplotlib()
import matplotlib as mpl

for item in items:
color = items[item]
proxies.append(mpl.lines.Line2D([0, 1], [0, 1], color=color, linewidth=5))
Expand Down
10 changes: 6 additions & 4 deletions src/xtgeo/plot/xtmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy.ma as ma

from xtgeo.common import null_logger
from xtgeo.common._import_mpl import get_matplotlib, get_pyplot

from .baseplot import BasePlot

Expand Down Expand Up @@ -118,7 +117,8 @@ def plot_surface(
levels = np.linspace(minvalue, maxvalue, self.contourlevels)
logger.debug("Number of contour levels: %s", levels)

plt = get_pyplot()
import matplotlib.pyplot as plt

plt.setp(self._ax.xaxis.get_majorticklabels(), rotation=xlabelrotation)

# zi = ma.masked_where(zimask, zi)
Expand All @@ -141,7 +141,8 @@ def plot_surface(

else:
logger.info("use LogLocator")
mpl = get_matplotlib()
import matplotlib as mpl

locator = mpl.ticker.LogLocator()
ticks = None
uselevels = None
Expand Down Expand Up @@ -175,8 +176,9 @@ def plot_faults(
.. _Matplotlib: http://matplotlib.org/api/colors_api.html
"""
import matplotlib as mpl

aff = fpoly.dataframe.groupby(idname)
mpl = get_matplotlib()

for name, _group in aff:
# make a dataframe sorted on faults (groupname)
Expand Down
4 changes: 2 additions & 2 deletions src/xtgeo/surface/_regsurf_oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import xtgeo
from xtgeo import XTGeoCLibError, _cxtgeo
from xtgeo.common import XTGeoDialog, null_logger
from xtgeo.common._import_mpl import get_matplotlib
from xtgeo.xyz import Polygons

xtg = XTGeoDialog()
Expand Down Expand Up @@ -565,7 +564,8 @@ def _proxy_map_polygons(surf, poly, inside=True):
xvals, yvals = proxy.get_xy_values(asmasked=False)
points = np.array([xvals.ravel(), yvals.ravel()]).T

mpl = get_matplotlib()
import matplotlib as mpl

for pol in usepolys:
idgroups = pol.dataframe.groupby(pol.pname)
for _, grp in idgroups:
Expand Down
4 changes: 2 additions & 2 deletions src/xtgeo/xyz/_xyz_oper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import xtgeo
from xtgeo import _cxtgeo
from xtgeo.common import XTGeoDialog, null_logger
from xtgeo.common._import_mpl import get_matplotlib

xtg = XTGeoDialog()
logger = null_logger(__name__)
Expand Down Expand Up @@ -40,7 +39,8 @@ def mark_in_polygons_mpl(self, poly, name, inside_value, outside_value):

self.dataframe[name] = outside_value

mpl = get_matplotlib()
import matplotlib as mpl

for pol in usepolys:
idgroups = pol.dataframe.groupby(pol.pname)
for _, grp in idgroups:
Expand Down

0 comments on commit 98b0927

Please sign in to comment.