0

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],```

1 Answer 1

1

Something like:

colorlist = [(0.04, 0.015, 0.02, 1.0), (0.8, 0.5, 0.2, 1.0)] * 10

Gets you 10 repetitions of the list as a list, which you can then cast to an array, as needed?

Similarly, if you want to repeat each colour a known number of times:

colorlist = ([(0.04, 0.015, 0.02, 1.0)] * 2 + [(0.8, 0.5, 0.2, 1.0)] * 3) * 4

Gets you 4 repetitions of the first colour twice and the second colour three times.

To cast this to an array of object, for ax.bar, you can do something like:

import numpy as np


colorlist = np.array(
    ([(0.04, 0.015, 0.02, 1.0)] * 2 + [(0.8, 0.5, 0.2, 1.0)] * 3) * 4, dtype=object
)

Given the specific example code, using color instead of facecolor works with the original list, even without the cast or the array data type:

import matplotlib.pyplot as plt

colorlist = ([(0.04, 0.015, 0.02, 1.0)] * 1 + [(0.8, 0.5, 0.2, 1.0)] * 1) * 16

fig, ax = plt.subplots()
y = [1] * 32
x = range(32)
ax.bar(x, y, color=colorlist)
plt.show()

However, if preferred, it also works with the np.array and either an object or tuple dtype, as well as with the given numpy data types. I just wanted to show the result can be achieved with basic Python and matplotlib alone.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Grismar, this is nice and simple and clear. It does exactly what you said it does. Unfortunatly it doesn't work in the ax.bar( statement, says "ValueError: Invalid RGBA argument:" presumably because it's not in a numpy array with dtype=object?
I've added a suggestion to cast accordingly; do note that this seems to translate the inner tuples into lists / arrays as well - but I expect that shouldn't be an issue
Unfortunately the translation from tuple to lists does make a difference to ax.bar. Some error, "Invalid RGBA argument". It's weird, why does it change it from a tuple to a list/array?
If you provide a more complete example in your code, that you would expect to work, I'd be happy to give it a try
Thanks Grismar, I appreciate it. I've edited the original question to include an example. It doesn't work for me, presumably because the RGBA values are tuples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.