code to read in the data - will depend on the format of the file import numpy as np import matplotlib.pyplot as plt %matplotlib inline filename = '/home/mebaldwi/DVN.txt' # first 3 lines are header information and should be skipped data = np.genfromtxt(filename,delimiter=',',skip_header=3,max_rows=75,missing_values='-9999.00',usemask=True) height_MSL_m=data[:,1] press_mb=data[:,0] temp_C=data[:,2] td_C=data[:,3] wind_spd_kt=data[:,5] wind_dir_deg=data[:,4] code to set up the skewed x-axis, this only needs to be executed once from matplotlib.axes import Axes import matplotlib.transforms as transforms import matplotlib.axis as maxis import matplotlib.spines as mspines import matplotlib.path as mpath from matplotlib.projections import register_projection # The sole purpose of this class is to look at the upper, lower, or total # interval as appropriate and see what parts of the tick to draw, if any. class SkewXTick(maxis.XTick): def draw(self, renderer): if not self.get_visible(): return renderer.open_group(self.__name__) lower_interval = self.axes.xaxis.lower_interval upper_interval = self.axes.xaxis.upper_interval if self.gridOn and transforms.interval_contains( self.axes.xaxis.get_view_interval(), self.get_loc()): self.gridline.draw(renderer) if transforms.interval_contains(lower_interval, self.get_loc()): if self.tick1On: self.tick1line.draw(renderer) if self.label1On: self.label1.draw(renderer) if transforms.interval_contains(upper_interval, self.get_loc()): if self.tick2On: self.tick2line.draw(renderer) if self.label2On: self.label2.draw(renderer) renderer.close_group(self.__name__) # This class exists to provide two separate sets of intervals to the tick, # as well as create instances of the custom tick class SkewXAxis(maxis.XAxis): def __init__(self, *args, **kwargs): maxis.XAxis.__init__(self, *args, **kwargs) self.upper_interval = 0.0, 1.0 def _get_tick(self, major): return SkewXTick(self.axes, 0, '', major=major) @property def lower_interval(self): return self.axes.viewLim.intervalx def get_view_interval(self): return self.upper_interval[0], self.axes.viewLim.intervalx[1] # This class exists to calculate the separate data range of the # upper X-axis and draw the spine there. It also provides this range # to the X-axis artist for ticking and gridlines class SkewSpine(mspines.Spine): def _adjust_location(self): trans = self.axes.transDataToAxes.inverted() if self.spine_type == 'top': yloc = 1.0 else: yloc = 0.0 left = trans.transform_point((0.0, yloc))[0] right = trans.transform_point((1.0, yloc))[0] pts = self._path.vertices pts[0, 0] = left pts[1, 0] = right self.axis.upper_interval = (left, right) # This class handles registration of the skew-xaxes as a projection as well # as setting up the appropriate transformations. It also overrides standard # spines and axes instances as appropriate. class SkewXAxes(Axes): # The projection must specify a name. This will be used be the # user to select the projection, i.e. ``subplot(111, # projection='skewx')``. name = 'skewx' def _init_axis(self): #Taken from Axes and modified to use our modified X-axis self.xaxis = SkewXAxis(self) self.spines['top'].register_axis(self.xaxis) self.spines['bottom'].register_axis(self.xaxis) self.yaxis = maxis.YAxis(self) self.spines['left'].register_axis(self.yaxis) self.spines['right'].register_axis(self.yaxis) def _gen_axes_spines(self): spines = {'top':SkewSpine.linear_spine(self, 'top'), 'bottom':mspines.Spine.linear_spine(self, 'bottom'), 'left':mspines.Spine.linear_spine(self, 'left'), 'right':mspines.Spine.linear_spine(self, 'right')} return spines def _set_lim_and_transforms(self): """ This is called once when the plot is created to set up all the transforms for the data, text and grids. """ rot = 30 #Get the standard transform setup from the Axes base class Axes._set_lim_and_transforms(self) # Need to put the skew in the middle, after the scale and limits, # but before the transAxes. This way, the skew is done in Axes # coordinates thus performing the transform around the proper origin # We keep the pre-transAxes transform around for other users, like the # spines for finding bounds self.transDataToAxes = self.transScale + (self.transLimits + transforms.Affine2D().skew_deg(rot, 0)) # Create the full transform from Data to Pixels self.transData = self.transDataToAxes + self.transAxes # Blended transforms like this need to have the skewing applied using # both axes, in axes coords like before. self._xaxis_transform = (transforms.blended_transform_factory( self.transScale + self.transLimits, transforms.IdentityTransform()) + transforms.Affine2D().skew_deg(rot, 0)) + self.transAxes # Now register the projection with matplotlib so the user can select # it. register_projection(SkewXAxes) code to plot a basic skew-T #Create a new figure. The dimensions here give a good aspect ratio fig = plt.figure(figsize=(13.5875, 12.2125)) ax = fig.add_subplot(111, projection='skewx') ax.grid(True) pmax = 1000 pmin = 10 dp = -10 presvals = np.arange(int(pmax), int(pmin)+dp, dp) plt.title('sounding Date/time = 20181102 1200 UTC', fontsize=14, loc='left') # Plot the data using normal plotting functions, in this case using # log scaling in Y direction ax.semilogy(temp_C, press_mb, 'r', lw=2) ax.semilogy(td_C, press_mb, 'g', lw=2) #plot the dry adiabats for theta_K in np.arange(223.15,383.15,10.): temps=(theta_K) / (np.power((1000. / presvals),0.286)) - 273.15 ax.semilogy(temps, presvals, 'r-', alpha=.2) # An example of a slanted line at constant X to highlight 0C isotherm l = ax.axvline(0, color='b', linestyle='--') # Disables the log-formatting that comes with semilogy ax.yaxis.set_major_formatter(plt.ScalarFormatter()) ax.set_yticks(np.linspace(100,1000,10)) ax.set_ylim(1050,100) ax.xaxis.set_major_locator(plt.MultipleLocator(10)) ax.set_xlim(-50,50) plt.show()
# Stuve chart code x = np.arange(220, 460, 10) y = np.arange(100, 1026, 25) theta_2D, P_2D = np.meshgrid(x, y) T_2D=theta_2D*(P_2D/1000.)**0.286 y = np.arange(40000, 102600, 2500) x = np.array([0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.5, 2., 3., 4., 6., 8., 10., 12., 16., 20.]) labels=['0.1', '0.2', '0.4', '0.6', '0.8', '1', '1.5', '2', '3', '4', '6', '8', '10', '12', '16', '20'] x = x/1000. ws_2D, Pws_2D = np.meshgrid(x, y) ws_T_2D=1./(1./273.15-1.844e-4*np.log(ws_2D*Pws_2D/611.3/(ws_2D+0.622))) fig, ax = plt.subplots() ax.set_yscale('log') ax.set_xlabel('temp K') ax.set_ylabel('pressure mb') ax.set_title('Stuve chart') ax.set_xlim(200, 300) ax.set_ylim(1025, 400) ax.minorticks_off() ax.set_xticks(np.arange(200,301,10)) ax.set_yticks([1000,850,700,600,500,400]) ax.set_yticklabels(['1000', '850','700','600','500','400']) ax.grid(True) ax.plot(ws_T_2D,Pws_2D*0.01,color='#a4c2f4',linestyle='dashed') ax.plot(T_2D,P_2D,color='#f6b26b') ax.plot(temp_C+273.15, press_mb, 'r', lw=2) ax.plot(td_C+273.15, press_mb, 'g', lw=2) for i in np.arange(16): ax.text(ws_T_2D[3,i],Pws_2D[3,i]*0.01,labels[i],color='#0000f4',ha='center',weight='bold') ax.text(ws_T_2D[22,i],Pws_2D[22,i]*0.01,labels[i],color='#0000f4',ha='center',weight='bold') fig.set_size_inches(10,8)
|