Skip to content

Plotter

ScanPyImports.plotter

A module for generating visual representations of data using spiral plots and word clouds.

Classes:

  • PlotSettings

    Manages plot settings, including defaults for spiral plots and WordCloud generation.

  • Spiral

    Generates spiral bar plots using given settings.

  • Cloud

    Generates word cloud plots using given settings.

  • DataPlotter

    Extends DataAnalyzer to provide methods for visualizing data on imported modules.

The module also includes utility functions to obtain the current file directory and load image masks.

Classes

PlotSettings()

A class to manage plot settings, including defaults for spiral plots and WordCloud generation.

Source code in ScanPyImports/plotter.py
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def __init__(self):
    """Initialize PlotSettings with default values."""
    self.fontpath: str = None
    """Path to the font file currently in use."""
    self.fontname: str = None
    """Name of the font."""
    self.fontfamily: str = None
    """Font family."""
    self.figsize: tuple[float,float] = (4.8,4.8)
    """Figure size."""

    # Only bar format
    self._spiral_defaults : dict = {
        'bottom': 30,
        'linewidth': 2,
        'edgecolor': 'white',
    }

    mask_path = os.path.join(current_file_dir(), "images", "mask.png")
    mask = mask_from_path(mask_path)
    self._cloud_defaults = {
        'background_color': None,
        'mode': "RGBA",
        'mask': mask,
        'font_path': None,
        'width': 1000,
        'height': 1000,
        'max_words': 200,
        'prefer_horizontal': 0.75,
        'repeat': True,
        'max_font_size': 100,
        'colormap': None
    }
Attributes
fontpath: str = None instance-attribute

Path to the font file currently in use.

fontname: str = None instance-attribute

Name of the font.

fontfamily: str = None instance-attribute

Font family.

figsize: tuple[float, float] = (4.8, 4.8) instance-attribute

Figure size.

spiral_defaults: dict property writable

Default parameters for bars in the spiral plot.

cloud_defaults: dict property writable

Default arguments for WordCloud generation.

Functions
set_font(name)

Set the font globally for plotting. This method updates the font settings for both WordCloud generation and Matplotlib plotting to use the specified font.

Parameters:

  • name (str) –

    Matplotlib font name, such as Arial, Verdana, etc.

Source code in ScanPyImports/plotter.py
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def set_font(self, name: str):
    """Set the font globally for plotting.
    This method updates the font settings for both WordCloud generation
    and Matplotlib plotting to use the specified font.

    Args:
        name (str): Matplotlib font name, such as `Arial`, `Verdana`, etc.
    """
    font_path = matplotlib.font_manager.findfont(name)
    prop = matplotlib.font_manager.FontProperties(fname=font_path)
    self.fontpath = font_path
    self.fontname = name
    self.fontfamily = prop.get_family()
    self.cloud_defaults = dict(font_path=font_path)

    # Change matplotlib globally
    matplotlib.rcParams[f'font.{self.fontfamily[0]}'] = self.fontname
    matplotlib.rcParams['font.family'] = self.fontfamily[0]
restore_font()

Restore default font from WordCloud and Matplotlib.

Source code in ScanPyImports/plotter.py
160
161
162
163
164
165
166
def restore_font(self):
    """Restore default font from WordCloud and Matplotlib."""
    self.fontpath = None
    self.fontname = None
    self.fontfamily = None
    self.cloud_defaults = dict(font_path=None)
    matplotlib.rcdefaults()

Spiral(settings=None)

A class to generate spiral bar plots using given settings.

Attributes:

Methods:

Parameters:

  • settings (Optional[PlotSettings], default: None ) –

    An instance of PlotSettings containing default plotting parameters.

Source code in ScanPyImports/plotter.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def __init__(self, settings: Optional[PlotSettings] = None):
    """
    Initialize a Spiral object.

    Args:
        settings: An instance of PlotSettings containing default plotting parameters.
    """
    if settings:
        self.settings = settings
    else:
        self.settings = PlotSettings()

    self.settings : PlotSettings  
    """A [PlotSettings][ScanPyImports.plotter.PlotSettings] instance."""
