Last answered:

01 Dec 2022

Posted on:

25 Nov 2022

0

Resolved: coloring the bars

Hello Eli, thank you for your lesson! I would sincerely appreciate it if you help me with the following issue:
Regarding the coloring of the bars, I typed:

plt.figure(figsize = (9 , 8))
plt.bar(x = df_used_cars["Brand"],
       height = df_used_cars["Cars Listings"],
       color = "grbgrbgrbb")
plt.xticks(rotation = 45)
plt.show()

However I got the following output and error:

ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_13440\1396978304.py in <module>
      1 plt.figure(figsize = (9 , 8))
----> 2 plt.bar(x = df_used_cars["Brand"],
      3        height = df_used_cars["Cars Listings"],
      4        color = "grbgrbgrbb")
      5 plt.xticks(rotation = 45)

~\anaconda3\lib\site-packages\matplotlib\pyplot.py in bar(x, height, width, bottom, align, data, **kwargs)
   2397         x, height, width=0.8, bottom=None, *, align='center',
   2398         data=None, **kwargs):
-> 2399     return gca().bar(
   2400         x, height, width=width, bottom=bottom, align=align,
   2401         **({"data": data} if data is not None else {}), **kwargs)

~\anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
   1410     def inner(ax, *args, data=None, **kwargs):
   1411         if data is None:
-> 1412             return func(ax, *map(sanitize_sequence, args), **kwargs)
   1413 
   1414         bound = new_sig.bind(ax, *args, **kwargs)

~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in bar(self, x, height, width, bottom, align, **kwargs)
   2354         linewidth = itertools.cycle(np.atleast_1d(linewidth))
   2355         hatch = itertools.cycle(np.atleast_1d(hatch))
-> 2356         color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
   2357                                 # Fallback if color == "none".
   2358                                 itertools.repeat('none'))

~\anaconda3\lib\site-packages\matplotlib\colors.py in to_rgba_array(c, alpha)
    357 
    358     if isinstance(c, str):
--> 359         raise ValueError("Using a string of single character colors as "
    360                          "a color sequence is not supported. The colors can "
    361                          "be passed as an explicit list instead.")

ValueError: Using a string of single character colors as a color sequence is not supported. The colors can be passed as an explicit list instead.



2 answers ( 1 marked as helpful)
Instructor
Posted on:

28 Nov 2022

2

Hi Seyed,
thanks for reaching out! It seems that the functionality of using a single string as a color has been deprecated in some versions of matplotlib and is no longer supported, that's why you're getting the error. If you'd like to have individual colors for each bar, you can do the following:
1. Have a list of all the colors, using their full names
2. Pass that list for the color argument
Here is some example code:


colors = ["red","green","blue","white","yellow","magenta","cyan"]
plt.bar(x = df_used_cars["Brand"], #specify the x axis
        height = df_used_cars["Cars Listings"], #specify the y axis
        color = colors)

Let me know if this version works for you or if you have any further questions!

Best,
365 Eli

Posted on:

01 Dec 2022

0

Hello Eli,
Thanks for your reply. I tested; and it worked fine! Thanks again!!

Submit an answer