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 |
|
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 |
|
restore_font()
¶
Restore default font from WordCloud and Matplotlib.
Source code in ScanPyImports/plotter.py
160 161 162 163 164 165 166 |
|
Spiral(settings=None)
¶
A class to generate spiral bar plots using given settings.
Attributes:
-
settings
(PlotSettings
) –An instance of PlotSettings containing default plotting parameters.
Methods:
-
plot
–Generate a spiral bar plot.
-
add_labels
–Helper function.
-
get_ax
–Helper function.
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 |
|
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
andvalues
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 |
|
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:
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 |
|
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 |
|
Cloud(settings=None)
¶
A class to generate word cloud plots using given settings.
Attributes:
-
settings
(PlotSettings
) –An instance of PlotSettings containing default plotting parameters.
Methods:
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 |
|
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 |
|
get_ax(ax=None)
¶
Helper function to get or create a Matplotlib Figure and Axes object.
Parameters:
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 |
|
DataPlotter(path, to_exclude=None)
¶
Bases: DataAnalyzer
DataPlotter extends DataAnalyzer to provide methods for visualizing data on imported modules.
Methods:
-
get_frequencies
–Return frequency of imported modules.
-
cloud_frequencies
–Generate a word cloud plot of data frequencies.
-
spiral_frequencies
–Generate a spiral bar plot based on data frequencies.
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 |
|
Attributes¶
path: str
instance-attribute
¶
Path to the directory.
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.
- where
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.
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 |
|
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 |
|
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 |
|
Functions¶
current_file_dir()
¶
Get the directory containing the current file.
Source code in ScanPyImports/plotter.py
32 33 34 35 36 37 |
|
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 |
|
get_fontname_list()
¶
Get a list of available font names.
Source code in ScanPyImports/plotter.py
44 45 46 |
|
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 |
|