Skip to content

Commit

Permalink
Merge pull request #96 from bobmyhill/fix_3.9
Browse files Browse the repository at this point in the history
[FIX] Modernise some matplotlib calls
  • Loading branch information
morganjwilliams committed Oct 16, 2023
2 parents dad53c6 + 138c8c3 commit 21bdc12
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
1 change: 1 addition & 0 deletions docs/source/dev/contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ comments, testing, bug reports, or feature requests.
* `Angela Rodrigues <https://github.com/AngRodrigues>`__
* `Sarah Shi <https://github.com/sarahshi>`__
* `Ondrej Lexa <https://github.com/ondrolexa>`__
* `Bob Myhill <https://github.com/bobmyhill>`__
13 changes: 9 additions & 4 deletions docs/source/gallery/examples/plotting/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""
import matplotlib.axes
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
import pandas as pd

Expand Down Expand Up @@ -41,16 +42,20 @@
cmap = "inferno"
compdata = df.copy()
compdata[comp] = CLRTransform().transform(compdata[comp])
ax = compdata.loc[:, comp].pyroplot.parallel(color_by=compdata.Depth.values, cmap=cmap)
ax = compdata.loc[:, comp].pyroplot.parallel(color=compdata.Depth.values, cmap=cmap)
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)

# we can add a meaningful colorbar to indicate one variable also, here Depth
sm = plt.cm.ScalarMappable(cmap=cmap)
sm.set_array(df.Depth)
plt.colorbar(sm)
plt.colorbar(sm, cax=cax, orientation='vertical')
plt.show()
########################################################################################
ax = compdata.loc[:, comp].pyroplot.parallel(
rescale=True, color_by=compdata.Depth.values, cmap=cmap
rescale=True, color=compdata.Depth.values, cmap=cmap
)
plt.colorbar(sm)
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(sm, cax=cax, orientation='vertical')
plt.show()
3 changes: 1 addition & 2 deletions pyrolite/geochem/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ def aggregate_element(

if logdata:
logger.debug("Log-transforming {} Data.".format(cation))
_df.loc[:, targetnames] = _df.loc[:, targetnames].applymap(np.log)

_df.loc[:, targetnames] = np.log(_df.loc[:, targetnames])
if drop:
logger.debug("Dropping redundant columns: {}".format(", ".join(drop)))
df = df.drop(columns=drop)
Expand Down
6 changes: 3 additions & 3 deletions pyrolite/plot/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def spider(
# if a color option is not specified, get the next cycled color
if l_kw.get("color") is None:
# add cycler color as array to suppress singular color warning
l_kw["color"] = np.array([next(ax._get_lines.prop_cycler)["color"]])
l_kw["color"] = ax._get_lines.get_next_color()

l_kw = linekwargs(process_color(**{**_line_defaults, **l_kw}))
# marker explictly dealt with by scatter
Expand Down Expand Up @@ -209,7 +209,7 @@ def spider(
)
# do these need to be ravelled?
ax.scatter(
indexes.ravel(), arr.ravel(), c=scattercolor, **{"zorder": 2, **s_kw}
indexes.ravel(), arr.ravel(), color=scattercolor, **{"zorder": 2, **s_kw}
)

# should create a custom legend handle here
Expand All @@ -218,7 +218,7 @@ def spider(
elif any([i in mode.lower() for i in ["binkde", "ckde", "kde", "hist"]]):
cmap = kwargs.pop("cmap", None)
if "contours" in kwargs and "vmin" in kwargs:
msg = "Combining `contours` and `vmin` arugments for density plots should be avoided."
msg = "Combining `contours` and `vmin` arguments for density plots should be avoided."
logger.warn(msg)
xe, ye, zi, xi, yi = conditional_prob_density(
arr,
Expand Down
4 changes: 2 additions & 2 deletions pyrolite/util/skl/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, **kwargs):

def transform(self, X, *args, **kwargs):
if isinstance(X, pd.DataFrame):
out = X.applymap(self.forward)
out = self.forward(X)
elif isinstance(X, pd.Series):
out = X.apply(self.forward)
else:
Expand All @@ -87,7 +87,7 @@ def transform(self, X, *args, **kwargs):

def inverse_transform(self, Y, *args, **kwargs):
if isinstance(Y, pd.DataFrame):
out = Y.applymap(self.inverse)
out = self.inverse(Y)
elif isinstance(Y, pd.Series):
out = Y.apply(self.inverse)
else:
Expand Down
4 changes: 2 additions & 2 deletions pyrolite/util/synthetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def example_spider_data(
df = ref.comp.pyrochem.compositional
if norm_to is not None:
df = df.pyrochem.normalize_to(norm_to, units=units)
start = df.applymap(np.log)
start = np.log(df)
nindex = df.columns.size

y = np.tile(start.values, size).reshape(size, nindex)
Expand All @@ -323,7 +323,7 @@ def example_spider_data(
if offsets is not None:
for element, offset in offsets.items():
syn_df[element] += offset # significant offset for e.g. Eu anomaly
syn_df = syn_df.applymap(np.exp)
syn_df = np.exp(syn_df)
return syn_df


Expand Down
19 changes: 12 additions & 7 deletions test/util/plot/util_plot_density.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_specified_contour_labels(self):
cs = plot_Z_percentiles(
self.xi, self.yi, zi=self.zi, contour_labels=contour_labels
)
for contour_label, label in zip(contour_labels, cs.labelTextsList):
for contour_label, label in zip(contour_labels, cs.labelTexts):
label = label.get_text()
self.assertTrue(contour_label == label)

Expand All @@ -80,12 +80,17 @@ def test_styling_specified(self):
linestyles=linestyles,
linewidths=linewidths,
)
for contour, color, ls, lw in zip(
cs.collections, colors, linestyles, linewidths
):
self.assertTrue((contour.get_edgecolor() == color).all())
self.assertEqual(contour.get_linestyle(), [_scale_dashes(*ls, lw)])
self.assertEqual(contour.get_linewidth(), lw)
try:
self.assertTrue(cs.colors == colors)
self.assertTrue(cs.linestyles == linestyles)
self.assertTrue((cs._us_lw == linewidths).all())
except AttributeError: # matplotlib version < 3.8
for contour, color, ls, lw in zip(
cs.collections, colors, linestyles, linewidths
):
self.assertTrue((contour.get_edgecolor() == color).all())
self.assertEqual(contour.get_linestyle(), [_scale_dashes(*ls, lw)])
self.assertEqual(contour.get_linewidth(), lw)

def test_linestyles_specified(self):
plot_Z_percentiles(
Expand Down
2 changes: 1 addition & 1 deletion test/util/util_pd.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_non_convertable(self):

class TestToNumeric(unittest.TestCase):
def setUp(self):
self.df = normal_frame().applymap(str)
self.df = normal_frame().astype(str)

def test_numeric(self):
df = self.df
Expand Down

0 comments on commit 21bdc12

Please sign in to comment.