def plot_moment(self): fig = figure((10, 4)) plot_signal(self.t, self.torque_disturbance() * 1000, plot_label="Disturbance", color="darkred", zorder=2) plot_signal(self.t[:-2], moving_average(self.torque_omega_dot(), 3) * 1000, plot_label="Steering", color="#fcdc9a", zorder=1) plot_signal( self.t[:-2], moving_average( (self.torque_omega_dot() + self.torque_disturbance()), 3) * 1000, plot_label="Total", color="darkblue", zorder=0, # Axes x_bounds=[0, 24], y_bounds=[-6, 6], # Labels x_label="t [s]", y_label="T [mNm]") import matplotlib.pyplot as plt plt.legend(prop={'family': 'monospace'}) plt.tight_layout() plt.show()
def f_delta(self): fig = figure((12, 4)) plot_signal(x=self.t, y=deg(self.psi), color="orange", plot_label="Ψ [deg]") plot_signal(x=self.t, y=deg(self.delta), color="green", plot_label="δ [deg]") plot_signal( x=self.t, y=deg(self.omega), plot_label="ω [deg s-1]", # Axes x_label="t [s]", y_label="[deg]", # Ticks tick_number=13, # Bounds x_bounds=[0, 24], y_bounds=[-110, 110]) import matplotlib.pyplot as plt plt.legend(prop={'family': 'monospace'}) plt.tight_layout() plt.show()
def test_demo(self): fig = figure((18, 8), backend=backend) ax1 = plt.subplot2grid((2, 6), (0, 0), colspan=2, rowspan=2, projection='3d', facecolor="#fff6e6") ax2 = plt.subplot2grid((2, 6), (0, 3), rowspan=1, aspect=1) ax3 = plt.subplot2grid((2, 6), (1, 3), rowspan=1, aspect=1) ax4 = plt.subplot2grid((2, 6), (0, 5), rowspan=1, aspect=1) ax5 = plt.subplot2grid((2, 6), (1, 5), rowspan=1, aspect=1) surface(fig=fig, ax=ax1, title="Demo", title_size=70, title_weight="bold", title_font="Pump Triline", title_color="#e69300", plot_label="Surface", background_color_plot="#fff6e6", edge_color="lightgrey", edges_to_rgba=True, azim=-160, elev=43) fill_area(fig=fig, ax=ax2, plot_label="Fill", aspect=1, grid=False) quiver(fig=fig, ax=ax3, plot_label="Quiver", grid=False) heatmap(fig=fig, ax=ax4, title="No label", plot_label="Heatmap", grid=False) streamline(fig=fig, ax=ax5, line_density=1, title="No label", plot_label="Streamline", grid=False) custom_canvas2(fig=fig, ax=ax2, background_color_figure="#fff6e6", legend=True, legend_loc=(0.6725, 0.425), resize_axes=False) if show: plt.show()
def f_path(self): fig = figure((12, 4)) scatter(x=self.x, y=self.y, plot_label="Reference points", marker="x", point_size=30, color="#7aa9ff", zorder=0, resize_axes=False) plot_signal( x=self.rx, y=self.path, color="darkorange", plot_label="Path", # Axes x_label="x [m]", y_label="y [m]", # Ticks tick_number=10, # Bounds x_bounds=[0, 54], y_bounds=[-2, 16]) import matplotlib.pyplot as plt # Contour plots x_0 = np.arange(0, 9, self.dx) y_0 = np.zeros(len(x_0)) x_c60 = np.arange(9, self.X_60, self.dx) y_c60 = np.linspace(0, self.Y_60, len(x_c60)) x_60 = np.arange(self.X_60, self.X_60 + 12, self.dx) y_60 = self.Y_60 * np.ones(len(x_60)) x_90 = np.arange(self.X_60 + 12, self.x_90[-1], self.dx) y_90 = np.zeros(len(x_90)) x_ref = np.concatenate((x_0, x_c60, x_60, x_90)) y_ref = np.concatenate((y_0, y_c60, y_60, y_90)) plt.plot(x_ref, y_ref, label="Reference", linewidth=1, linestyle="--", zorder=0) # Other plt.gca().set_facecolor('#ffeed9') fig.patch.set_facecolor('#ffeed9') plt.legend(prop={'family': 'monospace'}) plt.tight_layout() plt.show()
def plot_gear_ratio(self): fig = figure((6, 4)) plot_signal( self.t, np.cos(self.delta_f), # Labels x_label="t [s]", y_label="GR [-]", # Ticks tick_number=6, # Axes x_bounds=[0, 24], y_bounds=[0, 2]) import matplotlib.pyplot as plt plt.tight_layout() plt.show()
def n_pane_single(cls, x, y, labels=None, legend_labels=None, filename=None, where_does_this_go=None, **kwargs): fig = figure((5 * len(y), 3.5)) for i in range(len(y)): ax_transient = plt.subplot2grid((1, len(y)), (0, i), rowspan=1, colspan=1) if i < (len(y) - 1): line(x=x, y=y[i], color=one()[i], ax=ax_transient, fig=fig, y_label=labels[i] if not isinstance(labels, type(None)) else None, plot_label=legend_labels[i] if not isinstance(legend_labels, type(None)) else None) else: line(x=x, y=y[i], color=one()[i], ax=ax_transient, fig=fig, y_label=labels[i] if not isinstance(labels, type(None)) else None, legend=True if not isinstance(legend_labels, type(None)) else False, plot_label=legend_labels[i] if not isinstance(legend_labels, type(None)) else None, legend_loc=(0.875, 0.425), **kwargs) plt.subplots_adjust(left=0.1, right=0.85, wspace=0.6, hspace=0.35) if not isinstance(filename, type(None)) and not isinstance( where_does_this_go, type(None)): plt.savefig(f"{where_does_this_go}/{filename}.pdf") plt.show()
def test_basic(self): # backend = "Qt5Agg" # None -> regular non-interactive matplotlib output fig = figure(figsize=(10, 10), backend=backend) ax0 = plt.subplot2grid((2, 2), (0, 0), rowspan=1, aspect=1, fig=fig) ax1 = plt.subplot2grid((2, 2), (1, 0), rowspan=1, aspect=1, fig=fig) ax2 = plt.subplot2grid((2, 2), (0, 1), rowspan=1, aspect=1, fig=fig) ax3 = plt.subplot2grid((2, 2), (1, 1), rowspan=1, aspect=12, fig=fig) axes = [ax0, ax1, ax2, ax3] plots = [line, quiver, streamline, fill_area] for i in range(len(plots)): plots[i](fig=fig, ax=axes[i], backend=backend) if show: plt.show()
def n_pane_comparison(cls, t, y, axis_labels=None, legend_labels=None, filename=None, where_does_this_go=None, zorders=None, colors=None, alphas=None, save=False, show=True): t, y = to_ndarrays(t, y) t = cls.comparison_input_match(t, y) zorders = if_none(zorders, np.arange(len(y) + 1, 0, -1)) colors = if_none(colors, [one()[n] for n in range(len(y))]) print(colors) alphas = if_none(alphas, np.ones(len(y))) fig = figure((5 * len(y), 3.5)) for i in range(len(y)): ax_transient = plt.subplot2grid((1, len(y)), (0, i), rowspan=1, colspan=1) if i < (len(y) - 1): cls.comparison( [t[i][n] for n in range(len(y[1]))], [y[i][n] for n in range(len(y[1]))], ax_transient, fig, y_label=axis_labels[i] if not isinstance(axis_labels, type(None)) else None, zorders=zorders, colors=colors, alphas=alphas, ) else: cls.comparison( [t[i][n] for n in range(len(y[1]))], [y[i][n] for n in range(len(y[1]))], ax_transient, fig, y_label=axis_labels[i] if not isinstance(axis_labels, type(None)) else None, zorders=zorders, colors=colors, alphas=alphas, plot_labels=legend_labels, legend=True if not isinstance(legend_labels, type(None)) else False, legend_loc=(0.875, 0.425)) plt.subplots_adjust(left=0.1, right=0.85, wspace=0.6, hspace=0.35) legend = (c for c in ax_transient.get_children() if isinstance(c, mpl.legend.Legend)) if save: filename = input("Filename:") if isinstance(filename, type(None)) \ else filename where_does_this_go = input("Destination directory:") if isinstance(where_does_this_go, type(None)) \ else where_does_this_go try: plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) except FileNotFoundError: print_color( "Destination directory does not exist. Destination directory:", "blue") where_does_this_go = input() plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) if show: plt.show()
def n_pane_single(cls, x, y, labels=None, legend_labels=None, filename=None, where_does_this_go=None, **kwargs): # Regular defaults backend = kwargs.pop('backend', None) # Setup legend_loc = kwargs.pop('legend_loc', (0.875, 0.55)) # Legend show = kwargs.pop('show', False) # Display save = kwargs.pop('save', False) legend = kwargs.pop('legend', True if not isinstance(legend_labels, type(None)) else True) # Legend # Figure setup fig = figure((5 * len(y), 3.5), backend=backend) import matplotlib.pyplot as plt # Plot for i in range(len(y)): ax_transient = plt.subplot2grid((1, len(y)), (0, i), rowspan=1, colspan=1) if i < (len(y) - 1): line(x=x, y=y[i], color=one()[i], ax=ax_transient, fig=fig, y_label=labels[i] if not isinstance(labels, type(None)) else None, plot_label=legend_labels[i] if not isinstance(legend_labels, type(None)) else None, backend=backend) else: line(x=x, y=y[i], color=one()[i], ax=ax_transient, fig=fig, y_label=labels[i] if not isinstance(labels, type(None)) else None, legend=True if not isinstance(legend_labels, type(None)) else False, plot_label=legend_labels[i] if not isinstance(legend_labels, type(None)) else None, legend_loc=legend_loc, backend=backend, **kwargs) plt.subplots_adjust(left=0.1, right=0.85, wspace=0.6, hspace=0.35) if not isinstance(filename, type(None)) and not isinstance( where_does_this_go, type(None)): plt.savefig(f"{where_does_this_go}/{filename}.pdf") plt.show() if show: plt.show() if save: filename = input("Filename:") if isinstance(filename, type(None)) \ else filename where_does_this_go = input("Destination directory:") if isinstance(where_does_this_go, type(None)) \ else where_does_this_go try: plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) except FileNotFoundError: print_color( "Destination directory does not exist. Destination directory:", "blue") where_does_this_go = input() plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) if show: plt.show()
def n_pane_comparison(cls, t, y, axis_labels=None, legend_labels=None, zorders=None, colors=None, alphas=None, filename=None, where_does_this_go=None, **kwargs): # Input check t, y = lists_to_ndarrays(t, y) t = cls.comparison_input_match(t, y) # Special parameters zorders = if_none(zorders, np.arange(len(y) + 1, 0, -1)) colors = if_none(colors, [one()[n] for n in range(len(y))]) alphas = if_none(alphas, np.ones(len(y))) # Regular defaults backend = kwargs.pop('backend', None) # Setup legend_loc = kwargs.pop('legend_loc', (0.875, 0.55)) # Legend show = kwargs.pop('show', False) # Display save = kwargs.pop('save', False) # Figure setup fig = figure((5 * len(y), 3.5), backend=backend) import matplotlib.pyplot as plt # Plot for i in range(len(y)): ax_transient = plt.subplot2grid((1, len(y)), (0, i), rowspan=1, colspan=1) if i < (len(y) - 1): cls.comparison( [t[i][n] for n in range(len(y[1]))], [y[i][n] for n in range(len(y[1]))], ax=ax_transient, fig=fig, backend=backend, y_label=axis_labels[i] if not isinstance(axis_labels, type(None)) else None, zorders=zorders, colors=colors, alphas=alphas, legend=False) else: cls.comparison( [t[i][n] for n in range(len(y[1]))], [y[i][n] for n in range(len(y[1]))], ax=ax_transient, fig=fig, backend=backend, y_label=axis_labels[i] if not isinstance(axis_labels, type(None)) else None, zorders=zorders, colors=colors, alphas=alphas, plot_labels=legend_labels, legend=True if not isinstance(legend_labels, type(None)) else False, legend_loc=legend_loc, **kwargs) plt.subplots_adjust(left=0.1, right=0.85, wspace=0.6, hspace=0.35) legend = (c for c in ax_transient.get_children() if isinstance(c, mpl.legend.Legend)) if save: filename = input("Filename:") if isinstance(filename, type(None)) \ else filename where_does_this_go = input("Destination directory:") if isinstance(where_does_this_go, type(None)) \ else where_does_this_go try: plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) except FileNotFoundError: print_color( "Destination directory does not exist. Destination directory:", "blue") where_does_this_go = input() plt.savefig( f"{where_does_this_go}/{filename}.pdf", bbox_extra_artists=legend, ) if show: plt.show()
class Plot: mc = MC() fig = figure((15, 10)) n_ticks = 3 tick_label_size = 10 axis_font_size = 15 axis_title_pad = 10 def reaction(self): self.y_bounds = [-100, 100] self.torque_bound = 13 self.torque_bound_factor = self.torque_bound / max( self.mc.forward_motoring()[0]) self.main() def reaction_s(self): self.saturation = True self.n_ticks = 17 self.y_bounds = [-20, 20] self.torque_bound = self.mc.forward_motoring()[0].max() / ( self.mc.forward_braking()[1].max() / 20) self.torque_bound_factor = self.torque_bound / max( self.mc.forward_motoring()[0]) self.main() def main(self): import matplotlib.pyplot as plt self.basic(x=self.mc.forward_motoring()[0], y=self.mc.forward_motoring()[1], color="darkred") self.basic(x=self.mc.forward_braking()[0], y=self.mc.forward_braking()[1], color="darkred") self.basic(x=self.mc.reverse_braking()[0], y=self.mc.reverse_braking()[1], color="darkred") self.zero_lines() self.heat_lines() self.fill_continous_operation() self.fill_intermittent_operation() LC().plot() self.style(x=self.mc.reverse_motoring()[0], y=self.mc.reverse_motoring()[1], color="darkred") plt.xticks(np.linspace(-max(self.mc.forward_motoring()[0]), max(self.mc.forward_motoring()[0]), self.n_ticks), fontname="serif", fontsize=self.tick_label_size) self.second_axis() plt.show() def basic(self, x, y, color): line( x=x, y=y, color=color, line_width=2, spines_removed=[], fig=self.fig, ) def style(self, x, y, color): line(x=x, y=y, color=color, line_width=2, x_bounds=[-self.torque_bound, self.torque_bound], x_upper_resize_pad=0, x_lower_resize_pad=0, y_bounds=self.y_bounds, y_upper_resize_pad=0, y_lower_resize_pad=0, y_custom_tick_locations=[ self.mc.forward_braking()[1].max(), self.mc.reverse_braking()[1].min() ], spines_removed=["top", "right"], x_tick_number=self.n_ticks, y_tick_number=self.n_ticks, tick_ndecimals=2, x_tick_label_size=self.tick_label_size, y_tick_label_size=self.tick_label_size, grid=True, grid_color="lightgrey", grid_lines="--", title="", x_label="T [Nm]", x_label_size=self.axis_font_size, y_label="ω [rad s-1]", y_label_size=self.axis_font_size, y_label_rotation=90, x_label_pad=self.axis_title_pad, y_label_pad=5, fig=self.fig, resize_axes=True) def second_axis(self): """ Second axis """ import matplotlib.pyplot as plt from matplotlib.ticker import FormatStrFormatter ax2 = plt.gca().twiny() ax2.set_xlim(left=-0.920 * self.torque_bound_factor, right=0.920 * self.torque_bound_factor) ax2.xaxis.set_major_formatter(FormatStrFormatter('%.2f')) plt.xticks(self.mc.i(self.n_ticks), fontname="serif", fontsize=self.tick_label_size) ax2.set_title("I [A]", fontname="serif", fontsize=self.axis_font_size, pad=self.axis_title_pad) def zero_lines(self): """ Zero lines """ import matplotlib.pyplot as plt plt.axvline(0, color="grey", linestyle="--") plt.axhline(0, color="grey", linestyle="--") def heat_lines(self): """ Heat lines """ import matplotlib.pyplot as plt plt.axvline(self.mc.t_max_thermal(), color="red", linestyle="-") plt.axvline(-self.mc.t_max_thermal(), color="red", linestyle="-") def fill_continous_operation(self): # Thermal limit intersection limit = self.mc.t_max_thermal() idx_low = find_nearest_idx(self.mc.forward_braking()[0], -limit) idx_high = find_nearest_idx(self.mc.forward_motoring()[0], limit) x = np.concatenate((self.mc.forward_braking()[0][idx_low::], self.mc.forward_motoring()[0][::idx_high])) y = np.concatenate((self.mc.forward_braking()[1][idx_low::], self.mc.forward_motoring()[1][::idx_high])) z = np.concatenate((self.mc.reverse_motoring()[1][idx_low::], self.mc.reverse_braking()[1][::idx_high])) fill_area(x=x, y=y, z=z, between=True, color="#c7ffd6", resize_axes=False, zorder=0) def fill_intermittent_operation(self): # Thermal limit intersection limit = self.mc.t_max_thermal() idx_low = find_nearest_idx(self.mc.forward_braking()[0], -limit) idx_high = find_nearest_idx(self.mc.forward_motoring()[0], limit) x_low = self.mc.forward_braking()[0][:idx_low] y_low = self.mc.forward_braking()[1][:idx_low] z_low = self.mc.reverse_motoring()[1][:idx_low] fill_area(x=x_low, y=y_low, z=z_low, between=True, color="darkred", resize_axes=False, zorder=0) x_high = self.mc.forward_motoring()[0][idx_high:] y_high = self.mc.forward_motoring()[1][idx_high:] z_high = self.mc.reverse_braking()[1][idx_high:] fill_area(x=x_high, y=y_high, z=z_high, between=True, color="darkred", resize_axes=False, zorder=0)