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.

plotting in pandas is not picking the list of colors passed, it only gets the first value

See original GitHub issue

Code Sample, a copy-pastable example if possible

>>> df = pd.DataFrame({"A": range(4), "color": ['red', 'blue', 'blue', 'red']})
>>> ax = df.plot.bar(y='A', color=df['color'])
>>> [p.get_facecolor() for p in ax.patches]
[(1.0, 0.0, 0.0, 1.0),
 (1.0, 0.0, 0.0, 1.0),
 (1.0, 0.0, 0.0, 1.0),
 (1.0, 0.0, 0.0, 1.0)]

Problem description

It plots correctly but it does not pick the right color per bar. it only pick the color of the first value in the list. it was working perfectly in pandas 0.19.2

Expected Output

the plot should contain a different color as the list is being passed. Similar to matplotlib behavior and the old pandas version

Output of pd.show_versions()

pandas: 0.20.2 pytest: None pip: 9.0.1 setuptools: 36.0.1 Cython: None numpy: 1.12.1 scipy: 0.19.1 xarray: None IPython: 5.4.1 sphinx: None patsy: None dateutil: 2.6.0 pytz: 2017.2 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.0.2 openpyxl: None xlrd: 1.0.0 xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: 0.9999999 sqlalchemy: None pymysql: None psycopg2: 2.7.1 (dt dec pq3 ext lo64) jinja2: 2.9.6 s3fs: None pandas_gbq: None pandas_datareader: None

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
adesormecommented, Feb 8, 2019

I’m still facing this issue…

5reactions
bcolsencommented, Jul 18, 2017

I have a work around for people being affected by this:

Plot with color in an extra list:

df.plot(y='A', color=[(0.3,0.4,0.4,1)])

also fixes bar:

>>> df = pd.DataFrame({"A": range(4), "color": ['red', 'blue', 'blue', 'red']})
>>> ax = df.plot.bar(y='A', color=[df['color']])
>>> [p.get_facecolor() for p in ax.patches]
Out[35]: 
[(1.0, 0.0, 0.0, 1.0),
 (0.0, 0.0, 1.0, 1.0),
 (0.0, 0.0, 1.0, 1.0),
 (1.0, 0.0, 0.0, 1.0)]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Assign line colors in pandas - Stack Overflow
Given that color is a keyword argument from matplotlib, I'd recommend not using a Pandas Series to hold the color values.
Read more >
Chart visualization — pandas 1.5.2 documentation - PyData |
When you pass other type of arguments via color keyword, it will be directly passed to matplotlib for all the boxes , whiskers...
Read more >
Plotly express arguments in Python
Over 14 examples of Plotly Express Arguments including changing color, size, log axes, and more in Python.
Read more >
Plot With Pandas: Python Data Visualization for Beginners
In this tutorial, you'll get to know the basic plotting possibilities that Python provides in the popular data analysis library pandas.
Read more >
100 pandas puzzles - | notebook.community
In the RecentDelays column, the values have been entered into the DataFrame as a list. We would like each first value in its...
Read more >

github_iconTop Related Medium Post

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