robintw / Py6S

A Python interface to the 6S Radiative Transfer Model
GNU Lesser General Public License v3.0
186 stars 105 forks source link

"NameError: name 'subplots' is not defined"bug in 'all_angles.py' #103

Open Flemyng1999 opened 1 year ago

Flemyng1999 commented 1 year ago

When I run code-1 (copied from https://py6s.readthedocs.io/en/latest/helpers.html#running-for-many-angles):

from Py6S import *

s = SixS()
s.ground_reflectance = GroundReflectance.HomogeneousRoujean(0.037, 0.0, 0.133)
s.geometry.solar_z = 30
s.geometry.solar_a = 0
SixSHelpers.Angles.run_and_plot_360(s, 'view', 'pixel_reflectance')

Output this:

  File "C:\Users\xxx\miniconda3\envs\py6s-env\lib\site-packages\Py6S\SixSHelpers\all_angles.py", line 234, in plot_polar_contour
    fig, ax = subplots(subplot_kw=dict(projection="polar"), figsize=figsize)
NameError: name 'subplots' is not defined

So I make a change in 'all_angles.py':

    @classmethod
    def plot_polar_contour(
        cls, values, azimuths, zeniths, filled=True, colorbarlabel="", figsize=None
    ):
        # I added
        try:
            from matplotlib.pyplot import subplots
        except ImportError:
            raise ImportError("You must install matplotlib to use the plotting functionality")

        theta = np.radians(azimuths)
        zeniths = np.array(zeniths)

        values = np.array(values)
        values = values.reshape(len(azimuths), len(zeniths))

        r, theta = np.meshgrid(zeniths, np.radians(azimuths))
        fig, ax = subplots(subplot_kw=dict(projection="polar"), figsize=figsize)
        ax.set_theta_zero_location("N")
        ax.set_theta_direction(-1)
        if filled:
            cax = ax.contourf(theta, r, values, 30)
        else:
            cax = ax.contour(theta, r, values, 30)
        cb = fig.colorbar(cax)
        cb.set_label(colorbarlabel)

        return fig, ax, cax

The code-1 can run

machenme commented 3 months ago

does not work for jupyter . but original file can work with py file.