Reputation: 4208
below is the code to plot two stock curves.
study_symbol='TQQQ'
fig, ax1 = plt.subplots(figsize=(10, 6))
color = '#0072B2'
ax1.set_xlabel('Days')
ax1.set_ylabel('SPY', color=color)
ax1.plot(df2['Days'], df2['SPY'], color=color, ls='-', label='SPY')
ax1.scatter(df['Days'], df['SPY'], color=color, marker='o')
ax1.tick_params(axis='y', labelcolor=color)
ax1.legend(loc=(0.01, .94), frameon=False)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = 'r'
ax2.set_ylabel(study_symbol, color=color) # we already handled the x-label with ax1
ax2.plot(df2['Days'], df2[study_symbol], color=color, ls='--', label=study_symbol)
ax2.scatter(df['Days'], df[study_symbol], color=color, marker='x')
ax2.tick_params(axis='y', labelcolor=color)
ax2.legend(loc=(0.01, .89), frameon=False)
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title(r'$\bfStock\ of\ SPY$' +
'\ncorrelates with\n' +
r'$\bfStock\ of $'+r'$\bf\ '+study_symbol+'$')
plt.xticks(ticks=df['Days'].tolist(), labels=df['Days'].tolist())
after running the above code, it created a lot of texts like below. I am not sure how to avoid these text output. I just need the plot. Thanks
([<matplotlib.axis.XTick at 0x1e357c2a400>,
<matplotlib.axis.XTick at 0x1e357c2a3d0>,
<matplotlib.axis.XTick at 0x1e357ac70d0>,
<matplotlib.axis.XTick at 0x1e357c39d00>,
<matplotlib.axis.XTick at 0x1e357c8c280>,
<matplotlib.axis.XTick at 0x1e357c8cd30>,
<matplotlib.axis.XTick at 0x1e357c974c0>,
<matplotlib.axis.XTick at 0x1e357c97c10>,
<matplotlib.axis.XTick at 0x1e357c9d3a0>,
<matplotlib.axis.XTick at 0x1e357c97b50>,
<matplotlib.axis.XTick at 0x1e357c8ca30>,
<matplotlib.axis.XTick at 0x1e357c80df0>,
<matplotlib.axis.XTick at 0x1e357c9de50>,
<matplotlib.axis.XTick at 0x1e357de35e0>,
<matplotlib.axis.XTick at 0x1e357de3d30>,
...
[Text(0, 0, '0'),
Text(1, 0, '1'),
Text(2, 0, '2'),
Text(3, 0, '3'),
Text(4, 0, '4'),
Text(5, 0, '5'),
Text(6, 0, '6'),
Text(7, 0, '7'),
Upvotes: 1
Views: 1811
Reputation: 320
As Mohil Patel and bb1 point out, adding plt.show(); should do it for you.
fig.tight_layout() # otherwise the right y-label is slightly clipped
plt.title(r'$\bfStock\ of\ SPY$' +
'\ncorrelates with\n' +
r'$\bfStock\ of $'+r'$\bf\ '+study_symbol+'$')
plt.xticks(ticks=df['Days'].tolist(), labels=df['Days'].tolist())
plt.show()
Upvotes: 2