prettypyplot
Prettypyplot¶
This package provides helper functions to easen the usage of matplotlib.
The module is structured into the following submodules:
-
pyplot: This submodule contains all methods related to plotting inside a single axes, so basically related to matplotlib.pyplot.
-
style: This module provides only method to load and alter the current style.
-
subplots: This module provides methods to simplify dealing with matplotlib.pyplot.subplots grids.
-
texts: This module provides methods to plot text with the possibility to add a contour.
-
tools: This module provides utility methods to.
GrayTones
¶
Bases: namedtuple('GrayTones', 'dark light')
Class for holding light and dark gray tone.
is_number(number, *, dtype=float)
¶
Check if argument can be interpreated as number.
Parameters:
-
number
((string, float, int)
) –Variable to be check if it can be casted to float.
-
dtype
(dtype
, default:float
) –Check for different dtypes, so if is float or if it is int.
Returns:
-
is_number
(bool
) –Return if argument can be casted to float.
Source code in src/prettypyplot/tools.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
load_cmaps()
¶
Load and include custom colormaps to matplotlib.
Add sequential colormaps pastel5
, pastel6
, cbf4
, cbf5
, cbf8
,
and ufcd
as an corporate design. Except of ufcd
all palettes should be
'color-blind-friendly'.
Add continuous colormaps macaw, Turbo. The Copyright of those are given on top of the data.
See
Choosing an cmaps.
Source code in src/prettypyplot/colors.py
71 72 73 74 75 76 77 78 79 80 81 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 115 |
|
load_colors()
¶
Load and include custom colors to matplotlib.
Add colors of pastel5
which can be accessed via pplt:blue
, pplt:red
,
pplt:green
, pplt:orange
, pplt:lightblue
, pplt:gray
and
pplt:lightgray
. Further, the current colors will be added pplt:axes
,
pplt:text
, pplt:grid
.
See
Choosing an cmaps.
Source code in src/prettypyplot/colors.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
categorical_cmap(nc, nsc, *, cmap=None, return_colors=False)
¶
Generate categorical colors of given cmap.
Exract from a predefined colormap colors and generate for each the desired number of shades.
Parameters:
-
nc
(int
) –Number of colors
-
nsc
(int
) –Number of shades per colors
-
cmap
(`matplotlib.colors.Colormap` or str
, default:None
) –Matplotlib colormap to take colors from. The default is the active color cycle.
-
return_colors
(bool
, default:False
) –Return an array of rgb colors. Each color together with its shades are in an own row.
Returns:
-
scolors
(`matplotlib.colors.Colormap` or np.ndarray
) –Return discrete colormap. If return_colors, a 2d representation will be returned instead.
Source code in src/prettypyplot/colors.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
categorical_color(nsc, color, *, return_hex=False)
¶
Generate categorical shades of given colors.
Generate for each provided color the number of specified shades. The shaded colors are interpolated linearly in HSV colorspace. This function is based on following post: https://stackoverflow.com/a/47232942
Parameters:
-
nsc
(int
) –Number of shades per color.
-
color
(RGB color or matplotlib predefined color
) –Color used for generating shades.
-
return_hex
(bool
, default:False
) –Return colors in hex format instead of rgb.
Returns:
-
colors_rgb
(list of RGB colors
) –A list containing shaded colors. Where the list is sorted from the original color at the beginning to the most shaded one at the end. The default color encoding is rgb and hex if specified.
Source code in src/prettypyplot/colors.py
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 |
|
text_color(bgcolor, colors=('#000000', '#ffffff'))
¶
Select textcolor with maximal contrast on background.
All parameters needs to be colors accepted by matplotlib, see matplotlib.colors. The formulas are taken from W3C WCAG 2.1 (Web Content Accessibility Guidelines).
Parameters:
-
bgcolor
(matplotlib color
) –Background color to which the contrast is maximized.
-
colors
(list of matplotlib colors
, default:('#000000', '#ffffff')
) –Selection of textcolors to choose from.
Returns:
-
color
(matplotlib color
) –Color of colors which has the highest contrast on the given bgcolor.
Source code in src/prettypyplot/colors.py
256 257 258 259 260 261 262 263 264 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 |
|
is_greyshade(color)
¶
Check if color is a greyscale value including bw.
Source code in src/prettypyplot/colors.py
336 337 338 339 340 341 342 |
|
update_style(interactive=None, colors=None, cmap=None, ncs=None, figsize=None, figratio=None, mode=None, style=None, ipython=None, true_black=None, latex=None, sf=None)
¶
Update alternative matplotlib style.
This function updates specified parameters of use_style
without changing
other.
Parameters:
-
interactive
(bool
, default:None
) –Set interactive mode.
-
colors
(string
, default:None
) –Set the default color cycler from continuous or discrete maps. Use any of matplotlibs defaults or specified in the colors submodule.
-
cmap
(string
, default:None
) –Set the default colormap.
-
ncs
(int
, default:None
) –Number of colors if continuous cmap is selected.
-
figsize
(int or int tuple
, default:None
) –Give size of default figure in inches, either as tuple (x, y) or a single float for the x-axis. The y-axis will be determined by figratio.
-
figratio
(str or float
, default:None
) –Set ratio of figsize x:y to 1:1/'option', where 'option' is one of
['sqrt(2)', 'golden', 'sqrt(3)']
or any number. Golden stands for the golden ratio (1.618). This option is ignored if figsize is used with tuple. -
mode
(str
, default:None
) –One of the following modes:
'default'
: use matplotlib defaults'beamer'
: extra large fontsize'print'
: default sizes'poster'
: for Din A0 posters -
style
(str
, default:None
) –One of the following styles:
'default'
: enables grid and upper and right spines'minimal'
: removes all unneeded lines'none'
: no changes to style -
ipython
(bool
, default:None
) –Deactivate high-res in jpg/png for compatibility with IPyhton, e.g. jupyter notebook/lab.
-
true_black
(bool
, default:None
) –If true black will be used for labels and co., else a dark grey.
-
latex
(bool
, default:None
) –If true LaTeX font will be used.
-
sf
(bool
, default:None
) –Use sans-serif font for text and latex math environment.
Source code in src/prettypyplot/style.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
|
use_style(interactive=None, colors='pastel5', cmap='macaw', ncs=10, figsize=(3,), figratio='golden', mode=_pplt.MODE, style=_pplt.STYLE, ipython=False, true_black=False, latex=True, sf=False)
¶
Define alternative matplotlib style.
This function restores first the matplolib default values and finally changes depicted values to achieve a more appealing appearence. It additionally loads pplts colormaps and colors in matplolib.
See update_style for parameters.
Source code in src/prettypyplot/style.py
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 |
|
add_contour(txt, contourwidth, contourcolor='w')
¶
Draw contour around txt.
Parameters:
-
txt
(mpl Text
) –Instance of matplotlib.text.Text. Can be obtained by, e.g.,
txt = plt.text()
ortxt = plt.figtext()
. -
contourwidth
(scalar
) –Width of contour.
-
contourcolor
(RGB color or matplotlib predefined color
, default:'w'
) –Color of contour, default is white.
Source code in src/prettypyplot/texts.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
figtext(x, y, s, *, contour=None, **kwargs)
¶
Generate text object at figure position (x,y).
Wrapper around pyplot.figtext. The default alignment is changed to centered.
Parameters:
-
x
(scalars
) –The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the
transform
parameter. -
y
(scalars
) –The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the
transform
parameter. -
s
(str
) –The text.
-
contour
(bool or tuple(scalar, color)
, default:None
) –Add a contour to the text. Either use a boolean for default values, or give a tuple with linewidth and linecolor.
-
ax
(matplotlib axes
) –Matplotlib axes to plot in.
-
kwargs
–Text properties of matplotlib.pyplot.figtext
Source code in src/prettypyplot/texts.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
text(x, y, s, *, contour=None, ax=None, **kwargs)
¶
Generate text object at (x,y).
Wrapper around pyplot.text. The default alignment is changed to centered.
Parameters:
-
x
(scalars
) –The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.
-
y
(scalars
) –The position to place the text. By default, this is in data coordinates. The coordinate system can be changed using the transform parameter.
-
s
(str
) –The text.
-
contour
(bool or tuple(scalar, color)
, default:None
) –Add a contour to the text. Either use a boolean for default values, or give a tuple with linewidth and linecolor.
-
ax
(matplotlib axes
, default:None
) –Matplotlib axes to plot in.
-
kwargs
–Text properties of matplotlib.pyplot.text
Source code in src/prettypyplot/texts.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
hide_empty_axes(axs=None)
¶
Hide empty axes.
Loop over all axes and hide empty ones.
Parameters:
-
axs
(mpl.axes.Axes or list of
, default:None
) –Specify matplotlib.axes.Axes to check for empty state. Default use all of current figure.
Source code in src/prettypyplot/subplots.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|
label_outer(axs=None)
¶
Only show outer labels and tick labels.
This checks for outest visible axes only. Works only with single Gridspec.
Parameters:
-
axs
(mpl.axes.AxesSubplot or list of
, default:None
) –Specify matplotlib.axes.Axes to check for labeling only outer. Default use all of current figure.
Source code in src/prettypyplot/subplots.py
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 78 79 80 81 82 83 84 85 |
|
subplot_labels(*, fig=None, xlabel=None, ylabel=None)
¶
Add global labels for subplots.
This method adds shared x- and y-labels for a grid of subplots. These can
be created by, e.g. fig, axs = plt.subplots(...)
.
Parameters:
-
fig
(matplotlib figure
, default:None
) –If
None
the current figure will be used instead. -
xlabel
(str
, default:None
) –String of x label.
-
ylabel
(str
, default:None
) –String of y label.
Source code in src/prettypyplot/subplots.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|