Attributes
settings: PlotSettings instance-attribute

A PlotSettings instance.

Functions
plot(labels=None, values=None, ax=None, zero_at='NE', label_padding=2, defaults=True, **kwargs)

Generate a spiral bar plot.

Parameters:

  • labels (Optional[List[str]], default: None ) –

    List of labels for the bars.

  • values (Optional[List[Union[float, int]]], default: None ) –

    List of values for the bars. Order values in ascending order for a spiral effect.

  • ax (Optional[PolarAxes], default: None ) –

    The axes to plot on. If None, a new figure and axes are created. The ax parameter must be an instance of PolarAxes.

  • zero_at (str, default: 'NE' ) –

    Zero location for theta. Default is 'NE', which means that the largest value will point North-East (NE). Other possible values: 'N', 'S', 'SE', 'NW', and 'SW'.

  • label_padding (int, default: 2 ) –

    Padding for the labels.

  • defaults (bool, default: True ) –

    Whether to use default settings from the class.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments for the bar plot to be passed to Matplotlib (see: Axes.bar). If there is a collision, kwargs will overwrite default settings.

Notes

The ax parameter, if passed, must be an instance of PolarAxes.

When creating ax, you should pass polar as the projection argument. Here are two ways to achieve this:

from matplotlib import pyplot as plt

# One way:
fig = plt.figure()
ax = fig.add_subplot(projection='polar')

# Another way:
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})

Returns:

Raises:

  • ValueError

    If labels and values are not provided or if their lengths do not match.

Source code in ScanPyImports/plotter.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def plot(self, labels: Optional[List[str]] = None,
         values: Optional[List[Union[float, int]]] = None,
         ax: Optional[PolarAxes] = None,
         zero_at: str = 'NE',
         label_padding: int = 2,
         defaults: bool = True,
         **kwargs: Any) -> Tuple[Figure, PolarAxes, BarContainer, List[Text]]:
    """
    Generate a spiral bar plot.

    Args:
        labels: List of labels for the bars.
        values: List of values for the bars. Order values in ascending order for a spiral effect.
        ax: The axes to plot on. If None, a new figure and axes are created. 
            The `ax` parameter must be an instance of PolarAxes.
        zero_at: Zero location for theta. Default is 'NE', which means that the largest value will point North-East (NE). 
            Other possible values: 'N', 'S', 'SE', 'NW', and 'SW'.
        label_padding: Padding for the labels.
        defaults: Whether to use default settings from the class.
        **kwargs: Additional keyword arguments for the bar plot to be passed to Matplotlib (see: [Axes.bar][matplotlib.axes.Axes.bar]).
            If there is a collision, `kwargs` will overwrite default settings.


    Notes:
        The `ax` parameter, if passed, must be an instance of [**PolarAxes**][matplotlib.projections.polar.PolarAxes].

        When creating `ax`, you should pass `polar` as the projection argument. Here are two ways to achieve this:

        ```python
        from matplotlib import pyplot as plt

        # One way:
        fig = plt.figure()
        ax = fig.add_subplot(projection='polar')

        # Another way:
        fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
        ```


    Returns:
        The figure, axes, bars, and texts of the plot.

    Raises:
        ValueError: If `labels` and `values` are not provided or if their lengths do not match.
    """
    if labels is None or values is None:
        raise ValueError("Both 'labels' and 'values' must be provided.")
    if len(labels) != len(values):
        raise ValueError("Length of 'labels' and 'values' must be the same.")

    if defaults:
        kwargs = {**self.settings.spiral_defaults, **kwargs}

    val_max = max(values)
    heights = [(val / val_max) * 120 + 5 for val in values]
    n = len(labels)
    width_bar = 2 * np.pi / n
    thetas = [bar * width_bar for bar in range(1, n + 1)]

    fig, ax = self.get_ax(ax)

    ax.set_theta_zero_location(zero_at)
    bars = ax.bar(x=thetas, height=heights, width=width_bar, **kwargs)

    texts = self.add_labels(ax, bars, labels, thetas, heights, label_padding)

    ax.set_axis_off()
    return fig, ax, bars, texts
