def rotate_labels(ax: matplotlib.axes.Axes, which: str, rotation: float) -> None: if which == "x": ax.set_xticklabels(ax.get_xticklabels(), rotation=rotation) elif which == "y": ax.set_xticklabels(ax.get_xticklabels(), rotation=rotation) else: raise ValueError("Unknown axis")
def _generate_commute_count_plot(commute_data: pd.DataFrame, ax: mpl.axes.Axes, colours: dict): """ Generate a bar plot of number of commutes per month. Arguments: commute_data - A pandas DataFrame containing the commute activity data. ax - A set of matplotlib axes to generate the plot on. colours - A dictionary of colours to generate the plot with. """ # Group the commute data by month data = commute_data.resample('M').count() # Generate and format the bar plot sns.barplot(x=data.index.to_period('M'), y=data['distance'], color=colours['commute_count'], ax=ax) ax.set(ylabel='Number of commutes', xlabel='Month') ax.set_xticklabels(ax.get_xticklabels(), rotation=45, horizontalalignment='right') ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.grid(b=True, which='major', linewidth=1.0) ax.yaxis.grid(b=True, which='minor', linewidth=0.5) ax.set_axisbelow(True)
def _generate_activity_count_plot(activity_data: pd.DataFrame, ax: mpl.axes.Axes, colours: str): """ Generate a bar plot of activity counts over time (by type). Arguments: activity data - A pandas DataFrame containing the activity data. ax - A set of matplotlib axes to generate the plot on. colours - A name of the colour palette to generate the plot with. """ # Group the activity data by month and calculate the count of each activity type data = (activity_data.groupby([activity_data.index.to_period('M'), 'type' ]).size().to_frame('count').reset_index()) # Generate and format the bar plot sns.barplot(x='start_date_local', y='count', hue='type', data=data, palette=colours, ax=ax) ax.set(title='Activities over time', ylabel='Number of activities', xlabel='Month') ax.set_xticklabels(ax.get_xticklabels(), rotation=45, horizontalalignment='right') ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.grid(b=True, which='major', linewidth=1.0) ax.yaxis.grid(b=True, which='minor', linewidth=0.5) ax.set_axisbelow(True)
def _generate_mean_distance_plot(activity_data: pd.DataFrame, ax: mpl.axes.Axes, colour_palette: list): """ Generate a bar plot of mean activity distance over time (by type). Arguments: activity data - A pandas DataFrame containing the activity data. ax - A set of matplotlib axes to generate the plot on. colour_palette - The colour palette to generate the plot with. """ # Group the activity data by month and calculate the mean distance of each activity type data = activity_data.groupby([activity_data.index.to_period('Y'), 'type']).mean().reset_index() # Generate and format the bar plot sns.barplot(x='start_date_local', y='distance', hue='type', data=data, palette=colour_palette, ax=ax) ax.set(title='Mean activity distance over time', ylabel='Mean distance (km)', xlabel='Year') ax.set_xticklabels(ax.get_xticklabels(), rotation=45, horizontalalignment='right') ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) ax.grid(b=True, which='major', linewidth=1.0) ax.yaxis.grid(b=True, which='minor', linewidth=0.5) ax.set_axisbelow(True)
def update_xticks(ax: matplotlib.axes.Axes, font_size: float = 12, font_weight: str = 'bold') -> None: """Stylize x-ticks.""" ax.xaxis.set_tick_params(width=2) fp = font_manager.FontProperties(weight=font_weight, size=font_size) for label in ax.get_xticklabels(): label.set_fontproperties(fp)
def formatxticklabels(ax: mpl.axes.Axes, horizontalalignment: str = 'right', rotationmode: str = 'anchor', xticklabelrotation: int = 30, xticklabelfontsize: int = 10): for ticklabel in ax.get_xticklabels(): ticklabel.set_horizontalalignment(horizontalalignment) ticklabel.set_rotation_mode(rotationmode) ticklabel.set_rotation(xticklabelrotation) ticklabel.set_fontsize(xticklabelfontsize)
def tick(self, axs: mpl.axes.Axes) -> None: """Modify font of tick-labels.""" if not self.tickfont: return () return axs.get_xticklabels() + axs.get_yticklabels()