Environment.plotsettings module

plotsettings: use it like this, in the Environment folder:

>>> import sys; sys.path.insert(0, '..')
>>> from .plotsettings import BBOX_INCHES, signature, maximizeWindow, palette, makemarkers, add_percent_formatter, wraptext, wraplatex, legend, show_and_save, nrows_ncols
Environment.plotsettings.monthyear = 'Feb.2020'

Month.Year date

Environment.plotsettings.signature = ''

A small string to use as a signature

Environment.plotsettings.DPI = 120

DPI to use for the figures

Environment.plotsettings.FIGSIZE = (16, 9)

Figure size, in inches!

Environment.plotsettings.HLS = True

Use the HLS mapping, or HUSL mapping

Environment.plotsettings.VIRIDIS = False

Use the Viridis colormap

Environment.plotsettings.BBOX_INCHES = None

Use this parameter for bbox

Environment.plotsettings.palette(nb, hls=True, viridis=False)[source]

Use a smart palette from seaborn, for nb different plots on the same figure.

>>> palette(10, hls=True)  
[(0.86..., 0.37..., 0.33...), (0.86...,.65..., 0.33...), (0.78..., 0.86...,.33...), (0.49..., 0.86...,.33...), (0.33..., 0.86...,.46...), (0.33..., 0.86...,.74...), (0.33..., 0.68..., 0.86...) (0.33..., 0.40..., 0.86...) (0.56..., 0.33..., 0.86...) (0.84..., 0.33..., 0.86...)]
>>> palette(10, hls=False)  
[[0.96..., 0.44..., 0.53...], [0.88..., 0.52..., 0.19...], [0.71..., 0.60..., 0.19...], [0.54..., 0.65..., 0.19...], [0.19..., 0.69..., 0.34...], [0.20..., 0.68..., 0.58...],[0.21..., 0.67..., 0.69...], [0.22..., 0.65..., 0.84...], [0.55..., 0.57..., 0.95...], [0.85..., 0.44..., 0.95...]]
>>> palette(10, viridis=True)  
[(0.28..., 0.13..., 0.44...), (0.26..., 0.24..., 0.52...), (0.22..., 0.34..., 0.54...), (0.17..., 0.43..., 0.55...), (0.14..., 0.52..., 0.55...), (0.11..., 0.60..., 0.54...), (0.16..., 0.69..., 0.49...), (0.31..., 0.77..., 0.41...), (0.52..., 0.83..., 0.28...), (0.76..., 0.87..., 0.13...)]
  • To visualize:

>>> sns.palplot(palette(10, hls=True))  
>>> sns.palplot(palette(10, hls=False))  # use HUSL by default  
>>> sns.palplot(palette(10, viridis=True))  
Environment.plotsettings.makemarkers(nb)[source]

Give a list of cycling markers. See http://matplotlib.org/api/markers_api.html

Note

This what I consider the optimal sequence of markers, they are clearly differentiable one from another and all are pretty.

Examples:

>>> makemarkers(7)
['o', 'D', 'v', 'p', '<', 's', '^']
>>> makemarkers(12)
['o', 'D', 'v', 'p', '<', 's', '^', '*', 'h', '>', 'o', 'D']
Environment.plotsettings.PUTATRIGHT = False

Default parameter for legend(): if True, the legend is placed at the right side of the figure, not on it. This is almost mandatory for plots with more than 10 algorithms (good for experimenting, bad for publications).

Environment.plotsettings.SHRINKFACTOR = 0.75

Shrink factor if the legend is displayed on the right of the plot.

Warning

I still don’t really understand how this works. Just manually decrease if the legend takes more space (i.e., more algorithms with longer names)

Environment.plotsettings.MAXNBOFLABELINFIGURE = 8

Default parameter for maximum number of label to display in the legend INSIDE the figure

Environment.plotsettings.legend(putatright=False, fontsize='large', shrinkfactor=0.75, maxnboflabelinfigure=8, fig=None, title=None)[source]

plt.legend() with good options, cf. http://matplotlib.org/users/recipes.html#transparent-fancy-legends.

Environment.plotsettings.maximizeWindow()[source]

Experimental function to try to maximize a plot.

Warning

This function is still experimental, but “it works on my machine” so I keep it.

Environment.plotsettings.FORMATS = ('png', 'pdf')

