jod51
jod51

Reputation: 111

How to plot line connecting tops of bar plot

I have a bar plot that I want to plot a line over that connects the top of the bars to see a general trend. My code currently looks like this (note - ratio is a pandas dataframe with column data_points):

ratio['data_points'].plot(kind='bar')
ratio['data_points'].plot()
plt.title('')
plt.show()

And I get back a plot that looks like this: enter image description here

I'm not sure how to alter this so that the bar plot shows properly (ie, the full bar for Week 1 and Week 4 are actually on the plot) and how to make it so that the line plot just connects the tops of the bars (the fall to 0 on week 3 is fine still - I don't need week 2 to connect to week 4 unless that's the only way to do this)

Upvotes: 2

Views: 1674

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can do:

  1. plot line first, with higher zorder than bar
  2. plot bar in different color after, which help realign the xaxis automatically

Like this:

ratio.data_points.plot(zorder=5)
ratio['data_points'].plot(kind='bar', color='C2', zorder=1)
plt.title('')
plt.show()

Output:

enter image description here

Upvotes: 2

Related Questions