0

Matpltolib seems to apply the tab10 colormap by default.

enter image description here

How can I change the default colormap and use a different colormap instead, e.g. Set3?

I tried plt.rcParams['image.cmap'] = 'Set3' and plt.set_cmap('Set3') as suggested in this answer. However, non of the two options worked.

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['image.cmap']='Set3'
plt.set_cmap('Set3')

species = ('Adelie', 'Chinstrap', 'Gentoo')
sex_counts = {
    'Male': np.array([73, 34, 61]),
    'Female': np.array([73, 34, 58]),
}

width = 0.6  # the width of the bars: can also be len(x) sequence

fig, ax = plt.subplots()
bottom = np.zeros(3)

for sex, sex_count in sex_counts.items():
    p = ax.bar(species, sex_count, width, label=sex, bottom=bottom)
    bottom += sex_count

    ax.bar_label(p, label_type='center')

ax.set_title('Number of penguins by sex')
ax.legend()

plt.show()

enter image description here

Stücke
  • 868
  • 3
  • 14
  • 41
  • 1
    The default colors for the bars come from the cycler, not from the default colormap. E.g. `plt.rcParams['axes.prop_cycle'] = plt.cycler(color=plt.cm.Set3.colors)`. The default colormap for functions such as `plt.imshow(...)` is 'viridis' which is meant to color continuous data. – JohanC Feb 17 '23 at 10:17
  • That's the answer! Thank you! – Stücke Feb 17 '23 at 10:20

0 Answers0