8000 Fix units_scatter.py example, which was broken before this PR. · matplotlib/matplotlib@a23a30b · GitHub
[go: up one dir, main page]

Skip to content

Commit a23a30b

Browse files
efiringanntzer
authored andcommitted
Fix units_scatter.py example, which was broken before this PR.
Prior to this, examples/units/basic_units.py was not actually handling masked arrays, and units_scatter.py was not plotting its third panel correctly.
1 parent f979b1f commit a23a30b

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

examples/units/basic_units.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ def get_compressed_copy(self, mask):
174174
def convert_to(self, unit):
175175
if unit == self.unit or not unit:
176176
return self
177-
new_value = self.unit.convert_value_to(self.value, unit)
177+
try:
178+
new_value = self.unit.convert_value_to(self.value, unit)
179+
except AttributeError:
180+
new_value = self
178181
return TaggedValue(new_value, unit)
179182

180183
def get_value(self):
@@ -345,7 +348,20 @@ def convert(val, unit, axis):
345348
if units.ConversionInterface.is_numlike(val):
346349
return val
347350
if np.iterable(val):
348-
return [thisval.convert_to(unit).get_value() for thisval in val]
351+
if np.ma.isMaskedArray(val):
352+
val = val.astype(float).filled(np.nan)
353+
out = np.empty((len(val),), dtype=float)
354+
for i, thisval in enumerate(val):
355+
if np.ma.is_masked(thisval):
356+
out[i] = np.nan
357+
else:
358+
try:
359+
out[i] = thisval.convert_to(unit).get_value()
360+
except AttributeError:
361+
out[i] = thisval
362+
return out
363+
if np.ma.is_masked(val):
364+
return np.nan
349365
else:
350366
return val.convert_to(unit).get_value()
351367

examples/u 88DA nits/units_scatter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@
2727
ax2.scatter(xsecs, xsecs, yunits=hertz)
2828
ax2.axis([0, 10, 0, 1])
2929

30-
ax3.scatter(xsecs, xsecs, yunits=hertz)
31-
ax3.yaxis.set_units(minutes)
32-
ax3.axis([0, 10, 0, 1])
30+
ax3.scatter(xsecs, xsecs, yunits=minutes)
31+
ax3.axis([0, 10, 0, 0.2])
3332

3433
fig.tight_layout()
3534
plt.show()

0 commit comments

Comments
 (0)
0