We start with the bar plot I created here. Which gave us this…

Now we want to give it some nice colours.
This we can do by importing the colour map from Matplotlib and extracting a range of colours. I like the Viridis colormap so I’ll use that. Also, we have 5 variables in our stacked bar plot, so we only want 5 colours from it.
from matplotlib import cm # get the colormap and extract 5 colours viridis = cm.get_cmap('viridis', 5)
You can get the individual values like this…
print(viridis.colors)
Which should give you something like this
[[0.267004 0.004874 0.329415 1. ] [0.229739 0.322361 0.545706 1. ] [0.127568 0.566949 0.550556 1. ] [0.369214 0.788888 0.382914 1. ] [0.993248 0.906157 0.143936 1. ]]
Now we can allocate each of these values to out plot. Lets do the first one as an example…
p1 = plt.bar(xMain, dataVar1, color=viridis.colors[0])
Once you do the same for the other 4, you should get something that looks like this.

So much nicer!
The complete code looks like this:
#!/usr/local/bin/python3 # -*- coding: utf-8 -*- # # make a stackbar for data import pandas as pd import matplotlib.pyplot as plt import numpy as np from matplotlib import cm # open the file df = pd.read_csv('data_file.csv') # get the data you want to plot dataVar1 = df['column1'] dataVar2 = df['column2'] dataVar3 = df['column3'] dataVar4 = df['column4'] dataVar5 = df['column5'] # number of entries (x axis) xMain = np.arange(30) # color map viridis = cm.get_cmap('viridis', 5) # plot each data p1 = plt.bar(xMain, dataVar1, color=viridis.colors[0]) p2 = plt.bar(xMain, dataVar2, bottom=dataVar1, color=viridis.colors[1]) p3 = plt.bar(xMain, dataVar3, bottom=dataVar1+dataVar2, color=viridis.colors[2]) p4 = plt.bar(xMain, dataVar4, bottom=dataVar1+dataVar2+dataVar3, color=viridis.colors[3]) p5 = plt.bar(xMain, dataVar4, bottom=dataVar1+dataVar2+dataVar3+dataVar4, color=viridis.colors[4]) # show the graph plt.savefig('output_fig.png')