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

Using the same color for multiple plots if each dataset contains a common column

I have two data frames with several columns. I plot df#1 as as stacked bar plot and overlay df #2 as a line plot. If a data series in df#1 is present in df#2 I want the color of the bar segment & line to be the same, for ease of comparison. How would I do that? Below, I would want the colors of ‘FO’ to be the same.

Data Frames:

df#1

            FO     MI
Date                     
08-Jan-23  0.4925  0.0000
15-Jan-23  0.9075  0.0000
22-Jan-23  0.9750  0.0250
29-Jan-23  0.9625  0.0375
05-Feb-23  0.9825  0.0000
df#2

           FO      AR
Jan-23     1.0     1.1
Feb-23     1.0     2.2

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

>Solution :

You can get the column’s union and reindex:

cols = df1.columns.union(df2.columns)

fig, axes = plt.subplots(ncols=2)

df1.reindex(columns=cols).plot.bar(stacked=True, ax=axes[0])
df2.reindex(columns=cols).plot.line(ax=axes[1])

Output:

enter image description here

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