adataviz.utils module

class adataviz.utils.TextWithCircleHandler(marker_text='', label_text='', text_kws={}, **kwargs)[source]

Bases: HandlerBase

create_artists(legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans)[source]

Return the legend artists generated.

Parameters:
  • legend (~matplotlib.legend.Legend) – The legend for which these legend artists are being created.

  • orig_handle (~matplotlib.artist.Artist or similar) – The object for which these legend artists are being created.

  • xdescent (int) – The rectangle (xdescent, ydescent, width, height) that the legend artists being created should fit within.

  • ydescent (int) – The rectangle (xdescent, ydescent, width, height) that the legend artists being created should fit within.

  • width (int) – The rectangle (xdescent, ydescent, width, height) that the legend artists being created should fit within.

  • height (int) – The rectangle (xdescent, ydescent, width, height) that the legend artists being created should fit within.

  • fontsize (int) – The fontsize in pixels. The legend artists being created should be scaled according to the given fontsize.

  • trans (~matplotlib.transforms.Transform) – The transform that is applied to the legend artists being created. Typically from unit coordinates in the handler box to screen coordinates.

adataviz.utils.categorical_scatter(data, ax=None, basis='umap', x=None, y=None, hue=None, palette='auto', color=None, text_anno=None, text_kws=None, luminance=None, text_transform=None, dodge_text=False, dodge_kws=None, show_legend=False, legend_kws=None, s='auto', size=None, sizes=None, size_norm=None, size_portion=0.95, axis_format='tiny', max_points=50000, labelsize=4, linewidth=0.5, zoomxy=1.05, outline=None, outline_pad=3, alpha=0.7, outline_kws=None, scatter_kws=None, rasterized='auto', coding=False, coded_marker=True, legend_color_text=True, rectangle_marker=False, marker_fontsize=4, marker_pad=0.1)[source]

This function was copied from ALLCools and made some modifications. Plot categorical scatter plot with versatile options.

Parameters:
  • rasterized – Whether to rasterize the figure.

  • return_fig – Whether to return the figure.

  • size_portion – The portion of the figure to be used for the size norm.

  • data – Dataframe that contains coordinates and categorical variables

  • ax – this function do not generate ax, must provide an ax

  • basis – coords name, if provided, will automatically search for x and y

  • x – x coord name

  • y – y coord name

  • hue (str) – categorical col name or series for color hue.

  • palette (str or dict) – palette for color hue.

  • color – specify single color for all the dots

  • text_anno – categorical col name or series for text annotation.

  • text_kws

    kwargs pass to plt.text, see: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html including bbox, to see parameter for bbox, go to: https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.FancyBboxPatch.html#matplotlib.patches.FancyBboxPatch commonly used parameters are:

    text_kws=dict(fontsize=5,fontweight='black',
                            color='black', # color could be a dict, keys are text to be annotated
                            bbox=dict(boxstyle='round',edgecolor=(0.5, 0.5, 0.5, 0.2),fill=False,
                                                    facecolor=(0.8, 0.8, 0.8, 0.2), # facecolor could also be a dict
                                                    alpha=1,linewidth=0.5)
                            )
    

  • text_transform – transform for text annotation.

  • dodge_text – whether to dodge text annotation.

  • dodge_kws – kwargs for dodge text annotation.

  • show_legend – whether to show legend.

  • legend_kws – kwargs for legend.

  • s – single size value of all the dots.

  • size – mappable size of the dots.

  • sizes – mapping size to the sizes value.

  • size_norm – normalize size range for mapping.

  • axis_format – axis format.

  • max_points – maximum number of points to plot.

  • labelsize – label size pass to ax.text

  • linewidth – line width pass to ax.scatter

  • zoomxy – zoom factor for x and y-axis.

  • outline – categorical col name or series for outline.

  • outline_pad – outline padding.

  • outline_kws – kwargs for outline.

  • scatter_kws – kwargs for scatter.

Returns:

  • if return_fig is True, return the figure and axes.

  • else, return None.

adataviz.utils.continuous_scatter(data, ax=None, basis='umap', x=None, y=None, scatter_kws=None, hue=None, hue_norm=None, hue_portion=0.95, color=None, cmap='viridis', colorbar=True, size=None, size_norm=None, size_portion=0.95, sizes=None, sizebar=True, text_anno=None, dodge_text=False, dodge_kws=None, text_kws=None, luminance=0.48, text_transform=None, axis_format='tiny', max_points=50000, s='auto', labelsize=6, ticklabel_size=4, linewidth=0.5, zoomxy=1.05, outline=None, outline_kws=None, outline_pad=2, return_fig=False, rasterized='auto', cbar_kws=None, cbar_width=3)[source]

Plot scatter on given adata.

