1

While it seems not that difficult, for the life of me, I'm unable to figure this out. Any help will be greatly appreciated. Here is my scenario:

I have a dataframe, df1, that contains Date and Price. I can use this data to plot a graph using matplotlib.pyplot. Good so far.

I have another dataframe, df2, that contains again Date and Price information but only 6 rows. Now, I need to take the first two rows of df2and consider them as two points (x axis would be Date and y axis would be Price) and and connect them on the graph plotted above. Same with next two rows and so on. (there will be more rows in df2, but I hope you get the drift).

I tried calling plot() on both dfs one after the other - the result is that when df2.plot() is called, the graph plotted using df1.plot() is erased and df2 lines are plotted.

Below is how I need the result to look like. enter image description here

Pavan Kulkarni
  • 476
  • 5
  • 17

1 Answers1

4
df = pd.read_csv(r'https://vincentarelbundock.github.io/Rdatasets/csv/fpp2/goog200.csv', index_col=0)
df2 = df.loc[[57, 98, 169]]

plt.plot(df['time'], df['value'])
plt.plot(df2['time'], df2['value'])
plt.show()

enter image description here


Edit per comment

(Credit to user23564's linked answer in the comments to the OP)

df = pd.read_csv(r'https://vincentarelbundock.github.io/Rdatasets/csv/fpp2/goog200.csv', index_col=0)
df2 = df.loc[[57, 98, 169, 200]].reset_index()

plt.plot(df['time'], df['value'])
for i in range(0, len(df2), 2):
    plt.plot(df2.loc[i:i+1, 'time'], df2.loc[i:i+1, 'value'], c='grey')
plt.show()

enter image description here

Brendan
  • 3,901
  • 15
  • 23
  • Thanks Brendan! Though my mistake that I didn't upload the right image. I have edited the same. Could you pls check it? Thanks for your assistance – Pavan Kulkarni Jul 12 '19 at 13:27
  • Thanks Brendan! I ended up doing exactly this! Can't believe I couldn't figure such a simple solution earlier. Thanks for the help – Pavan Kulkarni Jul 12 '19 at 13:55