df.plot bars with different colors depending on values
See original GitHub issueI found a strange behavior with pandas.plot colors my version of pandas is
import pandas as pd
pd.__version__
'0.20.3'
Problem description
Before when i wanted to assign different colors to bars depending on value i could simply do
n=10
df = pd.DataFrame({"a":np.arange(1,n)})
df.plot(kind='bar', color=np.where(df["a"]>2, 'g', 'r'))
Now not only it accepts only a tuple
instead of an array as color but it breaks whenever the tuple
is longer than 5
n=5
df = pd.DataFrame({"a":np.arange(1,n)})
colors = tuple(np.where(df["a"]>2, 'g', 'r'))
df.plot(kind='bar', color= colors )
n=6
df = pd.DataFrame({"a":np.arange(1,n)})
colors = tuple(np.where(df["a"]>2, 'g', 'r'))
df.plot(kind='bar', color= colors )
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Color matplotlib bar chart based on value - Stack Overflow
Iterate over all values and append colors to a list depending on customized conditions, so you get a list with as many color...
Read more >pandas.DataFrame.plot.bar — pandas 1.5.2 documentation
Allows plotting of one column versus another. If not specified, all numerical columns are used. colorstr, array-like, or dict, optional. The color for ......
Read more >Pandas Plot: Make Better Bar Charts in Python - Shane Lynn
Colours can be added to each bar in the bar chart based on the values in a different categorical column. Using a dictionary...
Read more >Pandas Bar Plot – DataFrame.plot.bar() | Data Independent
color – The color you want your bars to be. If you have multiple sets of bars (like in a grouped or stacked...
Read more >Pandas bar graph plot with different color for each column ...
Coding example for the question Pandas bar graph plot with different color for each column based on value-Pandas,Python.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found that you have different color for each bar when you do this n=6 df = pd.DataFrame({“a”:np.arange(1,n)}) df[‘a’].plot(kind=‘bar’, color=tuple([“g”, “b”,“r”,“y”,“k”]))
This seems to still be an issue and @yz3101’s response above solved it for me. Explicitly calling the column of the DataFrame allowed me to specify colours. Otherwise, it doesn’t work.