Color cycles

ProPlot defines color cycles as color palettes comprising sets of distinct colors. Unlike colormaps, interpolation between these colors may not make sense. Color cycles are generally used with bar plots, line plots, and other distinct plot elements. ProPlot uses the ListedColormap class to name color cycles, then applies them to plots by updating the property cycler. Color cycles can also be made by sampling colormaps.

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. [1]:
  1. import proplot as plot
  2. fig, axs = plot.show_cycles()

_images/cycles_2_0.svg

Changing the color cycle

You can make and apply new property cyclers with the Cycle constructor function. Various plotting commands like plot and scatter now accept a cycle keyword arg, which is passed to Cycle (see cycle_changer). To save your color cycle data and use it every time ProPlot is imported, simply pass save=True to Cycle. If you want to change the global property cycler, pass a name to the [rc.cycle](https://proplot.readthedocs.io/en/latest/configuration.html?highlight=cycle#rc-proplot) setting or pass the result of Cycle to the [rc[‘axes.prop_cycle’]](https://matplotlib.org/tutorials/introductory/customizing.html?highlight=axes.prop_cycle#the-matplotlibrc-file) setting (see the configuration guide).

  1. [2]:
  1. import proplot as plot
  2. import numpy as np
  3. lw = 5
  4. state = np.random.RandomState(51423)
  5. data = (state.rand(12, 6) - 0.45).cumsum(axis=0)
  6. kwargs = {'legend': 'b', 'labels': list('abcdef')}
  7. # Modify the default color cycle
  8. plot.rc.cycle = '538'
  9. fig, axs = plot.subplots(ncols=3, axwidth=1.9)
  10. axs.format(suptitle='Changing the color cycle')
  11. ax = axs[0]
  12. ax.plot(data, lw=lw, **kwargs)
  13. ax.format(title='Global color cycle')
  14. # Pass the cycle to a plotting command
  15. ax = axs[1]
  16. ax.plot(data, cycle='qual1', lw=lw, **kwargs)
  17. ax.format(title='Local color cycle')
  18. # As above but draw each line individually
  19. # Note that the color cycle is not reset with each plot call
  20. ax = axs[2]
  21. labels = kwargs['labels']
  22. for i in range(data.shape[1]):
  23. ax.plot(data[:, i], cycle='qual1', legend='b', label=labels[i], lw=lw)
  24. ax.format(title='With multiple plot calls')

_images/cycles_4_0.svg

Making new color cycles

You can make new color cycles with the Cycle constructor function. One great way to make cycles is by sampling a colormap! Just pass the colormap name to Cycle, and optionally specify the number of samples you want to draw as the last positional argument (e.g. plot.Cycle('Blues', 5)).

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 the colormaps section for details.

In the below example, several cycles are constructed from scratch, and the lines are referenced with colorbars and legends. Note that ProPlot allows you to generate colorbars from lists of lines.

  1. [3]:
  1. import proplot as plot
  2. import numpy as np
  3. fig, axs = plot.subplots(ncols=2, share=0, axwidth=2.3)
  4. state = np.random.RandomState(51423)
  5. data = (20 * state.rand(10, 21) - 10).cumsum(axis=0)
  6. # Cycle from on-the-fly monochromatic colormap
  7. ax = axs[0]
  8. lines = ax.plot(data[:, :5], cycle='plum', cycle_kw={'fade': 85}, lw=5)
  9. fig.colorbar(lines, loc='b', col=1, values=np.arange(0, len(lines)))
  10. fig.legend(lines, loc='b', col=1, labels=np.arange(0, len(lines)))
  11. ax.format(title='Cycle from color')
  12. # Cycle from registered colormaps
  13. ax = axs[1]
  14. cycle = plot.Cycle('blues', 'reds', 'oranges', 15, left=0.1)
  15. lines = ax.plot(data[:, :15], cycle=cycle, lw=5)
  16. fig.colorbar(lines, loc='b', col=2, values=np.arange(0, len(lines)), locator=2)
  17. fig.legend(lines, loc='b', col=2, labels=np.arange(0, len(lines)), ncols=4)
  18. ax.format(
  19. title='Cycle from merged colormaps',
  20. suptitle='Color cycles from colormaps'
  21. )

_images/cycles_6_0.svg

Cycles of other properties

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.

  1. [4]:
  1. import numpy as np
  2. import pandas as pd
  3. # Create cycle that loops through 'dashes' Line2D property
  4. cycle = plot.Cycle(dashes=[(1, 0.5), (1, 1.5), (3, 0.5), (3, 1.5)])
  5. # Generate sample data
  6. state = np.random.RandomState(51423)
  7. data = (state.rand(20, 4) - 0.5).cumsum(axis=0)
  8. data = pd.DataFrame(data, columns=pd.Index(['a', 'b', 'c', 'd'], name='label'))
  9. # Plot data
  10. fig, ax = plot.subplots(axwidth=2.6, aspect=1)
  11. ax.format(suptitle='Plot without color cycle')
  12. obj = ax.plot(
  13. data, lw=3, cycle=cycle, legend='ul',
  14. legend_kw={'ncols': 2, 'handlelength': 3}
  15. )

_images/cycles_8_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 and call register_cycles (or restart your python session), or use from_file. The file name is used as the registered cycle name. See from_file for a table of valid file extensions.