add_labels(ax, bars, labels, thetas, heights, label_padding)

Helper function to add labels to the bars in the spiral plot.

Parameters:

  • ax (Axes) –

    The axes to add the labels to.

  • bars (BarContainer) –

    The bars of the spiral plot.

  • labels (List[str]) –

    List of labels for the bars.

  • thetas (List[float]) –

    List of theta values for the bars.

  • heights (List[float]) –

    List of heights for the bars.

  • label_padding (int) –

    Padding for the labels.

Returns:

  • List[Text]

    A list of Matplotlib text objects for the labels.

Source code in ScanPyImports/plotter.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def add_labels(self, ax: Axes, bars: BarContainer, labels: List[str],
                thetas: List[float], heights: List[float], label_padding: int) -> List[Text]:
    """
    Helper function to add labels to the bars in the spiral plot.

    Args:
        ax: The axes to add the labels to.
        bars: The bars of the spiral plot.
        labels: List of labels for the bars.
        thetas: List of theta values for the bars.
        heights: List of heights for the bars.
        label_padding: Padding for the labels.

    Returns:
        A list of Matplotlib text objects for the labels.
    """
    def theta_adj(theta):
        return (theta + ax.get_theta_offset()) % (np.pi * 2)

    def get_quadrant(theta):
        return theta // (np.pi / 2)

    def text_rot_theta(theta: float) -> float:
        quadrant = get_quadrant(theta_adj(theta))
        rotation = theta_adj(theta)
        if quadrant in [1, 2]:
            rotation += np.pi
        return rotation

    def text_rot_degree(theta: float) -> float:
        return np.rad2deg(text_rot_theta(theta))

    def text_ha(theta: float) -> str:
        return 'right' if get_quadrant(theta_adj(theta)) in [1, 2] else 'left'

    bottom = bars[0].xy[1]
    heights_pad = [y + label_padding + bottom for y in heights]

    texts = []
    for label, theta, y in zip(labels, thetas, heights_pad):
        text = ax.text(
            x=theta,
            y=y,
            s=label,
            ha=text_ha(theta),
            rotation=text_rot_degree(theta),
            va='center',
            rotation_mode="anchor"
        )
        texts.append(text)
    return texts
get_ax(ax=None)

Helper function to get or create a Matplotlib Figure and Axes.

Parameters:

Returns:

  • Tuple[Figure, PolarAxes]

    A tuple containing the Figure and Axes of the plot. If an Axes object is passed, the Axes and its figure will be returned. Otherwise, a new figure and Axes will be created.

Source code in ScanPyImports/plotter.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
def get_ax(self, ax: Optional[PolarAxes] = None) -> Tuple[Figure, PolarAxes]:
    """
    Helper function to get or create a Matplotlib Figure and Axes.

    Args:
        ax: An existing Matplotlib Axes object. 

    Returns:
        A tuple containing the Figure and Axes of the plot. If an Axes object is passed, the Axes and its figure will be returned. Otherwise, a new figure and Axes will be created.
    """
    if ax is None:
        fig = plt.figure(figsize=self.settings.figsize)
        ax = fig.add_subplot(projection='polar')
    else:
        fig = ax.get_figure()
    return fig, ax

Cloud(settings=None)

A class to generate word cloud plots using given settings.

Attributes:

Methods:

  • plot

    Generate a word cloud.

  • get_ax

    Helper function.

Parameters:

  • settings (Optional[PlotSettings], default: None ) –

    An instance of PlotSettings containing default plotting parameters.

Source code in ScanPyImports/plotter.py
346
347
348
349
350
351
352
353
354
355
356
357
358
359
def __init__(self, settings: Optional[PlotSettings] = None) -> None:
    """
    Initialize a Cloud object.

    Args:
        settings: An instance of PlotSettings containing default plotting parameters. 
    """
    if settings:
        self.settings = settings
    else:
        self.settings = PlotSettings()

    self.settings: PlotSettings
    """An instance of PlotSettings for default plotting parameters."""