List of formats to use for saving the figures, by default. It is a smart idea to save in both a raster and vectorial formats

Environment.plotsettings.show_and_save(showplot=True, savefig=None, formats=('png', 'pdf'), pickleit=False, fig=None)[source]

Maximize the window if need to show it, save it if needed, and then show it or close it.

Environment.plotsettings.add_percent_formatter(which='xaxis', amplitude=1.0, oldformatter='%.2g%%', formatter='{x:.1%}')[source]

Small function to use a Percentage formatter for xaxis or yaxis, of a certain amplitude.

  • which can be “xaxis” or “yaxis”,

  • amplitude is a float, default to 1.

  • More detail at http://stackoverflow.com/a/36320013/

  • Not that the use of matplotlib.ticker.PercentFormatter require matplotlib >= 2.0.1

  • But if not available, use matplotlib.ticker.StrMethodFormatter(“{:.0%}”) instead

Environment.plotsettings.WIDTH = 95

Default value for the width parameter for wraptext() and wraplatex().

Environment.plotsettings.wraptext(text, width=95)[source]

Wrap the text, using textwrap module, and width.

Environment.plotsettings.wraplatex(text, width=95)[source]

Wrap the text, for LaTeX, using textwrap module, and width.

Environment.plotsettings.nrows_ncols(N)[source]

Return (nrows, ncols) to create a subplots for N plots of the good size.

>>> for N in range(1, 22):
...     nrows, ncols = nrows_ncols(N)
...     print("For N = {:>2}, {} rows and {} cols are enough.".format(N, nrows, ncols))
For N =  1, 1 rows and 1 cols are enough.
For N =  2, 2 rows and 1 cols are enough.
For N =  3, 2 rows and 2 cols are enough.
For N =  4, 2 rows and 2 cols are enough.
For N =  5, 3 rows and 2 cols are enough.
For N =  6, 3 rows and 2 cols are enough.
For N =  7, 3 rows and 3 cols are enough.
For N =  8, 3 rows and 3 cols are enough.
For N =  9, 3 rows and 3 cols are enough.
For N = 10, 4 rows and 3 cols are enough.
For N = 11, 4 rows and 3 cols are enough.
For N = 12, 4 rows and 3 cols are enough.
For N = 13, 4 rows and 4 cols are enough.
For N = 14, 4 rows and 4 cols are enough.
For N = 15, 4 rows and 4 cols are enough.
For N = 16, 4 rows and 4 cols are enough.
For N = 17, 5 rows and 4 cols are enough.
For N = 18, 5 rows and 4 cols are enough.
For N = 19, 5 rows and 4 cols are enough.
For N = 20, 5 rows and 4 cols are enough.
For N = 21, 5 rows and 5 cols are enough.
Environment.plotsettings.addTextForWorstCases(ax, n, bins, patches, rate=0.85, normed=False, fontsize=8)[source]

Add some text labels to the patches of an histogram, for the last ‘rate’%.

Use it like this, to add labels for the bins in the 65% largest values n:

>>> n, bins, patches = plt.hist(...)
>>> addTextForWorstCases(ax, n, bins, patches, rate=0.65)
Environment.plotsettings.myviolinplot(*args, nonsymmetrical=False, **kwargs)[source]
Environment.plotsettings.violin_or_box_plot(data=None, labels=None, boxplot=False, **kwargs)[source]

Automatically add labels to a box or violin plot.

Warning

Requires pandas (https://pandas.pydata.org/) to add the xlabel for violin plots.

Environment.plotsettings.MAX_NB_OF_LABELS = 50

If more than MAX_NB_OF_LABELS labels have to be displayed on a boxplot, don’t put a legend.

Environment.plotsettings.adjust_xticks_subplots(ylabel=None, labels=(), maxNbOfLabels=50)[source]

Adjust the size of the xticks, and maybe change size of ylabel.

Environment.plotsettings.table_to_latex(mean_data, std_data=None, labels=None, fmt_function=None, name_of_table=None, filename=None, erase_output=False, *args, **kwargs)[source]

Tries to print the data from the input array or collection of array or pandas.DataFrame to the stdout and to the file filename (if it does not exist).

Warning

FIXME this is still experimental! And useless, most of the time we simply do a copy/paste from the terminal to the LaTeX in the article…