0

I have two time series data y en y1. The problem is that y is in a range from 400 to 600 and y1 in a range from 9 to 18 so while plotting I can't make a good comparison. I'm wondering if there is a technique to scale the plot to the time series without changing the value of y1 (like y1**2). The code:

y = pd.Series(np.random.randint(400, high=600, size=255))
y1 = pd.Series(np.random.randint(9, high=18, size=255))
date_today = datetime.now()
x = pd.date_range(date_today, date_today + timedelta(254), freq='D')
plt.plot(x,y,color = 'r',\
label = 'Stock',linewidth = 2)
plt.plot(x,y1,color = 'k',\
label = 'Index',linewidth = 2)
plt.title('Stock versus Index', fontsize=24,fontweight='bold')
plt.grid(linewidth=1.5)

Output:

enter image description here

JayDough
  • 91
  • 1
  • 10

2 Answers2

1

Based on the official example from pylab (here), I created a working solution for you as below:

from datetime import datetime
from datetime import timedelta
import pandas as pd

f, (ax, ax2) = plt.subplots(2, 1, sharex=True, figsize=(8, 6))

y = pd.Series(np.random.randint(400, high=600, size=255))
y1 = pd.Series(np.random.randint(9, high=18, size=255))
date_today = datetime.now()
x = pd.date_range(date_today, date_today + timedelta(254), freq='D')
ax.plot(x,y,color = 'r',label = 'Stock',linewidth = 2)
ax.plot(x,y1,color = 'k',label = 'Index',linewidth = 2)
ax2.plot(x,y,color = 'r',label = 'Stock',linewidth = 2)
ax2.plot(x,y1,color = 'k',label = 'Index',linewidth = 2)
ax.set_title('Stock versus Index', fontsize=24,fontweight='bold')

ax.grid(linewidth=1.5)
ax2.grid(linewidth=1.5)
ax.set_ylim(400, 620)  # upper data
ax2.set_ylim(5, 20)  # lower data 

# Merging and removing the middle horizontal axes
ax.spines['bottom'].set_visible(False)
ax2.spines['top'].set_visible(False)
ax.xaxis.tick_top()
ax.tick_params(labeltop='off') 
ax2.xaxis.tick_bottom()

# Modifying aesthetics of diagonal splitting lines
d = .01  
kwargs = dict(transform=ax.transAxes, color='k', clip_on=False)
ax.plot((-d, +d), (-d, +d), **kwargs)        
ax.plot((1 - d, 1 + d), (-d, +d), **kwargs)  
kwargs.update(transform=ax2.transAxes)  
ax2.plot((-d, +d), (1 - d, 1 + d), **kwargs)  
ax2.plot((1 - d, 1 + d), (1 - d, 1 + d), **kwargs)  

Output

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
1

You can use two scales.

Here is your code, changed to avoid panda as I'm not familiar with:

import numpy as np
import matplotlib.pyplot as plt
from datetime import *

#y = pd.Series(np.random.randint(400, high=600, size=255))
y = np.random.randint(400, high=600, size=255)
#y1 = pd.Series(np.random.randint(9, high=18, size=255))
y1 = np.random.randint(9, high=18, size=255)
date_today = datetime.now()
#x = pd.date_range(date_today, date_today + timedelta(254), freq='D')
x = [date_today + timedelta(days=x) for x in range(0, 255)]

fig, ax1 = plt.subplots()
ax1.plot(x,y,color = 'r',\
label = 'Stock',linewidth = 2)
ax1.set_ylabel('Stock', color='r')
ax1.tick_params('y', colors='r')

ax2 = ax1.twinx()
ax2.plot(x,y1,color = 'k',\
label = 'Index',linewidth = 2)
ax2.set_ylabel('Index', color='k')
ax2.tick_params('y', colors='k')

plt.title('Stock versus Index', fontsize=24,fontweight='bold')
plt.grid(linewidth=1.5)

plt.show()

enter image description here Of course you can change the axes range for ax1 and ax2 independently.

For reference, check: two_scales.py

hesham_EE
  • 1,125
  • 13
  • 24