Angie
Angie

Reputation: 348

Plot multiple lines from one data frame

I have all the data I want to plot in one pandas data frame, e.g.:

         date flower_color  flower_count
0  2017-08-01         blue             1
1  2017-08-01          red             2
2  2017-08-02         blue             5
3  2017-08-02          red             2

I need a few different lines on one plot: x-value should be the date from the first column and y-value should be flower_count, and the y-value should depend on the flower_color given in the second column.

How can I do that without filtering the original df and saving it as a new object first? My only idea was to create a data frame for only red flowers and then specifying it like:

figure.line(x="date", y="flower_count", source=red_flower_ds)
figure.line(x="date", y="flower_count", source=blue_flower_ds)

Upvotes: 3

Views: 4434

Answers (2)

Wenlong Liu
Wenlong Liu

Reputation: 444

If my understanding is right, you need a plot with two subplots. The X for both subplots are dates, and the Ys are the flower counts for each color?

In this case, you can employ the subplots in pandas visualization.

fig, axes = plt.subplots(2)
z[z.flower_color == 'blue'].plot(x=['date'], y= ['flower_count'],ax=axes[0]).set_ylabel('blue')
z[z.flower_color == 'red'].plot(x=['date'], y= ['flower_count'],ax=axes[1]).set_ylabel('red')

plt.show()

The output will be like:

enter image description here

Hope it helps.

Upvotes: 2

Vaishali
Vaishali

Reputation: 38425

You can try this

fig, ax = plt.subplots()
for name, group in df.groupby('flower_color'):
    group.plot('date', y='flower_count', ax=ax, label=name)

enter image description here

Upvotes: 8

Related Questions