python - How to add a line on top of a bar chart? -
i have barplot on top of draw line segment between 2 coordinates (i.e., in same figure). tried adding plt statement, line not drawn.
plt.figure(figsize=(12, 10)) ax = freq_series.plot(kind='bar',color=colors) plt.plot([coord_x1,coord_y1], [coord_x2, coord_y2], "r--")
update:
import datetime import matplotlib mpl import matplotlib.pyplot plt mpl.style.use('ggplot') d = { datetime.date(2015, 6, 21): 101.0, datetime.date(2015, 6, 22): 81.0, datetime.date(2015, 6, 23): 94.0, datetime.date(2015, 4, 24): 67.5, datetime.date(2015, 6, 26): 99.1 } df = pd.dataframe({'date': [x x in d.keys()], 'val':[x x in d.values()]}) %matplotlib ax = df.set_index('date').plot.bar(rot=0, figsize=(12, 10)) x = [ax.patches[0].get_x(), ax.patches[-1].get_x() + ax.patches[-1].get_width()] y = [df.val.max()] * 2 plt.plot(x, y, 'r--', c='g', linewidth=4) #plt.plot([ax.patches[0].get_width(), ax.patches[-1].get_width()], [y,y], 'r--', c='k') old answer:
here small demo:
import matplotlib mpl import matplotlib.pyplot plt mpl.style.use('ggplot') df = pd.dataframe(np.random.randint(0, 10, size=(5)), columns=list('a')) ax = df.plot.bar(figsize=(12, 10)) coord_x1 = 0.5 coord_y1 = 7.5 coord_x2 = 4.5 coord_y2 = 7.5 plt.plot([coord_x1, coord_x2], [coord_y1, coord_y1], '-o') 

Comments
Post a Comment