Parameters:
  • data (_type_) – _description_

  • ax (_type_, optional) – _description_, by default None

  • basis (str, optional) – _description_, by default “umap”

  • x (_type_, optional) – _description_, by default None

  • y (_type_, optional) – _description_, by default None

  • scatter_kws (_type_, optional) – _description_, by default None

  • hue (_type_, optional) – _description_, by default None

  • hue_norm (_type_, optional) – _description_, by default None

  • hue_portion (float, optional) – _description_, by default 0.95

  • color (_type_, optional) – _description_, by default None

  • cmap (str, optional) – _description_, by default “viridis”

  • colorbar (bool, optional) – _description_, by default True

  • size (_type_, optional) – _description_, by default None

  • size_norm (_type_, optional) – _description_, by default None

  • size_portion (float, optional) – _description_, by default 0.95

  • sizes (_type_, optional) – _description_, by default None

  • sizebar (bool, optional) – _description_, by default True

  • text_anno (_type_, optional) – _description_, by default None

  • dodge_text (bool, optional) – _description_, by default False

  • dodge_kws (_type_, optional) – _description_, by default None

  • text_kws (_type_, optional) – _description_, by default None

  • luminance (float, optional) – _description_, by default 0.48

  • text_transform (_type_, optional) – _description_, by default None

  • axis_format (str, optional) – _description_, by default “tiny”

  • max_points (int, optional) – _description_, by default 50000

  • s (str, optional) – _description_, by default “auto”

  • labelsize (int, optional) – _description_, by default 6

  • ticklabel_size (int, optional) – _description_, by default 4

  • linewidth (float, optional) – _description_, by default 0.5

  • zoomxy (float, optional) – _description_, by default 1.05

  • outline (_type_, optional) – _description_, by default None

  • outline_kws (_type_, optional) – _description_, by default None

  • outline_pad (int, optional) – _description_, by default 2

  • return_fig (bool, optional) – _description_, by default False

  • rasterized (str, optional) – _description_, by default “auto”

  • cbar_kws (_type_, optional) – _description_, by default None

  • cbar_width (int, optional) – width of colorbar, by default 3 mm

Returns:

_description_

Return type:

_type_

Raises:
  • ValueError – _description_

  • TypeError – _description_

adataviz.utils.density_contour(ax, data, x, y, groupby=None, c='lightgray', single_contour_pad=1, linewidth=1, palette=None)[source]
adataviz.utils.despine(fig=None, ax=None, top=True, right=True, left=False, bottom=False)[source]

Remove the top and right spines from plot(s).

Parameters:
  • fig (matplotlib figure, optional) – Figure to despine all axes of, defaults to the current figure.

  • ax (matplotlib axes, optional) – Specific axes object to despine. Ignored if fig is provided.

  • top (boolean, optional) – If True, remove that spine.

  • right (boolean, optional) – If True, remove that spine.

  • left (boolean, optional) – If True, remove that spine.

  • bottom (boolean, optional) – If True, remove that spine.

Return type:

None

adataviz.utils.df2stdout(df)[source]
adataviz.utils.get_brain_region_structure()[source]

https://atlas.brain-map.org/ BICAN: https://atlas.brain-map.org/atlasviewer/ontologies/11.json HBA: https://atlas.brain-map.org/atlasviewer/ontologies/7.json

adataviz.utils.get_cmap(cmap)[source]
adataviz.utils.level_one_palette(name_list, order=None, palette='auto')[source]
adataviz.utils.mpl_style()[source]
adataviz.utils.normalize_mc_by_cell(use_adata, normalize_per_cell=True, clip_norm_value=10, verbose=1, hypo_score=False)[source]
adataviz.utils.parse_gtf(gtf='gencode.v43.annotation.gtf', outfile=None)[source]
adataviz.utils.parse_json(url)[source]
adataviz.utils.plot_cmap_legend(cax=None, ax=None, cmap='turbo', label=None, kws=None, labelsize=6, linewidth=0.5, ticklabel_size=4)[source]

Plot legend for cmap.

Parameters:
  • cax (Axes into which the colorbar will be drawn.)

  • ax (axes to anchor.)

  • cmap (turbo, hsv, Set1, Dark2, Paired, Accent,tab20,exp1,exp2,meth1,meth2)

  • label (title for legend.)

  • kws (dict) – kws passed to plt.colorbar (matplotlib.figure.Figure.colorbar).

Returns:

cbar

Return type:

axes of legend

adataviz.utils.plot_color_dict_legend(D, ax=None, title=None, color_text=True, kws=None, luminance=0.5)[source]

plot legned for color dict

Parameters:
  • D (a dict, key is categorical variable, values are colors.)

  • ax (axes to plot the legend.)

  • title (title of legend.)

  • color_text (whether to change the color of text based on the color in D.)

  • label_side (right of left.)

  • kws (kws passed to plt.legend.)

Return type:

ax.legend

adataviz.utils.plot_marker_legend(color_dict=None, ax=None, title=None, color_text=True, marker='o', kws=None, luminance=0.5)[source]

plot legned for different marker

Parameters:
  • D (a dict, key is categorical variable, values are marker.)

  • ax (axes to plot the legend.)

  • title (title of legend.)

  • color_text (whether to change the color of text based on the color in D.)

  • label_side (right of left.)

  • kws (kws passed to plt.legend.)

Return type:

ax.legend

adataviz.utils.plot_text_legend(color_dict, code2label, ax=None, title=None, color_text=True, boxstyle='Circle', marker_pad=0.1, legend_kws=None, marker_fontsize=4, text_kws=None, alpha=0.7, luminance=0.5)[source]
adataviz.utils.prepare_color_palette(color_dict=None, outpath='palette.xlsx')[source]

Generating a .xlsx file including all color palette.

Parameters:

colors (dict) – A dict of dict, keys are categorical terms, values are HEX color code

adataviz.utils.read_google_sheet(url=None, **kwargs)[source]
adataviz.utils.serialize(x)[source]
adataviz.utils.tight_hue_range(hue_data, portion)[source]

Automatic select a SMALLEST data range that covers [portion] of the data.

adataviz.utils.zoom_ax(ax, zoom_scale, on='both')[source]

Zoom ax on both x and y-axis.

adataviz.utils.zoom_min_max(vmin, vmax, scale)[source]

Zoom min and max value.