Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I plot two pandas DataFrames in one graph with the same colors but different line styles?

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.

like image 345
GoCurry Avatar asked Apr 10 '18 21:04

GoCurry


People also ask

How can you compare two DataFrames are identical?

DataFrame - equals() function The equals() function is used to test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements. NaNs in the same location are considered equal.

Can you combine two DataFrames in pandas?

Pandas' merge and concat can be used to combine subsets of a DataFrame, or even data from different files. join function combines DataFrames based on index or column. Joining two DataFrames can be done in multiple ways (left, right, and inner) depending on what data must be in the final DataFrame.

Can you concat two DataFrames with different columns?

It is possible to join the different columns is using concat() method. DataFrame: It is dataframe name. axis: 0 refers to the row axis and1 refers the column axis. join: Type of join.


3 Answers

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="--")
like image 124
ImportanceOfBeingErnest Avatar answered Oct 19 '22 11:10

ImportanceOfBeingErnest


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

like image 8
Scott Boston Avatar answered Oct 19 '22 11:10

Scott Boston


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'}]
like image 5
ALollz Avatar answered Oct 19 '22 09:10

ALollz