Reputation: 197
I have the following Dataframe:
Food Men Women Year
0 Apples as fruit 89.18 90.42 1994
1 Berries 84.21 81.73 1994
2 Grapes 88.79 88.13 1994
3 Melons 80.74 84.96 1994
4 Oranges, Total 85.66 89.77 1994
5 Other citrus fruit 79.82 80.64 1994
6 Stone fruit 88.95 89.55 1994
7 Tropical fruit 74.61 80.40 1994
8 Apples as fruit 90.86 91.21 1994
9 Berries 88.57 88.29 2004
10 Grapes 88.55 90.14 2004
11 Melons 79.72 80.99 2004
12 Oranges, Total 84.46 88.07 2004
13 Other citrus fruit 73.74 69.60 2004
14 Stone fruit 94.02 87.94 2004
15 Tropical fruit 74.58 85.85 2004
I am trying to create a line plot of Fruits vs Year or time with the Food as different line colors. Following the answer here and using groupby, I tried the following:
for i, group in fruits.groupby('Food'):
plt.figure()
group.plot(x='Year', y='Men', title=str(i))
But this yields 8 graphs with one line, or one graph for each fruit. I want one graph with a different line for each fruit. How would I do this with pandas, matplotlib or numpy? Also, the year column in currently an int so the plot includes years like 1996, 1998, 2000 etc, which I don't have data for. I meant for it to be a range like 1993-1994 or 2003-2004. Is there any way to omit them?
Upvotes: 4
Views: 1537
Reputation: 339230
You would need to create the figure outside the loop. It is then best to supply a matplotlib axes to the Dataframe plot using ax
keyword argument.
import pandas as pd
import matplotlib.pyplot as plt
year=[1994,1994,1994,2000,2000,2000,2006,2006,2006]
fr = ["Apple", "Banana", "Cherry"]*3
men = [1,6,6,3,5,8,7,2,7]
fruits =pd.DataFrame({"Year" : year, "Food" : fr, "Men" : men})
fig, ax = plt.subplots()
for i, group in fruits.groupby('Food'):
group.plot(x='Year', y='Men', ax=ax, label=group["Food"].iloc[0])
ax.set_title("Food")
ax.set_ylabel("Men")
#optionally only set ticks at years present in the years column
ax.set_xticks(fruits["Year"].unique())
plt.show()
Upvotes: 2