Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
superimpose two stock price graph

Hi all,

I'm trying to superimpose two stock price graphs, and as you can see below, it prints separately. I'd like to plot them together so I can easily compare. Any help or reference will be greatly appreciated. Thank you.

Best,
Harry

prices = get_pricing(['mu'], start_date="2018-1-29", end_date="2018-1-29", fields='price', frequency='minute')  
prices_et = prices.tz_convert('US/Eastern')  
prices_first_30min_0129 = prices_et.between_time('9:30','10:00')

prices = get_pricing(['mu'], start_date="2018-1-30", end_date="2018-1-30", fields='price', frequency='minute')  
prices_et = prices.tz_convert('US/Eastern')  
prices_first_30min_0130 = prices_et.between_time('9:30','10:00')

import matplotlib.pyplot as plt  
prices_first_30min_0129.plot()  
prices_first_30min_0130.plot()  
plt.show()  
9 responses

You need to add plt.legend to add things into one graph

Hi Harry,

Perhaps try applying the plt.figure object, for example:

fig = plt.figure(figsize=(12,6), dpi=100)  # Define .figure object size and resolution as fig  
ax1 = fig.add_axes([0, 0, 1, 1])  # Add ax2 for plot2 with [x_origin, y_origin, x_size, y_size] etc  
ax1.set_title('First 30min Prices', fontsize=16)  
ax1.plot(prices_first_30min_0129, color='red')  
ax1.plot(prices_first_30min_0130, color='blue');  

Add legend:

ax1.legend(['prices_first_30min_0129', 'prices_first_30min_0130'], ncol=1, fontsize=12, frameon=False);  

Note ";" at the end of .plot() will suppress the object ID from printing on screen.

Hope this helps :o) have fun!

Hi Karl,
Thank you for your help. It plots two figure together, but it is not superimposed since they to have different date. is there anyway we can adjust time? say I'm looking for plotting both in same time x-axis between 930am and 1000am even those data have different date. I tried to adjust but I could not figure out. Thank you for your willingness. I appreciate it. Best,

Yes sure, Harry are you ok to attach your Notebook so I may add the codes to re-post it for your checking?

Cheers

Sure thing.

prices = get_pricing(['mu'], start_date="2018-1-29", end_date="2018-1-29", fields='price', frequency='minute')  
prices_et = prices.tz_convert('US/Eastern')  
prices_first_30min_0129 = prices_et.between_time('9:30','9:35')

prices = get_pricing(['mu'], start_date="2018-1-30", end_date="2018-1-30", fields='price', frequency='minute')  
prices_et = prices.tz_convert('US/Eastern')  
prices_first_30min_0130 = prices_et.between_time('9:30','9:35')

import matplotlib.pyplot as plt  
fig = plt.figure(figsize=(12,6), dpi=100)  # Define .figure object size and resolution as fig  
ax1 = fig.add_axes([0, 0, 1, 1])  # Add ax2 for plot2 with [x_origin, y_origin, x_size, y_size] etc  
ax1.set_title('First 30min Prices', fontsize=16)  
ax1.plot(prices_first_30min_0129, color='red')  
ax1.plot(prices_first_30min_0130, color='blue');  

ax1.legend(['prices_first_30min_0129', 'prices_first_30min_0130'], ncol=1, fontsize=12, frameon=False);  

The result is not superimposed, since their x-axis is different. I really appreciate your timely response.

No worries, Harry - you only need to drop the DatetimeIndex so you may superimpose the two (or more) plots over the same X-axis - see attached Notebook.

Hope this helps.

This is what I was looking for. I really appreciate your help. Have great weekends.

Glad to know, Harry that it helps.

Given that the two price series are of the same .between_time('9:30', '9:35') at one or more days apart, you may also compare the prices simply by plotting an entirely new DataFrame (to which you may add more price series) - see updated Notebook attached.

Cheers!

Actually, this is a similar problem I has been working on and you gave me great help. Now, I am able to plot multiple dates data. You're awesome.