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.

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

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
When you loop over the groups, every time you call plt.plot() it will plot a new line with a new color.

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.