5

Suppose I have the following two dataframes:

df1 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()
df2 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()

df2 plot

df1 plot

My question is that, how can I plot them in one graph such that:

  1. The three series of df1 and df2 are still in the same blue, orange and green lines as above.
  2. The three series of df1 are in solid lines
  3. The three series of df1 are in dashed lines

Currently the closest thing I can get is the following:

ax = df1.plot(style=['b','y','g'])
df2.plot(ax=ax, style=['b','y','g'], linestyle='--')

enter image description here

Is there any way to get the color codes used by default by DataFrame.plot()? Or is there any other better approach to achieve what I want? Ideally I don't want to specify any color codes with the style parameter but always use the default colors.

GoCurry
  • 899
  • 11
  • 31

3 Answers3

8

You could use get_color from the lines:

df1 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()
df2 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()

ax = df1.plot()
l = ax.get_lines()
df2.plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Hi @Scott Boston, I have posted a similar question: https://stackoverflow.com/questions/65445131/comparing-values-from-two-datasets . If you could have a look at mine too, it would be greatly appreciated. I have tried to follow your answer and other answers, but unfortunately I am still having difficulties in getting a good output (because of colours, lines and legend). Thanks a lot – V_sqrt Dec 25 '20 at 18:23
8

Without messing with the colors themselves or transferring them from one plot to the other you may easily just reset the colorcycle in between your plot commands

ax = df1.plot()
ax.set_prop_cycle(None)
df2.plot(ax=ax, linestyle="--")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
5

You can get the default color parameters that are currently being used from matplotlib.

import matplotlib.pyplot as plt
colors = list(plt.rcParams.get('axes.prop_cycle'))

[{'color': '#1f77b4'},
 {'color': '#ff7f0e'},
 {'color': '#2ca02c'},
 {'color': '#d62728'},
 {'color': '#9467bd'},
 {'color': '#8c564b'},
 {'color': '#e377c2'},
 {'color': '#7f7f7f'},
 {'color': '#bcbd22'},
 {'color': '#17becf'}]

so just pass style=['#1f77b4', '#ff7f0e', '#2ca02c'] and the colors should work.

If you want to set another color cycler, say the older version, then:

plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'bgrcmyk')")
list(plt.rcParams['axes.prop_cycle'])
#[{'color': 'b'},
# {'color': 'g'},
# {'color': 'r'},
# {'color': 'c'},
# {'color': 'm'},
# {'color': 'y'},
# {'color': 'k'}]
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • 1
    You can also get just the keys rather than a list of dictionaries, and plot them with seaborn! `sns.palplot(plt.rcParams['axes.prop_cycle'].by_key()['color'])` – johnchase Apr 10 '18 at 21:52
  • Ah that's smarter, I guess you don't really need the dictionary in this case – ALollz Apr 10 '18 at 21:56
  • Thank you ALollz. What does `list('bgrcmyk')` do? If I remove it and it will still returns the same result. And based on what I read in this page (https://matplotlib.org/users/dflt_style_changes.html#colors-color-cycles-and-color-maps) it is the 7 colors of the old version, which is no the 10 colors returned. – GoCurry Apr 12 '18 at 14:58
  • @Jimbo you're right, that didn't seem to be doing anything. It's still just listing the default. I'll update that, and show how you could set and get the colors for the old version. – ALollz Apr 12 '18 at 16:25