4

I have data that looks like this

Date         Fruit
2017-01-01   Orange
2017-01-01   Apple
2017-01-08   Orange
2017-01-09   Orange
2017-01-09   Apple

I want to plot Number of Oranges, Number of Apples by Date in a single plot. How would I do that?

I grouped them by Date and I see the result.

df.groupby(['Date','Fruit']).size()
Date         Fruit
2017-01-01   Orange  1
             Apple   1
2017-01-08   Orange  1
2017-01-09   Orange  1
             Apple   1

I tried this but it gives a bar plot having two categories but not against the dates.

sns.catplot(x="Fruit", hue="Fruit", kind="count",
            palette="pastel", edgecolor=".6",
            data=df);

How can plot a graph have Date on the x-axis and number of apples and the number of oranges for each date?

2 Answers 2

3

Framing the dataset:

df = pd.DataFrame(columns=["Date", "Fruit"], data=[['2017-01-01','Orange'], ['2017-01-01','Orange'], ['2017-01-01','Apple'], ['2017-01-08','Orange'], ['2017-01-09','Orange'], ['2017-01-09','Apple']])

The data looks like this

By using unstack and group by a bar plot can be drawn:

(df
 .groupby(['Date', 'Fruit'])
 .size()
 .unstack()
 .plot.bar()
)

Here is the plot by date

Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this.

# dummy data
date_range = pd.date_range('2019-01-01', '2019-01-06', freq='D')
df = pd.DataFrame(['Orange', 'Apple', 'Orange', 'Orange',
                   'Apple', 'Apple', 'Apple', 'Orange', 'Orange'],
    index=[date_range[0], date_range[0], date_range[1], date_range[2],
           date_range[2], date_range[2], date_range[2], date_range[3],
           date_range[3]],
    columns=['Fruit'])
df.index.name = 'Date'

groupby as you do, then unstack, which looks like this.

>>> print(df.unstack())

Fruit       Apple  Orange
Date                     
2019-01-01    1.0     1.0
2019-01-02    NaN     1.0
2019-01-03    3.0     1.0
2019-01-04    NaN     2.0

And then plot the unstacked data.

df.unstack().plot(kind='bar')
plt.show()

enter image description here

(You'll have to do something about the date formatting though).

1 Comment

Hi Chris, Thank you. That made sense. I had tried a similar way and I think it worked. Posting the solution below.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.