1

I'm trying to plot 13 different columns from a dataframe in one line graph using matplotlib and I am really struggling to understand how matplotlib works with colors. After 10 unique colors the pallet begins to loop.

There seem to be countless examples of how to do this on SO but, my limited understanding of the tool makes rhetorical examples such as these quiet inaccessible. What if my data is not an arbitrary collection of lines?

Better yet, is there anyway I can permanently change my matplotlib configuration to infinitely create new colors as needed?

I've been struggling with this for a while and can use whatever advice you can offer.

NYezhov
  • 45
  • 5

1 Answers1

1

If you are working with Pandas dataframe, you can pass a cmap to plot:

df.plot(cmap='tab20')

Update: With matplotlib:

cmap=plt.get_cmap('tab20')
for i,col in enumerate(df.columns):
    plt.plot(df[col], color=cmap(i), label=col)
    
plt.legend()
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
  • 1
    I am trying to work in matplotlib, however. Just because I feel that pandas built in plot has its fair share of limitations. – NYezhov Feb 23 '21 at 19:21
  • 1
    @NYezhov Agreed that pandas plotting isn't as accessible as matplotlib's. However, I see for most common plots, it does just fine. If you are plotting just line plots, you can catch the axis instance returned by `df.plot` and format the axis further. That said, see updates for a matplotlib solution. – Quang Hoang Feb 23 '21 at 19:28
  • you are a saint. Thank you. – NYezhov Feb 23 '21 at 19:31