Attributes
settings: PlotSettings instance-attribute

An instance of PlotSettings for default plotting parameters.

Functions
plot(data_dic, defaults=True, ax=None, imshow={}, **kwargs)

Generate a word cloud.

Parameters:

  • data_dic (Dict[str, Union[int, float]]) –

    Dictionary of word frequencies of label-value pairs.

  • defaults (bool, default: True ) –

    Whether to use default settings of the Class.

  • ax (Optional[Axes], default: None ) –

    The axes to plot on. If None, a new figure and axes are created.

  • imshow (Optional[Dict[str, Any]], default: {} ) –

    Additional arguments to pass to the Matplotlib imshow method (see: Axes.imshow).

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the WordCloud object (see: wordcloud.WordCloud)

Returns:

  • Tuple[Figure, Axes, WordCloud, AxesImage]

    A tuple containing the figure, axes, WordCloud object, and AxesImage object of the plot.

Source code in ScanPyImports/plotter.py
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
def plot(self, data_dic: Dict[str, Union[int, float]], 
         defaults: bool = True, 
         ax: Optional[Axes] = None, 
         imshow: Optional[Dict[str, Any]] = {}, 
         **kwargs: Any ) -> Tuple[Figure, Axes, WordCloud, AxesImage]:
    """
    Generate a word cloud.

    Args:
        data_dic: Dictionary of word frequencies of label-value pairs.
        defaults: Whether to use default settings of the Class.  
        ax: The axes to plot on. If None, a new figure and axes are created.
        imshow: Additional arguments to pass to the Matplotlib imshow method (see: [Axes.imshow][matplotlib.axes.Axes.imshow]).
        **kwargs: Additional keyword arguments passed to the WordCloud object (see: [wordcloud.WordCloud](https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.WordCloud))

    Returns:
        A tuple containing the figure, axes, WordCloud object, and AxesImage object of the plot.
    """
    if defaults:
        kwargs = {**self.settings.cloud_defaults, **kwargs}

    wc = WordCloud(**kwargs)
    wc.generate_from_frequencies(data_dic)

    fig, ax = self.get_ax(ax)
    if imshow:
        im = ax.imshow(wc, **imshow)
    else:
        im = ax.imshow(wc)    
    ax.set_axis_off()

    return fig, ax, wc, im
get_ax(ax=None)

Helper function to get or create a Matplotlib Figure and Axes object.

Parameters:

  • ax (Optional[Axes], default: None ) –

    An existing Matplotlib Axes object.

Returns:

  • Tuple[Figure, Axes]

    A tuple containing the figure and axes. If an Axes object is passed, the Axes and its figure will be returned. Otherwise, a new figure and Axes will be created.

Source code in ScanPyImports/plotter.py
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
def get_ax(self, ax: Optional[Axes] = None) -> Tuple[Figure, Axes]:
    """
    Helper function to get or create a Matplotlib Figure and Axes object.

    Args:
        ax: An existing Matplotlib Axes object. 

    Returns:
        A tuple containing the figure and axes. If an Axes object is passed, the Axes and its figure will be returned. Otherwise, a new figure and Axes will be created.
    """
    if ax is None:
        fig = plt.figure(figsize=self.settings.figsize)
        ax = fig.add_subplot()
    else:
        fig = ax.get_figure()
    return fig, ax

DataPlotter(path, to_exclude=None)

Bases: DataAnalyzer

DataPlotter extends DataAnalyzer to provide methods for visualizing data on imported modules.

Methods:

Parameters:

  • path (str) –

    Path to the directory.

  • to_exclude (List[str], default: None ) –

    List of packages' names to exclude from the analysis.

