def kde_plot(self): """Create a kde plot in seaborn using the style sheet and chosen variable values. :param x_var: The variable you want on the x axis :type x_var: str :param y_var: The variable you want on the y axis :type y_var: str """ warnings.warn("Deprecated: Will be moved into Seaborn.py soon.tm", DeprecationWarning) # Validate the arguments provided self._validate_variable_args(locals(), locals().values(), ["custom_ranking"]) # Set defaults before plotting figure, axis = plt.subplots(figsize=(self.figure_x, self.figure_y)) sns.despine(figure, left=True, bottom=True) # Generate the plot # NB Some arguments are left at default that are given sensible defaults by seaborn plot = sns.kdeplot(data=self._data) # Write out the plot to chosen write directory as a png self.write_plot(plot) return plot
def bar_plot(self, x_var, y_var, gradient_variable): """Create a bar plot in seaborn using the style sheet and chosen variable values. :param x_var: The variable you want on the x axis :type x_var: str :param y_var: The variable you want on the y axis :type y_var: str :param gradient_variable: A variable to apply a gradient of colour to the points :type gradient_variable: str :param size_variable: A variable that will be used to decide the line width :type size_variable: str """ warnings.warn("Deprecated: Will be moved into Seaborn.py soon.tm", DeprecationWarning) # Validate the arguments provided self._validate_variable_args(locals(), locals().values(), ["custom_ranking"]) # Set defaults before plotting figure, axis = plt.subplots(figsize=(self.figure_x, self.figure_y)) sns.despine(figure, left=True, bottom=True) # Generate the plot # NB Some arguments are left at default that are given sensible defaults by seaborn plot = sns.barplot(x=x_var, y=y_var, data=self._data, hue=gradient_variable) # Write out the plot to chosen write directory as a png self.write_plot(plot) return plot
def _forest_plot_plot(self, weight_area): # Instatiate the figure (we will recreate the axes, so don't need them) fig, ax2 = StyleSheet(dpi=300).seaborn_figure(return_figure=True) # TODO: +5 and -5 work with this number of rows/cols but this should be tested # with other data sets to see if it needs to be proportionate to number of rows/cols # Set the figure size so it's proportionate to the amount of data in the figure # todo Agreed, this needs to be generalised but cannot think of how to do that right now. fig.set_size_inches(5, 5) # Make sure the two subplots use available space using tight_layout() plt.tight_layout() # # Remove distance between the table and the graph # plt.subplots_adjust(wspace=0.1, hspace=0) # # Create the subplots to our data requirements (makes sure that the rows of the # # table will line up with the graph we draw). # ax2 = plt.subplot2grid( # (no_of_rows, 2), (1, 1), rowspan=no_of_rows - 1, colspan=1 # ) # Plotting the forest plot # print(self._store_data) # print(self._store_data.iloc[:,3]) ax2.set(xlim=(min(self._store_data.iloc[:, 3]), max(self._store_data.iloc[:, 4])), ylim=(0, 10)) # Make sure the plots are ordered correctly against the table plt.gca().invert_yaxis() # Altering the appearance of the axis ticks and labels ax2.tick_params( axis="y", # changes apply to the x-axis which="both", # both major and minor ticks are affected labelleft=False, # ticks along the bottom edge are off top=False, ) # ticks along the top edge are off sns.despine(left=True) # Ensure that there are as many colours as there are values self.number_of_colours = 10 # Drawing the actual lines and squares on the plot # print(self._store_data.values) # print(self.palette()) for index, ((exposure, eff_size, se, lower, upper), colour) in enumerate( zip(self._store_data.values, self.palette())): # print(eff_size) ax2.plot([lower, upper], [index + 0.5, index + 0.5], color=colour) ax2.plot([eff_size], [index + 0.5], marker="s", markersize=0.1 * weight_area, color=colour) # Draw the dotted line ax2.plot([0, 0], [0, 10], "--", color="White") self.write_plot(plt)
def box_plot( self, x_var, y_var, gradient_variable=None, custom_ranking=None, orientation=None, ): """Create a box plot in seaborn using the style sheet and chosen variable values. :param x_var: The variable you want on the x axis :type x_var: str :param y_var: The variable you want on the y axis :type y_var: str :param gradient_variable: A variable to apply a gradient of colour to the points :type gradient_variable: str :param custom_ranking: A list of rankings to use instead of the default set :type custom_ranking: list :param orientation: Whether plot should be vertical ("v") or horizontal ("h") :type orientation: str :return: The seaborn plot is returned, and .png image saved to the write directory :rtype: matplotlib.axes._subplots.AxesSubplot """ warnings.warn("Deprecated: Will be moved into Seaborn.py soon.tm", DeprecationWarning) # Validate the arguments provided self._validate_variable_args(locals(), locals().values(), ["custom_ranking", "orientation"]) # Set defaults before plotting figure, axis = plt.subplots(figsize=(self.figure_x, self.figure_y)) sns.despine(figure, left=True, bottom=True) # Generate the plot # NB Some arguments are left at default that are given sensible defaults by seaborn plot = sns.boxplot( x=x_var, y=y_var, data=self._data, palette=self.palette(), linewidth=self.outline_width, hue=gradient_variable, orient=orientation, hue_order=custom_ranking, order=custom_ranking, ax=axis, ) # # Write out the plot self.write_plot(plot) return plot
def residual_plot(self, x_var, y_var, ignore_na=True, colour=None, legend_label=None): """Regress y_var on x_var, and then draw a scatterplot of the residuals. :param x_var: The variable you want on the x axis :type x_var: str :param y_var: The variable you want on the y axis :type y_var: str :param ignore_na: If True, ignore observations with missing data when fitting & plotting :type ignore_na: bool :param colour: Colour to use for all elements of the plot :type colour: matplotlib color :param legend_label: Label that will be used in plot legend :type legend_lable: str :return: The seaborn plot is returned, and .png image saved to the write directory :rtype: matplotlib.axes._subplots.AxesSubplot """ warnings.warn("Deprecated: Will be moved into Seaborn.py soon.tm", DeprecationWarning) # Validate the arguments provided self._validate_variable_args(locals(), locals().values(), ["ignore_na", "colour", "legend_label"]) # Set defaults before plotting figure, axis = plt.subplots(figsize=(self.figure_x, self.figure_y)) sns.despine(figure, left=True, bottom=True) # Generate the plot # NB Some arguments are left at default that are given sensible defaults by seaborn plot = sns.residplot( x=x_var, y=y_var, data=self._data, dropna=ignore_na, label=legend_label, color=colour, ax=axis, ) # Write out the plot to chosen write directory as a png self.write_plot(plot) return plot
def seaborn_figure(self, return_figure=False): """ Creates a default figures """ self.set_seaborn_style() figure, axis = plt.subplots(figsize=(self.figure_x, self.figure_y)) sns.despine(figure, left=self.d_spline_left, bottom=self.d_spline_bottom, right=self.d_spline_right, top=self.d_spline_top) axis.spines["bottom"].set_color("White") axis.xaxis.label.set_color("White") axis.tick_params(axis='x', colors='White') if return_figure: return figure, axis else: return axis