From 1ab28136c64d817cc377210933fb54733105669d Mon Sep 17 00:00:00 2001 From: Alho Markku J Date: Wed, 8 Jan 2025 14:39:36 +0200 Subject: [PATCH] pylab imports replaced with matplotlib.pyplot, as strongly recommended --- examples/plot_VSC.py | 3 +-- miscellaneous/rankine.py | 4 ++-- pyCalculations/find_x_and_o.py | 5 ++--- pyCalculations/gyrophaseangle.py | 12 ++++++------ pyCalculations/themis_observation.py | 14 +++++++------- pyMayaVi/mayavigrid.py | 12 ++++++------ pyPlots/plot_variables.py | 6 +++--- pyVlsv/reduction.py | 4 ++-- 8 files changed, 29 insertions(+), 31 deletions(-) diff --git a/examples/plot_VSC.py b/examples/plot_VSC.py index 64681437..176cdee1 100644 --- a/examples/plot_VSC.py +++ b/examples/plot_VSC.py @@ -3,7 +3,6 @@ import matplotlib.colors as colors import matplotlib.cm as cmx import matplotlib.pyplot as plt -import pylab as pl import numpy as np import sys import pytools as pt @@ -63,7 +62,7 @@ profiles_T.append(f.read_variable("Temperature", cellids=cellid)*1e-6) #to MK # Init figure -fig = pl.figure() +fig = plt.figure() fig.set_size_inches(6,9) fig.add_subplot(4,1,1) diff --git a/miscellaneous/rankine.py b/miscellaneous/rankine.py index e1927723..4d6706f6 100644 --- a/miscellaneous/rankine.py +++ b/miscellaneous/rankine.py @@ -23,7 +23,7 @@ # Rankine-Hugoniot conditions to determine the right state from the left state of an MHD oblique shock import numpy as np -import pylab as pl +import matplotlib.pyplot as plt import sys from scipy import optimize from cutthrough import cut_through @@ -222,7 +222,7 @@ def plot_rankine( vlsvReader, point1, point2 ): numberOfVariables = len(variables) fig = plot_variables( distances, variables, figure=[] ) - pl.show() + plt.show() return fig diff --git a/pyCalculations/find_x_and_o.py b/pyCalculations/find_x_and_o.py index ca5a9866..cb7e3637 100755 --- a/pyCalculations/find_x_and_o.py +++ b/pyCalculations/find_x_and_o.py @@ -2,7 +2,6 @@ # import pytools as pt import numpy as np -import pylab as pl import matplotlib.pyplot as plt import sys from scipy.ndimage import convolve @@ -102,7 +101,7 @@ def findIntersection(v1,v2): dfdx,dfdz=np.gradient(flux_function) #calculate the 0 contours of df/dx and df/dz - pl.figure(1) + plt.figure(1) contour1=plt.contour(x_array,z_array, dfdx, [0]) contour1_paths=contour1.collections[0].get_paths() contour2=plt.contour(x_array,z_array, dfdz, [0]) @@ -180,7 +179,7 @@ def findIntersection(v1,v2): o_point_location.append(coords) o_point_fluxes.append(interpolated_flux) - pl.close('all') + plt.close('all') np.savetxt(path_to_save+"/o_point_location_"+str(index)+".txt", o_point_location) np.savetxt(path_to_save+"/o_point_location_and_fluxes_"+str(index)+".txt", np.concatenate( (o_point_location,np.array(o_point_fluxes)[:,np.newaxis]), axis=1)) diff --git a/pyCalculations/gyrophaseangle.py b/pyCalculations/gyrophaseangle.py index 698bd3c5..5dde8a75 100644 --- a/pyCalculations/gyrophaseangle.py +++ b/pyCalculations/gyrophaseangle.py @@ -22,7 +22,7 @@ # import numpy as np -import pylab as pl +import matplotlib.pyplot as plt from rotation import rotateVectorToVector import logging @@ -39,8 +39,8 @@ def gyrophase_angles_from_file( vlsvReader, cellid): vlsvReader = VlsvReader("fullf.0001.vlsv") result = gyrophase_angles_from_file( vlsvReader=vlsvReader, cellid=1924) # Plot the data - import pylab as pl - pl.hist(result[0].data, weights=result[1].data, bins=100, log=False) + import matplotlib.pyplot as plt + plt.hist(result[0].data, weights=result[1].data, bins=100, log=False) ''' # Read the velocity cells: velocity_cell_data = vlsvReader.read_velocity_cells(cellid) @@ -95,8 +95,8 @@ def gyrophase_angles(bulk_velocity, B_unit, velocity_cell_data, velocity_coordin vlsvReader = VlsvReader("fullf.0001.vlsv") result = gyrophase_angles_from_file( vlsvReader=vlsvReader, cellid=1924, cosine=True, plasmaframe=False ) # Plot the data - import pylab as pl - pl.hist(result[0].data, weights=result[1].data, bins=100, log=False) + import matplotlib.pyplot as plt + plt.hist(result[0].data, weights=result[1].data, bins=100, log=False) ''' # Get avgs data: @@ -118,5 +118,5 @@ def gyrophase_angles(bulk_velocity, B_unit, velocity_cell_data, velocity_coordin # Return the gyrophase angles and avgs values: from output import output_1d return output_1d([gyro_angles, avgs], ["Gyrophase_angle", "avgs"], [units, ""]) - #pl.hist(gyro_angles, weights=avgs, bins=bins, log=log) + #plt.hist(gyro_angles, weights=avgs, bins=bins, log=log) diff --git a/pyCalculations/themis_observation.py b/pyCalculations/themis_observation.py index 9b41f454..cdc6a196 100644 --- a/pyCalculations/themis_observation.py +++ b/pyCalculations/themis_observation.py @@ -22,7 +22,7 @@ # import numpy as np -import pylab as pl +import matplotlib.pyplot as plt import matplotlib from rotation import rotateVectorToVector from scipy.interpolate import griddata @@ -126,13 +126,13 @@ def themis_plot_detector(vlsvReader, cellID, detector_axis=np.array([0,1,0]), po values = abs(values); grid_r, grid_theta = np.meshgrid(energies,angles) - fig,ax=pl.subplots(subplot_kw=dict(projection="polar"),figsize=(12,10)) + fig,ax=plt.subplots(subplot_kw=dict(projection="polar"),figsize=(12,10)) ax.set_title("Detector view at cell " + str(cellID)) logging.info("Plotting...") cax = ax.pcolormesh(grid_theta,grid_r,values, norm=matplotlib.colors.LogNorm(vmin=vmin,vmax=vmax), cmap=themis_colormap) ax.grid(True) fig.colorbar(cax) - pl.show() + plt.show() def themis_plot_phasespace_contour(vlsvReader, cellID, plane_x=np.array([1.,0,0]), plane_y=np.array([0,0,1.]), smooth=False, xlabel="Vx", ylabel="Vy", pop="proton"): ''' Plots a contour view of phasespace, as seen by a themis detector, at the given cellID @@ -167,7 +167,7 @@ def themis_plot_phasespace_contour(vlsvReader, cellID, plane_x=np.array([1.,0,0] blurkernel = np.exp(-.17*np.power([6,5,4,3,2,1,0,1,2,3,4,5,6],2)) vi = sepfir2d(vi, blurkernel, blurkernel) / 4.2983098411528502 - fig,ax=pl.subplots(figsize=(12,10)) + fig,ax=plt.subplots(figsize=(12,10)) ax.set_aspect('equal') ax.set_title("Phasespace at cell " + str(cellID)) ax.set_xlabel(xlabel+" (km/s)") @@ -175,7 +175,7 @@ def themis_plot_phasespace_contour(vlsvReader, cellID, plane_x=np.array([1.,0,0] cax = ax.contour(xi,yi,vi.T, levels=np.logspace(np.log10(vmin),np.log10(vmax),20), norm=matplotlib.colors.LogNorm(vmin=vmin,vmax=vmax)) ax.grid(True) fig.colorbar(cax) - pl.show() + plt.show() def themis_plot_phasespace_helistyle(vlsvReader, cellID, plane_x=np.array([1.,0,0]), plane_y=np.array([0,0,1.]), smooth=True, xlabel="Vx", ylabel="Vy"): ''' Plots a view of phasespace, as seen by a themis detector, at the given cellID, in the style that heli likes. @@ -210,7 +210,7 @@ def themis_plot_phasespace_helistyle(vlsvReader, cellID, plane_x=np.array([1.,0, blurkernel = np.exp(-.17*np.power([6,5,4,3,2,1,0,1,2,3,4,5,6],2)) vi = sepfir2d(vi, blurkernel, blurkernel) / 4.2983098411528502 - fig,ax=pl.subplots(figsize=(12,10)) + fig,ax=plt.subplots(figsize=(12,10)) ax.set_aspect('equal') ax.set_title("Phasespace at cell " + str(cellID)) ax.set_xlabel(xlabel+" (km/s)") @@ -224,7 +224,7 @@ def themis_plot_phasespace_helistyle(vlsvReader, cellID, plane_x=np.array([1.,0, #cax3 = ax.contour(xi,yi,vi.T, levels=np.logspace(np.log10(vmin),np.log10(vmax),20), norm=matplotlib.colors.LogNorm(vmin=vmin,vmax=vmax), cmap=pl.get_cmap("binary")) ax.grid(True) fig.colorbar(cax) - pl.show() + plt.show() def themis_observation_from_file( vlsvReader, cellid, matrix=np.array([[1,0,0],[0,1,0],[0,0,1]]), countrates=True, interpolate=True,binOffset=[0.,0.],pop='proton'): ''' Calculates artificial THEMIS EMS observation from the given cell :param vlsvReader: Some VlsvReader class with a file open diff --git a/pyMayaVi/mayavigrid.py b/pyMayaVi/mayavigrid.py index da186fd0..41634414 100644 --- a/pyMayaVi/mayavigrid.py +++ b/pyMayaVi/mayavigrid.py @@ -32,7 +32,7 @@ from mayavi.core.ui.mayavi_scene import MayaviScene import vlsvfile from numpy import mgrid, empty, sin, pi, ravel -import pylab as pl +import matplotlib.pyplot as plt from tvtk.api import tvtk import traits.api import mayavi.api @@ -373,8 +373,8 @@ def __picker_callback( self, picker ): from pitchangle import pitch_angles result = pitch_angles( vlsvReader=self.vlsvReader, cellid=cellid, cosine=True, plasmaframe=True ) # plot: - pl.hist(result[0].data, weights=result[1].data, bins=50, log=False) - pl.show() + plt.hist(result[0].data, weights=result[1].data, bins=50, log=False) + plt.show() elif (self.picker == "Gyrophase_angle"): # Find the nearest cell id with distribution: # Read cell ids with velocity distribution in: @@ -400,8 +400,8 @@ def __picker_callback( self, picker ): from gyrophaseangle import gyrophase_angles_from_file result = gyrophase_angles_from_file( vlsvReader=self.vlsvReader, cellid=cellid) # plot: - pl.hist(result[0].data, weights=result[1].data, bins=36, range=[-180.0,180.0], log=True, normed=1) - pl.show() + plt.hist(result[0].data, weights=result[1].data, bins=36, range=[-180.0,180.0], log=True, normed=1) + plt.show() elif (self.picker == "Cut_through"): # Get the cut-through points point1 = self.__last_pick @@ -446,7 +446,7 @@ def __picker_callback( self, picker ): self.draw_streamline( point1, point2 ) from plot import plot_multiple_variables fig = plot_multiple_variables( distances, variables, figure=[] ) - pl.show() + plt.show() # Close the optimized file read: self.vlsvReader.optimize_close_file() # Read in the necessary variables: diff --git a/pyPlots/plot_variables.py b/pyPlots/plot_variables.py index 7f8421de..373d7ad6 100644 --- a/pyPlots/plot_variables.py +++ b/pyPlots/plot_variables.py @@ -29,7 +29,7 @@ ############################### -import pylab as pl +import matplotlib.pyplot as plt import logging import numpy as np from matplotlib.ticker import MaxNLocator @@ -135,12 +135,12 @@ def plot_multiple_variables( variables_x_list, variables_y_list, figure=[], clea length_of_list = len(variables_x_list) if figure != []: - fig = pl.figure + fig = plt.figure() if len(fig.get_axes()) < length_of_list: for i in (np.arange(length_of_list-len(fig.get_axes())) + len(fig.get_axes())): fig.add_subplot(length_of_list,1,i) else: - fig = pl.figure() + fig = plt.figure() for i in range(length_of_list): fig.add_subplot(length_of_list,1,i+1) diff --git a/pyVlsv/reduction.py b/pyVlsv/reduction.py index a02a82f8..51605fb6 100644 --- a/pyVlsv/reduction.py +++ b/pyVlsv/reduction.py @@ -25,7 +25,7 @@ ''' import logging import numpy as np -import pylab as pl +import matplotlib.pyplot as plt from reducer import DataReducerVariable from rotation import rotateTensorToVector, rotateArrayTensorToVector from gyrophaseangle import gyrophase_angles @@ -698,7 +698,7 @@ def gyrophase_relstddev( variables, velocity_cell_data, velocity_coordinates ): B_unit = B / np.linalg.norm(B) gyrophase_data = gyrophase_angles(bulk_velocity, B_unit, velocity_cell_data, velocity_coordinates) - histo = pl.hist(gyrophase_data[0].data, weights=gyrophase_data[1].data, bins=36, range=[-180.0,180.0], log=False, normed=1) + histo = plt.hist(gyrophase_data[0].data, weights=gyrophase_data[1].data, bins=36, range=[-180.0,180.0], log=False, normed=1) return np.std(histo[0])/np.mean(histo[0]) def Dng( variables ):