Source code in ScanPyImports/plotter.py
420
421
422
423
424
425
426
427
428
429
def __init__(self, path: str, to_exclude : List[str] = None):
    """Initiate DataPlotter.

    Args:
        path: Path to the directory.
        to_exclude: List of packages' names to exclude from the analysis.
    """
    super().__init__(path, to_exclude=to_exclude)
    self.settings = PlotSettings() # setting shared by all plots
    """An instance of PlotSettings containing default plotting parameters."""
Attributes
path: str instance-attribute

Path to the directory.

directory: Directory instance-attribute

An instance of Directory initiated with the given path.

df: Optional[pd.DataFrame] property

DataFrame containing import data or None if the directory does not exist.

DataFrame content

The DataFrame contains the following columns:

  • imported_0, imported_1, ... representing the imported packages and modules.
    • where imported_0 represents the top-level package or library being imported.
    • imported_1 represents the module or submodule within the package being imported.
    • and so on imported_2 represent further nested modules, submodules if present in the import statement.
  • original: The original line of text containing the import statement.
  • alias: Alias (if any) of the submodule.
  • path: Full path of the file containing the import.
  • file: File name.
  • filename: File name without extension.
  • extension: File extension.
  • directory: Directory path of the file.

The data creation takes place in this private method. One could modify this code to retreive a dictionary or a JSON file instead of a DataFrame.

to_exclude: List[str] instance-attribute

List of packages' names to exclude from the analysis.

clean_df: pd.DataFrame property

A cleaned copy of df after conducting some minor changes.

own_processed_df: pd.DataFrame property

A copy of the DataFrame (df) after processing own-created modules.

Own-created modules

Own-created modules are defined as Python scripts that are imported as modules and reside in the same folder as the script containing the import statement.

A natural extension would be to also include own-created packages residing in the same folder as the .py or .ipynb file where the import statment resides.

In the returned DataFrame, own-created modules are dropped and replaced by the import statements residing inside the own-created module script, provided they relate to external libraries.

settings = PlotSettings() instance-attribute

An instance of PlotSettings containing default plotting parameters.

Functions
get_frequencies(exclude=True, process_own_modules=True)

Get the frequency of imported modules.

Parameters:

  • exclude (bool, default: True ) –

    Whether to exclude the packages listed in to_exclude.

  • process_own_modules (bool, default: True ) –

    Whether to process own-created modules.

Returns:

  • Series

    pd.Series: Series of import frequencies sorted in descending order.

Source code in ScanPyImports/analyzer.py
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
def get_frequencies(self, exclude: bool = True, process_own_modules: bool = True) -> pd.Series:
    """
    Get the frequency of imported modules.

    Args:
        exclude: Whether to exclude the packages listed in [to_exclude][ScanPyImports.analyzer.DataAnalyzer.to_exclude].
        process_own_modules: Whether to process own-created modules.

    Returns:
        pd.Series: Series of import frequencies sorted in descending order.
    """
    df = self.own_processed_df if process_own_modules else self.clean_df
    count_series = (df.groupby('imported_0')
                    .size()
                    .sort_values(ascending=False))

    count_series.index.name = 'Imports'

    if exclude:
        count_series = count_series[~count_series.index.isin(self.to_exclude)]

    return count_series
cloud_frequencies(exclude=True, process_own_modules=True, defaults=True, ax=None, imshow=None, **kwargs)

Generate a word cloud plot of data frequencies.

Parameters:

  • exclude (bool, default: True ) –

    Whether to exclude the packages listed in to_exclude.

  • process_own_modules (bool, default: True ) –

    Whether to process own-created modules.

  • defaults (bool, default: True ) –

    Whether to use default settings defined in settings.

  • ax (Optional[Axes], default: None ) –

    The axes to plot on. If None, a new figure and axes are created.

  • imshow (Optional[Dict[str, Any]], default: None ) –

    Additional arguments to pass to the Matplotlib imshow method (see: Axes.imshow).

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the WordCloud object (see: wordcloud.WordCloud)

Returns:

