Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to color pandas plot in groups

If i have a pandas dataframe:

x = pd.DataFrame(np.random.randn(5,5), columns = ['0','1.1','1.2', '2.1', '2.2'])

and would like to plot it like this:

x.plot()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do i color them by groups, like 0 in one color, 1.1 and 1.2 in one color, 2.1 and 2.2 in one color. I would like the legend still to say all five types

>Solution :

You could just add a list of colors with the length of number of plots:
Instead of x.plot() you write:

x.plot(color=['r', 'g', 'g', 'b', 'b']) # red green green blue blue
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading