3

I want to use the same colormap/color cycle/palette to every plot in a Jupyter notebook.

With the seaborn package, I can use:

seaborn.set_palette('Set1')  

Is there a way to do the same using only matplotlib, without using seaborn?

I know how to define the colormap to each plot separately and I am aware of the predefined style (e.g, ggplot), but I can't find a way to define only the colormap to all the plots at once.

My intention is simplify the code for my students, thus using the intricate code behind set_palette() is not an option.

Edit: as the accepted answer shows, I was confusing colormap with color cycle.

lincolnfrias
  • 1,983
  • 4
  • 19
  • 29
  • 1
    The source code for `set_palette` [can be found here](https://github.com/mwaskom/seaborn/blob/master/seaborn/rcmod.py#L468). Due to helper functions, there is quite a lot of code that would be required to reproduce `set_palette`'s *exact* behavior without seaborn. But if you only want a subset of that behavior, you may find what you need in the source code. Or explain what subset of behavior you are looking for (a specific use case, your python version and matplotlib version), and maybe we can help you. – unutbu Oct 16 '17 at 12:23
  • Yes, I tried this, but my intention is to make the code simpler for my students and, as you set, it requires many helper functions. I'll clarify my question. – lincolnfrias Oct 16 '17 at 15:55

1 Answers1

9

The default colormap in matplotlib is "viridis". This is set at the rcParam "image.cmap".

The default colorcycle can be changed via the "axes.prop_cycle" rcParam.

import matplotlib.pyplot as plt
# to change default colormap
plt.rcParams["image.cmap"] = "Set1"
# to change default color cycle
plt.rcParams['axes.prop_cycle'] = plt.cycler(color=plt.cm.Set1.colors)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Thanks, but AFAIK this works only for images (plots created with `imshow( )`), not for regular plots (scatter plots, barplots etc.). Please, let me know if I am wrong. – lincolnfrias Oct 16 '17 at 15:53
  • This works for any plot that uses a colormap. It will then use the colormap that you specify as rcParam, e.g. scatter plots. Matplotlib barplots don't use any colormap, so what would you expect to happen? – ImportanceOfBeingErnest Oct 16 '17 at 15:57
  • I am looking for the same behavior of `set_palette( )`. If I redefine the colormap, with something like `plt.rcParams["image.cmap"] = "Accent"`, when I call `plt.scatter(x[:,0],x[:,1])` (without the color argument) it would use the first color on the colormap. But maybe I'm wrongly expecting a colormap to function as palette. – lincolnfrias Oct 16 '17 at 16:07
  • 1
    Ah, you don't want to change any colormap here. You want to set the default color cycle. I believe the combination of the two duplicates gives you what you want. But I also edited the answer accordingly. If not, you may ask more specifically. – ImportanceOfBeingErnest Oct 16 '17 at 16:16
  • Yes, that is exactly what I want! But it is really a duplicate. Should I delete then? I don't want to waste your effort. – lincolnfrias Oct 16 '17 at 16:18