Source code in ScanPyImports/plotter.py
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
def cloud_frequencies(self
                      , exclude: bool = True
                      , process_own_modules: bool = True
                      , defaults: bool = True
                      , ax: Optional[Axes] = None
                      , imshow: Optional[Dict[str, Any]] = None
                      , **kwargs: Any) -> Tuple[Figure, Axes, WordCloud, AxesImage]: 
    """Generate a word cloud plot of data frequencies.

    Args:
        exclude: Whether to exclude the packages listed in [to_exclude][ScanPyImports.plotter.DataPlotter.to_exclude].
        process_own_modules: Whether to process own-created modules.
        defaults: Whether to use default settings defined in [settings][ScanPyImports.plotter.DataPlotter.settings].
        ax: The axes to plot on. If None, a new figure and axes are created.
        imshow: Additional arguments to pass to the Matplotlib imshow method (see: [Axes.imshow][matplotlib.axes.Axes.imshow]).
        **kwargs: Additional keyword arguments passed to the WordCloud object (see: [wordcloud.WordCloud](https://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.html#wordcloud.WordCloud))

    Returns:
        The figure, axe , WordCloud object, and image axe.
    """
    s = self.get_frequencies(exclude=exclude,
                             process_own_modules=process_own_modules)

    cloud_plot = Cloud(self.settings)
    fig, ax, wc, im = cloud_plot.plot(data_dic=dict(s), defaults=defaults,
                           ax=ax, imshow=imshow, **kwargs)
    return fig, ax, wc, im 
spiral_frequencies(exclude=True, process_own_modules=True, top=25, ax=None, zero_at='NE', defaults=True, label_padding=2, **kwargs)

Generate a spiral bar plot based on data frequencies.

Parameters:

  • exclude (bool, default: True ) –

    Whether to exclude the packages listed in to_exclude.

  • process_own_modules (bool, default: True ) –

    Whether to process own-created modules.

  • defaults (bool, default: True ) –

    Whether to use default settings defined in settings.

  • ax (Optional[Axes], default: None ) –

    The axes to plot on. If None, a new figure and axes are created.

  • top (Optional[int], default: 25 ) –

    Number of top most frequent modules to include. If None, all modules are included.

  • zero_at (str, default: 'NE' ) –

    Zero location for theta. Default is 'NE', which means that the largest value will point North-East (NE). Other possible values: 'N', 'S', 'SE', 'NW', and 'SW'.

  • label_padding (int, default: 2 ) –

    Padding for the labels.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments for the bar plot (see: Axes.bar)

Notes

The ax parameter, if passed, must be an instance of PolarAxes.

When creating ax, you should pass polar as the projection argument. Here are two ways to achieve this:

from matplotlib import pyplot as plt

# One way:
fig = plt.figure()
ax = fig.add_subplot(projection='polar')

# Another way:
fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})

Returns:

Source code in ScanPyImports/plotter.py
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
def spiral_frequencies(self, exclude: bool = True
                       , process_own_modules: bool = True
                       , top: Optional[int] = 25
                       , ax: Optional[Axes] = None
                       , zero_at: str = 'NE'
                       , defaults: bool = True
                       , label_padding: int = 2
                       , **kwargs: Any) -> Tuple[Figure, Axes, BarContainer, List[Text]]:
    """ 
    Generate a spiral bar plot based on data frequencies. 

    Args:
        exclude: Whether to exclude the packages listed in [to_exclude][ScanPyImports.plotter.DataPlotter.to_exclude].
        process_own_modules: Whether to process own-created modules.
        defaults: Whether to use default settings defined in [settings][ScanPyImports.plotter.DataPlotter.settings].
        ax: The axes to plot on. If None, a new figure and axes are created.
        top: Number of top most frequent modules to include. If None, all modules are included.
        zero_at: Zero location for theta. Default is 'NE', which means that the largest value will point North-East (NE). 
            Other possible values: 'N', 'S', 'SE', 'NW', and 'SW'.
        label_padding: Padding for the labels.
        **kwargs: Additional keyword arguments for the bar plot (see: [Axes.bar][matplotlib.axes.Axes.bar])

    Notes:
        The `ax` parameter, if passed, must be an instance of [**PolarAxes**][matplotlib.projections.polar.PolarAxes].

        When creating `ax`, you should pass `polar` as the projection argument. Here are two ways to achieve this:

        ```python
        from matplotlib import pyplot as plt

        # One way:
        fig = plt.figure()
        ax = fig.add_subplot(projection='polar')

        # Another way:
        fig, ax = plt.subplots(subplot_kw={'projection': 'polar'})
        ```


    Returns:
        The figure, axes, bars, and texts of the plot.
    """
    s = self.get_frequencies(exclude=exclude,
                            process_own_modules=process_own_modules)

    if top is not None:
        s = s[:top]

    s = s.sort_values()
    values = list(s.values)
    labels = list(s.index)

    spiral = Spiral(self.settings)
    fig, ax, bars, texts = spiral.plot(labels=labels, values=values,
                                        ax=ax, zero_at=zero_at, defaults=defaults,label_padding=label_padding, **kwargs)

    return fig, ax, bars, texts 

