Compustretch
Compustretch

Reputation: 127

Can't add 2nd plot to Log Transform figure

Per below code, plotting the log transformation of historical price data works fine, however when any 2nd plot (eg. closing price) is added to the figure, log transform is plotted as ts=0 ?!

Code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plot

df = pd.read_csv('historical_price_data.csv')
df = df[::-1]
df['Date'] = pd.to_datetime(df.Date, infer_datetime_format=True) 

# Set index to Date returns KeyError: "['Date'] not in index"
# df.set_index('Date', inplace=True)
df.sort_index(inplace=True)

# Log Transform 
log_transform = df['Close']
df['log'] = np.log(log_transform)

# Log transform plots fine by itself
ax = df[['Date', 'log']].plot(figsize=(14, 7), x='Date')

# Adding another plot to the figure results in log_transform being set to 0 !
ax = df[['Date', 'Close']].plot(figsize=(14, 7), x='Date', ax=ax)

plot.show()

Log Transformation plots by itself

Adding 2nd plot breaks Log Transformation plot

Just to note that plotting them with a single loc instead of 2 results in the same problem with the log transform line getting squashed to 0:

ax = df[['Date', 'log', 'Close']].plot(x='Date')

Upvotes: 1

Views: 61

Answers (3)

Sandipan Dey
Sandipan Dey

Reputation: 23101

Yet another syntax:

df['rolling_mean'] = df.Close.rolling(window=7).mean()
df.plot(x='Date', y=['Close', 'rolling_mean'], logy=True, figsize=(15,5))

with a simulated dataset:

enter image description here

Upvotes: 0

DavidG
DavidG

Reputation: 25362

If you wanted to plot your data on the same subplot, rather than on 2 separate ones, you can create a secondary y axis using ax.twinx(). You can than pass this new axes as an argument to the plotting function using ax=:

ax = df[['Date', 'log']].plot(figsize=(14, 7), x='Date')

ax2 = ax.twinx() # create seconday y axis and pass it into the plotting function below
ax2 = df[['Date', 'Close']].plot(figsize=(14, 7), x='Date', ax=ax2)

Upvotes: 1

Andrey Portnoy
Andrey Portnoy

Reputation: 1509

Your log-transformed data is orders of magnitude smaller (by definition almost) than the original data, so it's visually collapsed on the plot: Close peaks at about 20000, while log peaks at about 10.

It rarely makes sense to compare the original data to the log transform on the same plot.

If you ever fix your index issue, use the following to plot on separate plots:

df['log'] = np.log(df['Close'])
df[['Close', 'log']].plot(figsize=(14, 7), subplots=True)

Upvotes: 1

Related Questions