0

Say I have a pandas dataframe and I plot it with matplotlib via the pandas built-in function plot:

mydata = list(zip([1, 2, 3], [4, 5, 6]))
dataframe = pandas.DataFrame(mydata)
barchart = dataframe.plot(kind = 'bar')

How do I access the color of the bars?

More generally, how do I access all the attributes?

E.g. I can get the background color with barchart.get_axis_bgcolor() and I can set it to grey with barchart.set_axis_bgcolor('grey'), but what about all the other attributes?

1 Answer 1

1

To change the color of a series you can do this with colors,

mydata = list(zip([1, 2, 3], [4, 5, 6]))
dataframe = pd.DataFrame(mydata) 
barchart = dataframe.plot(kind = 'bar',color=['maroon','purple'])

However, with the barchart variable you have a handle on the axes, now you can use get_children() to get a hold of any patches, text, axis, or any other object in the chart to modify just about anything you want.

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

4 Comments

Then I guess I do not know how to access the attribute color for the bars. I tried dir but found nothing related.
Also, say I want to change the colors of the bars via a colormap. Using plot I'd do something like plot(..., color=cmap). Is it possible to achieve the same through the handles?
Ok I managed to change colors, e.g.: barchart.patches[0].set_color('g'). Great, thank you. What about using a colormap?
Ok I guess I managed to do that too: just looping over all patches. Great. Thank you for your input. Case dismissed.

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.