Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add explicit registration of units in examples #23508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/units/annotate_with_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"""

import matplotlib.pyplot as plt
from basic_units import cm
from basic_units import register_units, cm


register_units()

fig, ax = plt.subplots()

Expand Down
5 changes: 4 additions & 1 deletion examples/units/artist_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import matplotlib.text as text
import matplotlib.collections as collections

from basic_units import cm, inch
from basic_units import register_units, cm, inch
import numpy as np
import matplotlib.pyplot as plt


register_units()

fig, ax = plt.subplots()
ax.xaxis.set_units(cm)
ax.yaxis.set_units(cm)
Expand Down
5 changes: 4 additions & 1 deletion examples/units/bar_demo2.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
This example requires :download:`basic_units.py <basic_units.py>`
"""
import numpy as np
from basic_units import cm, inch
from basic_units import register_units, cm, inch
import matplotlib.pyplot as plt


register_units()

cms = cm * np.arange(0, 10, 2)
bottom = 0 * cm
width = 0.8 * cm
Expand Down
4 changes: 3 additions & 1 deletion examples/units/bar_unit_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
"""

import numpy as np
from basic_units import cm, inch
from basic_units import register_units, cm, inch
import matplotlib.pyplot as plt


register_units()

N = 5
men_means = [150*cm, 160*cm, 146*cm, 172*cm, 155*cm]
men_std = [20*cm, 30*cm, 32*cm, 10*cm, 20*cm]
Expand Down
15 changes: 14 additions & 1 deletion examples/units/basic_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,17 @@ def cos(x):
return math.cos(x.convert_to(radians).get_value())


units.registry[BasicUnit] = units.registry[TaggedValue] = BasicUnitConverter()
def register_units():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still a little tan about who is incorrect here. It really seems sphinx gallery should spawn a new process for each example so the import happens again. However, I can see why that would be slow. It does make me extremely suspicious about changing behaviour via a registry that gets set at import time like this... (and yes we do it internally for dates and categorical as well).

"""
Explicitly register units provided here.

Normally, this will not be needed as units are registered on import.
However, if you reset the Matplotlib unit registry (as is done by
sphinx-gallery between running each example), then you will need to
explicitly re-register units from this module by calling this function.
"""
converter = BasicUnitConverter()
units.registry[BasicUnit] = units.registry[TaggedValue] = converter


register_units()
4 changes: 3 additions & 1 deletion examples/units/ellipse_with_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
This example requires :download:`basic_units.py <basic_units.py>`
"""

from basic_units import cm
from basic_units import register_units, cm
import numpy as np
from matplotlib import patches
import matplotlib.pyplot as plt


register_units()

xcenter, ycenter = 0.38*cm, 0.52*cm
width, height = 1e-1*cm, 3e-1*cm
angle = -30
Expand Down
5 changes: 4 additions & 1 deletion examples/units/radian_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import matplotlib.pyplot as plt
import numpy as np

from basic_units import radians, degrees, cos
from basic_units import register_units, radians, degrees, cos


register_units()

x = [val*radians for val in np.arange(0, 15, 0.01)]

Expand Down
5 changes: 4 additions & 1 deletion examples/units/units_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
This example requires :download:`basic_units.py <basic_units.py>`

"""
from basic_units import cm, inch
from basic_units import register_units, cm, inch
import matplotlib.pyplot as plt
import numpy as np


register_units()

cms = cm * np.arange(0, 10, 2)

fig, axs = plt.subplots(2, 2, constrained_layout=True)
Expand Down
5 changes: 4 additions & 1 deletion examples/units/units_scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"""
import numpy as np
import matplotlib.pyplot as plt
from basic_units import secs, hertz, minutes
from basic_units import register_units, secs, hertz, minutes


register_units()

# create masked array
data = (1, 2, 3, 4, 5, 6, 7, 8)
Expand Down