Matplotlib utilities¶
This module contains a number of helper functions for matplotlib.
Adjusting axis properties¶
Fonts
set_legend_title_fontsize (ax, fontsize) |
Set the font size of the title of the legend. |
set_legend_fontsize (ax, fontsize) |
Set the font size of the items of the legend. |
set_title_fontsize (ax, fontsize) |
Set the font size of the title of the axis. |
set_label_fontsize (ax, fontsize[, axis]) |
Set the font size of the label of the axis. |
set_ticklabels_fontsize (ax, fontsize[, …]) |
Set the font size of the tick labels. |
set_ticklabel_rotation (ax, rotation[, axis, …]) |
Set the rotation of the tick labels. |
Axes
center_splines (ax) |
This function places the splines of the given axis in the center of the plot. |
hide_first_y_tick_label (ax) |
Hide the first tick label on the y-axis. |
hide_tick_labels_by_text (ax[, to_remove_x, …]) |
Hide tick labels which match the given values. |
hide_tick_labels_by_index |
Creating standard plots¶
plot_simple_bar_chart |
|
plot_simple_scatter (x, y[, ax, …]) |
Plot a simple scatter plot of x vs. |
plot_stacked_bar_graph |
|
plot_sorted_values (values[, ymin, ymax, ax, …]) |
Sort values and plot them |
Plotting standard machine learning and statistical results¶
plot_binary_prediction_scores |
|
plot_confusion_matrix (confusion_matrix[, …]) |
Plot the given confusion matrix |
plot_roc_curve (tpr, fpr[, auc, field_names, …]) |
|
plot_trend_line (ax, x, intercept, slope, …) |
Draw the trend line implied by the given coefficients. |
plot_venn_diagram (sets[, ax, set_labels, …]) |
This function is a wrapper around matplotlib_venn. |
Other helpers¶
add_fontsizes_to_args (args[, …]) |
Add reasonable default fontsize values to the arguments |
draw_rectangle (ax, base_x, base_y, width, height) |
Draw a rectangle at the given x and y coordinates. |
get_diff_counts (data_np) |
This function extracts the differential counts necessary for visualization with stacked_bar_graph. |
Definitions¶
This module contains a number of helper functions for matplotlib.
-
pyllars.mpl_utils.
add_fontsizes_to_args
(args, legend_title_fontsize=12, legend_fontsize=10, title_fontsize=20, label_fontsize=15, ticklabels_fontsize=10)[source]¶ Add reasonable default fontsize values to the arguments
-
pyllars.mpl_utils.
center_splines
(ax)[source]¶ This function places the splines of the given axis in the center of the plot. This is useful for things like scatter plots where (0,0) should be in the center of the plot.
Parameters: ax (mpl.Axis) – The axis Returns: Return type: None, but the splines are updated
-
pyllars.mpl_utils.
create_stacked_bar_graph
(ax, data, colors=<matplotlib.colors.LinearSegmentedColormap object>, x_tick_labels=None, stack_labels=None, y_ticks=None, y_tick_labels=None, hide_first_ytick=True, edge_colors=None, showFirst=-1, scale=False, widths=None, heights=None, y_title=None, x_title=None, gap=0.0, end_gaps=False, show_legend=True, legend_loc='best', legend_bbox_to_anchor=None, legend_ncol=-1, log=False, font_size=8, label_font_size=12, legend_font_size=8)[source]¶ Create a stacked bar plot with the given characteristics.
This code is adapted from code by Michael Imelfort.
-
pyllars.mpl_utils.
draw_rectangle
(ax, base_x, base_y, width, height, center_x=False, center_y=False, **kwargs)[source]¶ Draw a rectangle at the given x and y coordinates. Optionally, these can be adjusted such that they are the respective centers rather than edge values.
Parameters: - ax (mpl.Axis) – The axis on which the rectangle will be drawn
- base_{x,y} (number) – The base x and y coordinates
- height (width,) – The width (change in x) and height (change in y) of the rectangle
- center_{x,y} (bool) – Whether to adjust the x and y coordinates such that they become the center rather than lower left. In particular, if center_x is True, then base_x will be shifted left by width/2; likewise, if center_y is True, then base_y will be shifted down by height/2.
- kwargs (key=value pairs) – Additional keywords are passed to the patches.Rectangle constructor
- base –
-
pyllars.mpl_utils.
get_diff_counts
(data_np)[source]¶ This function extracts the differential counts necessary for visualization with stacked_bar_graph. It assumes the counts for each bar are given as a separate row in the numpy 2-d array. Within the rows, the counts are ordered in ascending order. That is, the first column contains the smallest count, the second column contains the next-smallest count, etc.
For example, if the columns represnt some sort of filtering approach, then the last column would contain the unfiltered count, the next-to-last column would give the count after the first round of filtering, etc.
-
pyllars.mpl_utils.
hide_first_y_tick_label
(ax)[source]¶ Hide the first tick label on the y-axis.
Parameters: ax (mpl.Axis) – The axis Returns: Return type: None, but the tick label is hidden
-
pyllars.mpl_utils.
hide_tick_labels
(ax, keep_x=[], keep_y=[], axis='both')[source]¶ Hide the tick labels on both axes. Optionally, some can be preserved.
Parameters: - ax (mp.Axis) – The axis
- keep_{x,y} (list-like of ints) – The indices of any x-axis ticks to keep. The numbers are passed directly as indices to the xticks array.
- axis (string in {'both', 'x', 'y'}) – Axis of the tick labels to hide
Returns: Return type: None, but the tick labels of the axis are removed, as specified
-
pyllars.mpl_utils.
hide_tick_labels_by_text
(ax, to_remove_x=[], to_remove_y=[])[source]¶ Hide tick labels which match the given values.
Parameters: - ax (mpl.Axis) – The axis
- to_remove_{x,y} (list-like of strings) – The values to remove
-
pyllars.mpl_utils.
plot_confusion_matrix
(confusion_matrix, ax=None, show_cell_labels=True, show_colorbar=True, title='Confusion matrix', cmap=None, true_tick_labels=None, predicted_tick_labels=None, ylabel='True labels', xlabel='Predicted labels', title_font_size=20, label_font_size=15, true_tick_rotation=None, predicted_tick_rotation=None, out=None)[source]¶ Plot the given confusion matrix
-
pyllars.mpl_utils.
plot_simple_scatter
(x, y, ax=None, equal_aspect=True, set_lim=True, show_y_x_line=True, xy_line_kwargs={}, **kwargs)[source]¶ Plot a simple scatter plot of x vs. y on ax
If fig and ax are not given, then will be created.
- See the matplotlib documentation for more keyword arguments and details:
- https://matplotlib.org/api/_as_gen/matplotlib.pyplot.plot.html
Parameters: - x,y (array-like of numbers) – The values to plot
- ax (mpl.Axis) – An axis for plotting. If this is not given, then a figure and axis will be created.
- equal_aspect (bool) – Whether to set the aspect of the axis to equal
- set_lim (bool) – Whether to automatically set the min and max axis limits
- show_y_x_line (bool) – Whether to draw the y=x line. This will look weird if set_lim is False.
- xy_line_kwargs (dict) – keyword arguments for plotting the y=x line, if it plotting
- **kwargs (<key>=<value> pairs) –
Additional keyword arguments to pass to the plot function. Some useful keyword arguments are:
- label : the label for a legend
- marker : https://matplotlib.org/examples/lines_bars_and_markers/marker_reference.html
Returns: fig, ax – The figure and axis on which the scatter points were plotted
Return type: mpl.Figure and mpl.Axis
-
pyllars.mpl_utils.
plot_sorted_values
(values, ymin=None, ymax=None, ax=None, scale_x=False, **kwargs)[source]¶ Sort values and plot them
Parameters: - values (list-like of numbers) – The values to plot
- y{min,max} (floats) – The min and max values for the y-axis. If not given, then these default to the minimum and maximum values in the list.
- scale_x (bool) – If True, then the x values will be equally-spaced between 0 and 1. Otherwise, they will be the values 0 to len(values)
- ax (mpl.Axis) – An axis for plotting. If this is not given, then a figure and axis will be created.
- **kwargs (<key>=<value> pairs) –
Additional keyword arguments to pass to the plot function. Some useful keyword arguments are:
- label : the label for a legend
- lw : the line width
- ls : https://matplotlib.org/gallery/lines_bars_and_markers/line_styles_reference.html
- marker : https://matplotlib.org/examples/lines_bars_and_markers/marker_reference.html
Returns: - fig (mpl.Figure) – The Figure associated with ax, or a new Figure
- ax (mpl.Axis) – Either ax or a new Axis
-
pyllars.mpl_utils.
plot_trend_line
(ax, x, intercept, slope, power, **kwargs)[source]¶ Draw the trend line implied by the given coefficients.
Parameters: - ax (mpl.Axis) – The axis on which the line will be drawn
- x (list of floats) – The points at which the line will be drawn
- slope, power (intercept,) – The coefficients of the trend line
- **kwargs (<key>=<value> pairs) – Keyword arguments to pass to the ax.plot function (color, etc.)
Returns: Return type: None, but the line will be drawn on the axis
-
pyllars.mpl_utils.
plot_venn_diagram
(sets, ax=None, set_labels=None, weighted=False, use_sci_notation=False, labels_fontsize=14, counts_fontsize=12, sci_notation_limit=999)[source]¶ This function is a wrapper around matplotlib_venn. It most just makes setting the fonts and and label formatting a bit easier.
Parameters: - sets – either a dictionary, a list-like of two sets or a list-like of three sets. If a dictionary, it must follow the conventions of matplotlib_venn. If a dictionary is given, the number of sets will be guessed based on the length of a random key.
- ax (mpl.axis) – an axis for drawing
- set_labels (list of strings) – the label for each set. The order of the labels must match the order of the sets
- weighted (bool) – whether to draw a weighted or unweighted diagram
- use_sci_notation (bool) – whether to convert numbers to scientific notation
- sci_notation_limit (float) – the maximum number to show before switching to scientific notation
- counts_fontsize (labels_fontsize,) – the respective fontsizes
Returns: the diagram
Return type: matplotlib_venn.VennDiagram
- Imports:
- matplotlib_venn
-
pyllars.mpl_utils.
remove_top_and_right_splines
(ax)[source]¶ This function removes the spines on the top and right of the axis.
Parameters: ax (mpl.Axis) – The axis Returns: Return type: None, but the splines and ticks of the axis are updated
-
pyllars.mpl_utils.
set_label_fontsize
(ax, fontsize, axis='both')[source]¶ Set the font size of the label of the axis.
Parameters: - ax (mpl.Axis) – The axis
- fontsize (int, or string mpl recognizes) – The size of the title font
- which (string) – Should be ‘both’, ‘x’, or ‘y’
Returns: Return type: None, but the respective label fontsizes are updated
-
pyllars.mpl_utils.
set_legend_fontsize
(ax, fontsize)[source]¶ Set the font size of the items of the legend.
Parameters: - ax (mpl.Axis) – The axis
- fontsize (int, or string mpl recognizes) – The size of the legend text
Returns: Return type: None, but the legend text fontsize is updated
-
pyllars.mpl_utils.
set_legend_title_fontsize
(ax, fontsize)[source]¶ Set the font size of the title of the legend.
Parameters: - ax (mpl.Axis) – The axis
- fontsize (int, or string mpl recognizes) – The size of the legend title
Returns: Return type: None, but the legend title fontsize is updated
-
pyllars.mpl_utils.
set_ticklabel_rotation
(ax, rotation, axis='x', which='both')[source]¶ Set the rotation of the tick labels.
Parameters: - ax (mpl.Axis) – The axis
- rotation (int, or a string mpl recognizes) – The rotation of the labels
- axis ('x', 'y', 'both') – The axis whose tick labels will be rotated
- which ('major', 'minor', 'both') – Which of the tick labels to affect
Returns: Return type: None, but the ticklabels are rotated
-
pyllars.mpl_utils.
set_ticklabels_fontsize
(ax, fontsize, axis='both', which='major')[source]¶ Set the font size of the tick labels.
Parameters: - ax (mpl.Axis) – The axis
- fontsize (int, or string mpl recognizes) – The size of the ticklabels
- which (axis,) – Values passed to ax.tick_params. Please see the mpl documentation for more details.
Returns: Return type: None, but the ticklabel fontsizes are updated