Functions

current_file_dir()

Get the directory containing the current file.

Source code in ScanPyImports/plotter.py
32
33
34
35
36
37
def current_file_dir() -> str:
    """Get the directory containing the current file."""
    try:
        return os.path.dirname(os.path.abspath(__file__))
    except:
        return os.path.dirname(os.path.abspath('__file__'))

mask_from_path(path)

Loads an image from the given path and returns it as a NumPy array.

Source code in ScanPyImports/plotter.py
39
40
41
42
def mask_from_path(path: str) -> np.ndarray:
    """Loads an image from the given path and returns it as a NumPy array."""
    mask = np.array(Image.open(path))
    return mask

get_fontname_list()

Get a list of available font names.

Source code in ScanPyImports/plotter.py
44
45
46
def get_fontname_list():
    """Get a list of available font names."""
    return sorted(matplotlib.font_manager.get_font_names())

color_list(n=3, colormap='Blues', lower=0.3, upper=1.0, hex_colors=True, reversed_cmap=False)

List of color codes based on the specified Matplotlib colormap.

Parameters:

  • n (int, default: 3 ) –

    The number of color codes to be generated.

  • colormap (str, default: 'Blues' ) –

    The name of the colormap to be used.

  • lower (float, default: 0.3 ) –

    The lower bound of the color range to be selected from the colormap.

  • upper (float, default: 1.0 ) –

    The upper bound of the color range to be selected from the colormap.

  • hex_colors (bool, default: True ) –

    If True, the color codes will be returned in hexadecimal format otherwise colors are in RGBA format.

  • reversed_cmap (bool, default: False ) –

    List colors in reverse order.

Returns:

  • list

    A list of n color codes in either hexadecimal or RGBA format.

Source code in ScanPyImports/plotter.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def color_list(n : int = 3, colormap : str = 'Blues' , lower : float = 0.3,  upper : float = 1.0 , hex_colors : bool = True , reversed_cmap : bool = False) -> list :  
    """List of color codes based on the specified Matplotlib colormap. 

    Args:
        n: The number of color codes to be generated. 
        colormap: The name of the colormap to be used.
        lower: The lower bound of the color range to be selected from the colormap.
        upper: The upper bound of the color range to be selected from the colormap.
        hex_colors: If True, the color codes will be returned in hexadecimal format otherwise colors are in RGBA format.
        reversed_cmap: List colors in reverse order.

    Returns:
        A list of `n` color codes in either hexadecimal or RGBA format.
    """
    if colormap:
        # Get matplotlib colormap 
        colormap = matplotlib.colormaps[colormap]

        # Select colors list
        colors =  colormap(np.linspace(lower, upper, n ))

        # If reversed colors 
        if reversed_cmap :
            colors = colors[::-1]

        if hex_colors :
            # Convert color codes to hexadecimal format
            colors = [matplotlib.colors.to_hex(c) for c in colors]

        return colors