0

I have a sample data frame presented as ;

df=pd.DataFrame({'day': [1, 1, 2, 2, 3, 3, 4, 4],
                 'type': ['car', 'bike', 'car', 'bike', 'car', 'bike', 'car', 'bike'],
                 'percent': [21, 23, 56, 3, 15, 6, 7, 19]}

I need to make a line plot (Y=percent) vs. (X=day), we need two lines in the same plot colored by the variable type. Can we do it using Matplotlib? I tried the following code;

fig, ax =plt.subplots(figsize=(12,4))
plt.plot("day", "percent", color='type',data=df)
ax.set_title("Percentage of Deliveries")

ax.set_ylabel("Delivery Percent")

plt.show()

But I am getting an error;

Invalid RGBA argument: 'type'

Kindly help.

ywbaek
  • 2,971
  • 3
  • 9
  • 28
jay
  • 1,319
  • 6
  • 23
  • 42

1 Answers1

1

If your third variable was not comprised of string categories, you could do

plt.scatter("day", "percent", c='type',data=df)

But this will error for your example because type column values are strings. What you're really looking to do is plot by a grouping (Scatter plots in Pandas/Pyplot: How to plot by category). You can group first then loop over the groups to plot:

grouped=df.groupby('type')
for name, group in grouped:
    plt.plot(group.day,group.percent)
chris
  • 1,267
  • 7
  • 20
  • Appreciate your reply, actually I need to make a line plot, apologize if it was not clear.will the approach still be identical? kindly advice. In other words, I need to make 2 line plots in the same figure, of different colors – jay Jun 30 '20 at 22:41
  • When you loop over the groups, every time you call `plt.plot()` it will plot a new line with a new color. – chris Jul 01 '20 at 13:43