question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

df.plot bars with different colors depending on values

See original GitHub issue

I 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:open
  • Created 6 years ago
  • Reactions:5
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
yz3101commented, Oct 23, 2018

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”]))

2reactions
rjp23commented, Jan 30, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found