I am looking for a pythonic/simple way to create a list/sequence of repeating tuples (specifically RGBA values) so that I can apply it to the colors of bars in a matplotlib bar chart.
ax.bar(...
, color=colorlist
)
For example
print(colorlist)
array([(0.04, 0.015, 0.02, 1.0),
(0.8, 0.5, 0.2, 1.0),
(0.04, 0.015, 0.02, 1.0), ...,
(0.8, 0.5, 0.2, 1.0),
(0.04, 0.015, 0.02, 1.0),
(0.8, 0.5, 0.2, 1.0)], dtype=object)
I know the length of colorlist (e.g. N) I am after. I just can't seem to find a way to create colorlist.
I am after a way to arrange the two colors (say color1 and color2) either one immediately followed by the second color (e.g. 0,1,0,1,0,1,..), or some number of one color in a row, then followed another number of the second color, and repeated until the end (0,0,0,1,1,1,0,0,0,1,1,1,..).
EDIT: A worked example;
import numpy as np
import matplotlib.pyplot as plt
colorlist = np.array(
([(0.04, 0.015, 0.02, 1.0)] * 1 + [(0.8, 0.5, 0.2, 1.0)] * 1) * 16, dtype=object
)
fig, ax = plt.subplots()
y = np.ones(32)
x = np.linspace(1,32,32)
ax.bar(x, y, facecolor = colorlist)
with error
ValueError: Invalid RGBA argument: array([[0.04, 0.015, 0.02, 1.0],
[0.8, 0.5, 0.2, 1.0],
[0.04, 0.015, 0.02, 1.0],```