Color cycles

Color cycles are palettes composed of a jumbled set of distinct colors. Unlike Colormaps, interpolation between these colors does not make sense. Color cycles are generally used with line plots, bar plots, and other plot elements. They are conceptually implemented in matplotlib with the ListedColormap class (although it is often improperly used). ProPlot uses this class to register color cycles, and the color cycles are “applied” by globally or locally modifying the property cycler. Colormaps can also be cut up and used as color cycles (see Making new color cycles).

ProPlot adds several features to help you use color cycles effectively in your figures. This section documents the new registered color cycles, explains how to make and modify colormaps, and shows how to apply them to your plots.

Included color cycles

Use show_cycles to generate a table of the color cycles registered by default and loaded from your ~/.proplot/cycles folder. You can make your own color cycles using the Cycle constructor function.

[1]:
import proplot as plot
f = plot.show_cycles()
_images/cycles_4_0.svg

Changing the color cycle

ProPlot lets you specify a property cycle by passing cycle to plotting commands like plot and scatter (see cycle_changer), which is passed to Cycle, and Cycle keyword args can be specified with cycle_kw. If you want to save your own color cycle into the ~/.proplot/cycles folder, simply pass save=True to Cycle. Color cycles in this folder are loaded every time you import ProPlot. If you want to change the global property cycler, use the rc.cycle setting (see Configuring proplot).

[2]:
import proplot as plot
import numpy as np
state = np.random.RandomState(51423)
data = (state.rand(12, 12) - 0.45).cumsum(axis=0)
plot.rc.cycle = 'contrast'
lw = 5
f, axs = plot.subplots(ncols=3, axwidth=1.7)
# Use the default cycle
ax = axs[0]
ax.plot(data, lw=lw)
# Pass cycle to the plotting command (note this never resets the cycle)
ax = axs[1]
ax.plot(data, cycle='qual2', lw=lw)
ax = axs[2]
for i in range(data.shape[1]):
    ax.plot(data[:, i], cycle='qual2', lw=lw)
axs.format(suptitle='Local and global color cycles demo')
_images/cycles_7_0.svg

Making new color cycles

You can make new color cycles with the Cycle constructor. One great way to make cycles is by sampling arbitrary colormaps! Just pass a color name or colormap name to the Cycle constructor, with the last positional argument indicating the number of samples you want to draw.

Positional arguments passed to Cycle are interpreted by the Colormap constructor, and the resulting colormap is sampled at discrete values. To exclude near-white colors on the end of a colormap, pass e.g. left=x to Cycle, or supply a plotting command with e.g. cycle_kw={'left':x}. See Colormaps for details.

In the below example, several cycles are constructed from scratch, and the lines are referenced with colorbars and legends. For making colorbars from a list of lines, see Colorbars and legends.

[3]:
import proplot as plot
import numpy as np
f, axs = plot.subplots(ncols=2, share=0, axwidth=2, aspect=1.2)
state = np.random.RandomState(51423)
data = (20*state.rand(10, 21) - 10).cumsum(axis=0)
# Example 1
ax = axs[0]
lines = ax.plot(data[:, :5], cycle='plum', cycle_kw={'left': 0.3}, lw=5)
f.colorbar(lines, loc='b', col=1, values=np.arange(0, len(lines)))
f.legend(lines, loc='b', col=1, labels=np.arange(0, len(lines)))
ax.format(title='Cycle from color')
# Example 2
ax = axs[1]
cycle = plot.Cycle('blues', 'reds', 'oranges', 15, left=0.1)
lines = ax.plot(data[:, :15], cycle=cycle, lw=5)
f.colorbar(lines, loc='b', col=2, values=np.arange(0, len(lines)), locator=2)
f.legend(lines, loc='b', col=2, labels=np.arange(0, len(lines)), ncols=4)
ax.format(title='Cycle from merged colormaps',
          suptitle='Color cycles from colormaps demo')
_images/cycles_10_0.svg

Generic property cycles

Cycle can also generate cyclers that change properties other than color. Below, a single-color dash style cycler is constructed and applied to the axes locally. To apply it globally, simply use plot.rc['axes.prop_cycle'] = cycle.

[4]:
import proplot as plot
import numpy as np
import pandas as pd
f, ax = plot.subplots(axwidth=3, aspect=1.5)
state = np.random.RandomState(51423)
data = (state.rand(20, 4) - 0.5).cumsum(axis=0)
data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))
ax.format(suptitle='Plot without color cycle')
cycle = plot.Cycle(dashes=[(1, 0.5), (1, 1.5), (3, 0.5), (3, 1.5)])
obj = ax.plot(data, lw=3, cycle=cycle, legend='ul',
              legend_kw={'ncols': 2, 'handlelength': 3})
_images/cycles_13_0.svg

Downloading color cycles

There are plenty of online interactive tools for generating and testing color cycles, including i want hue, coolers, and viz palette.

To add color cycles downloaded from any of these sources, save the cycle data to a file in your ~/.proplot/cycles folder, then call register_cycles. The file should be named name.ext, where name will be the registered cycle name. See register_cmaps for valid file extensions.