def __spacetime_diagram_o_prime_frame(self): # from (x,t) to (x',t') def tr(x_prime, t_prime): x_prime, t_prime = np.asarray(x_prime), np.asarray(t_prime) return self.lorentz_transformations.transform( x_prime, t_prime, self.velocity) # form (x',t') to (x,t) def inv_tr(x, t): x, t = np.asarray(x), np.asarray(t) return self.lorentz_transformations.transform(x, t, -self.velocity) grid_helper = GridHelperCurveLinear((tr, inv_tr)) ax = SubplotHost(self.fig, 1, 2, 2, grid_helper=grid_helper) self.fig.add_subplot(ax) ax.set_xlabel("x'", loc="center") ax.set_ylabel("t'", loc="center") # O x axis ax.axis["x1"] = x1 = ax.new_floating_axis(0, 0) x1.label.set_text("x") # O t axis ax.axis["t1"] = t1 = ax.new_floating_axis(1, 0) t1.label.set_text("t") self.__add_x_and_y_axis(ax) ax.format_coord = self.__format_coord_o_prime_frame self.__remove_ticks(ax, x1, t1) self.world_lines_plotter.transform_and_plot(plt, ax, self.velocity)
def curvelinear_test2(fig): """ polar projection, but in a rectangular box. """ global ax1 import numpy as np import mpl_toolkits.axisartist.angle_helper as angle_helper from matplotlib.projections import PolarAxes from matplotlib.transforms import Affine2D from mpl_toolkits.axisartist import SubplotHost from mpl_toolkits.axisartist import GridHelperCurveLinear # see demo_curvelinear_grid.py for details tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, np.inf), ) grid_locator1 = angle_helper.LocatorDMS(12) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) # Now creates floating axis #grid_helper = ax1.get_grid_helper() # floating axis whose first coordinate (theta) is fixed at 60 ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60) axis.label.set_text(r"$\theta = 60^{\circ}$") axis.label.set_visible(True) # floating axis whose second coordinate (r) is fixed at 6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) axis.label.set_text(r"$r = 6$") ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
def curvelinear_test(fig): """Polar projection, but in a rectangular box. """ # 创建一个极坐标变换。PolarAxes.PolarTransform使用弧度,但本例 # 要设置的坐标系中角度的单位为度 tr = Affine2D().scale(np.pi / 180., 1.) + PolarAxes.PolarTransform() # 极坐标投影涉及到周期,在坐标上也有限制,需要一种特殊的方法来找到 # 坐标的最小值和最大值 extreme_finder = angle_helper.ExtremeFinderCycle( 20, 20, lon_cycle=360, lat_cycle=None, lon_minmax=None, lat_minmax=(0, np.inf), ) # 找到适合坐标的网格值(度、分、秒) grid_locator1 = angle_helper.LocatorDMS(12) # 使用适当的Formatter。请注意,可接受的Locator和Formatter类 # 与Matplotlib中的相应类稍有不同,后者目前还不能直接在这里使用 tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) # 创建浮动坐标轴 # 浮动坐标轴的第一个坐标(theta)指定为60度 ax1.axis["lat"] = axis = ax1.new_floating_axis(0, 60) axis.label.set_text(r"$\theta = 60^{\circ}$") axis.label.set_visible(True) # 浮动坐标轴的第二个坐标(r)指定为6 ax1.axis["lon"] = axis = ax1.new_floating_axis(1, 6) axis.label.set_text(r"$r = 6$") ax1.set_aspect(1.) ax1.set_xlim(-5, 12) ax1.set_ylim(-5, 10) ax1.grid(True)
def SemiPolarPlot(fig): # see demo_curvelinear_grid.py for details tr = Affine2D().scale(pi/180., 1.) + PolarAxes.PolarTransform() extreme_finder = angle_helper.ExtremeFinderCycle(20, 20, lon_cycle = 360, lat_cycle = None, lon_minmax = None, lat_minmax = (0, 1), ) grid_locator1 = angle_helper.LocatorDMS(11) grid_locator2 = FixedLocator([0.25, 0.5, 1., 0.75]) tick_formatter1 = angle_helper.FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, grid_locator2=grid_locator2, tick_formatter1=tick_formatter1, tick_formatter2=None ) ax1 = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) fig.add_subplot(ax1) ax1.axis["top"].set_visible(False) ax1.axis["right"].set_visible(False) ax1.axis["bottom"].set_visible(False) ax1.axis["lon"] = ax1.new_floating_axis(1, 1) ax1.set_aspect(1) ax1.set_xlim(0, 2) ax1.set_ylim(-1., 1.) ax1.grid(True) curved_ax = ax1.get_aux_axes(tr) curved_ax.patch = ax1.patch # for aux_ax to have a clip path as in ax ax1.patch.zorder=0.9 return ax1, curved_ax
def sgrid(): # From matplotlib demos: # https://matplotlib.org/gallery/axisartist/demo_curvelinear_grid.html # https://matplotlib.org/gallery/axisartist/demo_floating_axis.html # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction sampling_points = 20 extreme_finder = ModifiedExtremeFinderCycle(sampling_points, sampling_points, lon_cycle=360, lat_cycle=None, lon_minmax=(90,270), lat_minmax=(0, np.inf),) grid_locator1 = angle_helper.LocatorDMS(15) tick_formatter1 = FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) fig = plt.figure() ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) # make ticklabels of right invisible, and top axis visible. visible = True ax.axis[:].major_ticklabels.set_visible(visible) ax.axis[:].major_ticks.set_visible(False) ax.axis[:].invert_ticklabel_direction() ax.axis["wnxneg"] = axis = ax.new_floating_axis(0, 180) axis.set_ticklabel_direction("-") axis.label.set_visible(False) ax.axis["wnxpos"] = axis = ax.new_floating_axis(0, 0) axis.label.set_visible(False) ax.axis["wnypos"] = axis = ax.new_floating_axis(0, 90) axis.label.set_visible(False) axis.set_axis_direction("left") ax.axis["wnyneg"] = axis = ax.new_floating_axis(0, 270) axis.label.set_visible(False) axis.set_axis_direction("left") axis.invert_ticklabel_direction() axis.set_ticklabel_direction("-") # let left axis shows ticklabels for 1st coordinate (angle) ax.axis["left"].get_helper().nth_coord_ticks = 0 ax.axis["right"].get_helper().nth_coord_ticks = 0 ax.axis["left"].get_helper().nth_coord_ticks = 0 ax.axis["bottom"].get_helper().nth_coord_ticks = 0 fig.add_subplot(ax) ### RECTANGULAR X Y AXES WITH SCALE #par2 = ax.twiny() #par2.axis["top"].toggle(all=False) #par2.axis["right"].toggle(all=False) #new_fixed_axis = par2.get_grid_helper().new_fixed_axis #par2.axis["left"] = new_fixed_axis(loc="left", # axes=par2, # offset=(0, 0)) #par2.axis["bottom"] = new_fixed_axis(loc="bottom", # axes=par2, # offset=(0, 0)) ### FINISH RECTANGULAR ax.grid(True, zorder=0,linestyle='dotted') _final_setup(ax) return ax, fig
def sgrid(): # From matplotlib demos: # https://matplotlib.org/gallery/axisartist/demo_curvelinear_grid.html # https://matplotlib.org/gallery/axisartist/demo_floating_axis.html # PolarAxes.PolarTransform takes radian. However, we want our coordinate # system in degree tr = Affine2D().scale(np.pi/180., 1.) + PolarAxes.PolarTransform() # polar projection, which involves cycle, and also has limits in # its coordinates, needs a special method to find the extremes # (min, max of the coordinate within the view). # 20, 20 : number of sampling points along x, y direction sampling_points = 20 extreme_finder = ModifiedExtremeFinderCycle(sampling_points, sampling_points, lon_cycle=360, lat_cycle=None, lon_minmax=(90,270), lat_minmax=(0, np.inf),) grid_locator1 = angle_helper.LocatorDMS(15) tick_formatter1 = FormatterDMS() grid_helper = GridHelperCurveLinear(tr, extreme_finder=extreme_finder, grid_locator1=grid_locator1, tick_formatter1=tick_formatter1 ) fig = plt.figure() ax = SubplotHost(fig, 1, 1, 1, grid_helper=grid_helper) # make ticklabels of right invisible, and top axis visible. visible = True ax.axis[:].major_ticklabels.set_visible(visible) ax.axis[:].major_ticks.set_visible(False) ax.axis[:].invert_ticklabel_direction() ax.axis["wnxneg"] = axis = ax.new_floating_axis(0, 180) axis.set_ticklabel_direction("-") axis.label.set_visible(False) ax.axis["wnxpos"] = axis = ax.new_floating_axis(0, 0) axis.label.set_visible(False) ax.axis["wnypos"] = axis = ax.new_floating_axis(0, 90) axis.label.set_visible(False) axis.set_axis_direction("left") ax.axis["wnyneg"] = axis = ax.new_floating_axis(0, 270) axis.label.set_visible(False) axis.set_axis_direction("left") axis.invert_ticklabel_direction() axis.set_ticklabel_direction("-") # let left axis shows ticklabels for 1st coordinate (angle) ax.axis["left"].get_helper().nth_coord_ticks = 0 ax.axis["right"].get_helper().nth_coord_ticks = 0 ax.axis["left"].get_helper().nth_coord_ticks = 0 ax.axis["bottom"].get_helper().nth_coord_ticks = 0 fig.add_subplot(ax) ### RECTANGULAR X Y AXES WITH SCALE #par2 = ax.twiny() #par2.axis["top"].toggle(all=False) #par2.axis["right"].toggle(all=False) #new_fixed_axis = par2.get_grid_helper().new_fixed_axis #par2.axis["left"] = new_fixed_axis(loc="left", # axes=par2, # offset=(0, 0)) #par2.axis["bottom"] = new_fixed_axis(loc="bottom", # axes=par2, # offset=(0, 0)) ### FINISH RECTANGULAR ax.grid(True, zorder=0,linestyle='dotted') _final_setup(ax) return ax, fig