Basics
There are several ways to customize your visualizations:
Besides modifying the parameters as shown here, you can:
- Work with Returned Values after creating your visualizations.
- Pass Keyword Arguments (
kwargs
) when calling methods such as spiral_frequencies, cloud_frequencies, and plot. - Adjust Settings.
Working with Returned Values¶
The methods spiral_frequencies, cloud_frequencies and plot return the matplotlib figure and axes, which can be adjusted after the creation of the plot.
In addition, for spiral plots, the bars and text objects relating to the labels of the bars are returned.
Let's add a title and change the color of the labels of the bars for a Spiral plot.
from ScanPyImports.plotter import DataPlotter
import matplotlib.pyplot as plt
Generate the spiral plot.
plotter = DataPlotter('../../ScanPyImports')
# Get the figure, axes, bars, and texts from the spiral frequencies plot
fig, ax, bars, texts = plotter.spiral_frequencies()
# If you prefer, you can use the Spiral class instead
# fig, ax, bars, texts = spiral.plot(labels, values)
Work with the returned ax
, bars
and texts
.
# Get the color of the bars
color = bars[0].get_facecolor()
# Add a title to the plot
title = ax.set_title('ScanPyImports',
color='white',
weight='bold',
loc='left',
y=0.9, x=0.1,
bbox=dict(boxstyle='round',
edgecolor=color,
facecolor=color),
fontsize='xx-large'
)
# Change the color of the bar labels
for text in texts:
text.set_color(color)
# Return the figure
fig
Keyword Arguments (**kwargs
)¶
_ = plotter.spiral_frequencies(facecolor = 'red')
_ = plotter.cloud_frequencies(colormap = 'Dark2')
Adjusting Settings¶
Settings control default values and parameters affecting the plots.
Fonts¶
Changing the fonts through settings will affect the font of any new plot, even for plots using the Matplotlib API directly.
Let's change the font. If your computer has the font Impact, you can try the following:
plotter.settings.set_font('impact')
_ = plotter.cloud_frequencies()
Otherwise, you can check which fonts are available on your computer.
from ScanPyImports.plotter import get_fontname_list
get_fontname_list()[:5]
['Agency FB', 'Algerian', 'Arial', 'Arial Rounded MT Bold', 'Bahnschrift']
Finally, you can always restore the original font using restore_font()
.
plotter.settings.restore_font()
# Get the default settings for the spiral plot
defaults = plotter.settings.spiral_defaults
for param, value in defaults.items():
print(f'{param:<10}: {value}')
bottom : 30 linewidth : 2 edgecolor : white
Cloud defaults¶
# Get the default settings for the cloud plot
defaults = plotter.settings.cloud_defaults
# Print each parameter
for param, value in defaults.items():
if param != 'mask':
print(f'{param:<20}: {value}')
else:
# But mask it's a large numpy array
print(f'{param:<20}: a numpy array (too long to show)')
background_color : None mode : RGBA mask : a numpy array (too long to show) font_path : None width : 1000 height : 1000 max_words : 200 prefer_horizontal : 0.75 repeat : True max_font_size : 100 colormap : None
Modifying Default Settings¶
You can add or modify default values. Just ensure that the arguments are accepted by the bar plot method in matplotlib (for the Spiral defaults) or by the WordCloud object (for the Cloud defaults).
As an example, let's add the facecolor
and change the linewidth
of the bars.
# Modify the default settings
plotter.settings.spiral_defaults = dict(facecolor='cyan', linewidth=5)
# Check the updated default settings
defaults = plotter.settings.spiral_defaults
for param, value in defaults.items():
print(f'{param:<10}: {value}')
bottom : 30 linewidth : 5 edgecolor : white facecolor : cyan
_ = plotter.spiral_frequencies()
Explicitly passing a **kwargs
argument at call time takes precedence over the defaults!
# Bars will be orange and not cyan as specified in the defaults!
_ = plotter.spiral_frequencies(facecolor='orange')