1

I have data somewhat like this, presenting Net Cash Flow per Portfolio, and on what dates:

import datetime
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df = pd.DataFrame({'PORTFOLIO':  ['A', 'A', 'A', 'A','A', 'A', 'A', 'B','B', 'B','B', 'B', 'B', 'B','C'],
               'DATE': ['28-02-2018','28-02-2018','28-02-2018','10-10-2018','10-10-2018','01-12-2018','31-12-2018',
                        '30-09-2018','30-09-2018','30-09-2018','31-12-2018','31-01-2019','28-02-2019','05-03-2019','01-07-2019'],
               'NCF': [ 856000, 900000, 45000, 2005600,43900, 46700, 900000, 7890000, 821000, 95000, 400000, 7000000, 82500,10000000,1525000],
               })
df2=df.groupby(['PORTFOLIO','DATE']).sum().reset_index()
df2

I group it as I am only interested in seeing the cash flows per days. enter image description here

Now I am interested in visualizing the Cash Flow in a bar chart per portfolio.

sns.set(style='dark', color_codes=True)
g=sns.FacetGrid(df2, col="PORTFOLIO", hue='PORTFOLIO',col_wrap=3, height=5,  sharey=False, sharex=False)


g=g.map(plt.bar,'DATE','NCF')
g.set_xticklabels(rotation=45)
plt.tight_layout()
plt.show()

Unfortunately, the seaborn facetgrid multiplots gives me incorrect values on the x axis, no matter what I try to do with the dataset. It is like the first portfolio sets the tick-values, and the rest just has to follow even thoug the dates are incorrect. enter image description here

If I remove g.set_xticklabels(rotation=45) Then portfolio C gets the correct date, and it seems like the correct dates on B are hidden behind the incorrect 'A'-dates. enter image description here

The order of the bins change, but still not correct (monotonic increasing by date).

What am I doing wrong, and how can I fix this?

  • Could you please elaborate more on what is the desired result? – Julia Mar 12 '19 at 13:41
  • @Julia, I would like to see correct dates under the bars in the orange and green barplots. For example, portfolio 'C' only have one cash flow on the 1st of July, not the 1st of december as it appears from the barplot. – Heidi Falkeborg Mar 12 '19 at 13:45

1 Answers1

2

First convert to datetime and sort:

df2.DATE = pd.to_datetime(df2.DATE)
df2 = df2.sort_values(by=['PORTFOLIO', 'DATE'])
df2.DATE = df2.DATE.astype(str)

You can access the individual axes with g.axes (based on this answer). So:

sns.set(style='dark', color_codes=True)
g=sns.FacetGrid(df2, col="PORTFOLIO", hue='PORTFOLIO',col_wrap=3, height=5,  sharey=False, sharex=False)
g=g.map(plt.bar,'DATE','NCF')
g.set_xticklabels(rotation=45)
for idx, v in enumerate(df2.PORTFOLIO.unique()):
    g.axes[idx].set_xticklabels(df2.loc[df2.PORTFOLIO == v, 'DATE'])
plt.tight_layout()
plt.show()

Gives you:

enter image description here

Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75
  • thank you for your answer. Unfortunately I dont think this will work, as my multiplot will vary each day, meaning one day I will look at 1 plot and other days I might have to look at 10 plots. So cant hardcode like this, i guess. – Heidi Falkeborg Mar 12 '19 at 14:08
  • 1
    I changed it a bit, maybe this helps – Josh Friedlander Mar 12 '19 at 14:12
  • Is it me, or does this "fix" not work anymore. My code that I run every day in production suddently startet showing crazy x-axis again. I thought I might have scrued up the code, but havent touched it since this post. I thought I would look up this post, but were very suprised to see that your Answer no longer "fixed" the issue. Could there be an update in Seaborne that "ruins" your fix? Or maybe the matplotlib update a few days ago?? I am confused,.. – Heidi Falkeborg May 10 '19 at 12:32
  • 1
    Hey Heidi, I just retested on my setup (pandas 0.24.2, matplotlib 3.0.2, seaborn 0.9.0) and it looks OK. Also, an upgrade in a library won't affect you as long as you don't install it. Maybe something else changed? – Josh Friedlander May 10 '19 at 13:58
  • yeah I havent downladed anything. Do you see the dates in the blue barchart in perfect order? Cause I see 01-12-2018, 10-10-2018, 28-02-2018, 31-12-2018. – Heidi Falkeborg May 13 '19 at 05:48
  • 1
    Hey, you're correct that the dates are out of order. In fact they were out of order in my original answer, too! You can fix this by first converting to `datetime`, sorting, and then converting back to string (this last bit isn't strictly necessary, but you did start off with strings, and seaborn gives weird results otherwise). I'll add the lines to do this to my answer. I hope this is clear, if not feel free to ask. :) – Josh Friedlander May 13 '19 at 11:28