问题
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?
回答1:
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']])
By using unstack and group by a bar plot can be drawn:
(df
.groupby(['Date', 'Fruit'])
.size()
.unstack()
.plot.bar()
)
回答2:
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()
(You'll have to do something about the date formatting though).
来源:https://stackoverflow.com/questions/56050037/how-to-plot-categorical-variable-against-a-date-column-in-python