Vishal Naik
Vishal Naik

Reputation: 134

How to plot the Data For given DataFrame?

This is the main DataFrame I am working with:

     -   Date     Open    High    Low   Close
1. 01-08-2019 | 97.85 | 98.45 | 96.40 |97.25
2. 02-08-2019 | 97.15 | 98.95 | 96.75 |98.15
3. 05-08-2019 | 98.30 | 98.70 | 94.30 |95.65
4. 06-08-2019 | 95.75 | 97.75 | 95.20 |97.05
5. 07-08-2019 | 96.80 | 97.70 | 96.05 |96.90
6. 08-08-2019 | 97.40 | 98.90 | 96.55 |97.40
7. 09-08-2019 | 97.20 | 98.10 | 96.65 |97.30
8. 12-08-2019 | 97.20 | 97.25 | 93.40 |93.75
9. 13-08-2019 | 93.70 | 96.60 | 93.15 |96.35

I have extracted High, Low, Close of Daily,3Days,Weekly,Monthly Data from above DataFrame. How Do I plot the Daily high low Close, Weekly High, Low, Close and Monthly High Low Close in single plot ?

Daily High Low Close

      Date      High    Low     Close
0   2019-08-01  98.45   96.40   97.25  
1   2019-08-02  98.95   96.75   98.15  
2   2019-08-05  98.70   94.30   95.65  
3   2019-08-06  97.75   95.20   97.05  
4   2019-08-07  97.70   96.05   96.90  

Weekly High, Low, Close

    Date        High    Low    Close  
0   2019-08-04  98.95   96.75   98.15  
1   2019-08-11  98.90   96.65   97.40  
2   2019-08-18  97.25   94.15   96.35  
3   2019-08-25  93.85   92.25   93.50  
4   2019-09-01  94.85   93.40   94.20  

Monthly high Low Close

     Date        High    Low    Close
0   2019-08-31  98.95   96.75   98.15
1   2019-09-30  104.90  102.08  103.83
2   2019-10-31  102.90  100.15  102.05
3   2019-11-30  118.90  117.95  118.55
4   2019-12-31  138.40  135.85  135.90

Link to CSV file I am working with

Upvotes: 1

Views: 72

Answers (3)

r-beginners
r-beginners

Reputation: 35115

This is the first time I have created a graph with multiple x-axes, and I did a lot of research. I have a few issues to answer, such as validating the data and improving the display of the x-axis. I customized it based on these answers.

import pandas as pd
import numpy as np

df= pd.read_csv('../../../Downloads/US Coffee Aug19-Jul20 - US Coffee Aug19-Jul20.csv')
df['Date'] = pd.to_datetime(df['Date'])
weeks = df.resample('W-Mon', on='Date')[['High','Close']].agg('mean').reset_index().sort_values(by='Date')
months = df.resample('1M', on='Date')[['High','Close']].agg('mean').reset_index().sort_values(by='Date')

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(16,9),dpi=144)
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()
ax3 = ax1.twiny()

weeks_tick_locations = weeks['Date']
months_tick_locations = months['Date']

ax1.plot(df['Date'], df['High'], lw=1, color='b', label='Daily High')
ax1.plot(df['Date'], df['Close'], lw=1, color='r', label='Daily Low')

ax2.plot(weeks['Date'], weeks['High'], lw=2, color='g', label='Week High')
ax2.plot(weeks['Date'], weeks['Close'], lw=2, color='y', label='Week Low')

ax3.plot(months['Date'], months['High'], lw=2, color='k', label='Month High')
ax3.plot(months['Date'], months['Close'], lw=2, color='m', label='Month Low')

# Move twinned axis ticks and label from top to bottom
ax2.xaxis.set_ticks_position("bottom")
ax2.xaxis.set_label_position("bottom")

# Offset the twin axis below the host
ax2.spines["bottom"].set_position(("axes", -0.1))

ax2.set_frame_on(True)
ax2.patch.set_visible(False)

for sp in iter(ax2.spines.values()):
    sp.set_visible(False)
ax2.spines["bottom"].set_visible(True)

ax2.set_xticks(weeks_tick_locations)
ax2.set_xticklabels(weeks_tick_locations)
ax2.set_xlabel('Week')

ax3.xaxis.set_ticks_position("bottom")
ax3.xaxis.set_label_position("bottom")

# Offset the twin axis below the host
ax3.spines["bottom"].set_position(("axes", -0.2))

ax3.set_frame_on(True)
ax3.patch.set_visible(False)

for sp in iter(ax3.spines.values()):
    sp.set_visible(False)
ax3.spines["bottom"].set_visible(True)

ax3.set_xticks(months_tick_locations)
ax3.set_xticklabels(months_tick_locations)
ax3.set_xlabel('Month')

fig.legend(loc='upper left', bbox_to_anchor=(0.88, 0.98), bbox_transform=ax1.transAxes,)
plt.show()

enter image description here

Upvotes: 1

user8560167
user8560167

Reputation:

Remember to delete variables for 'fake' today date, I had to do it because this dataset is not up to date

import datetime
import matplotlib.pyplot as plt
from datetime import date
from datetime import datetime
from datetime import timedelta

today1 = date.today() #run in up to date dataframe , change to 'today' and remove fake today below

today = '2019-08-06'
w_today = '2019-08-06'
m_today = '2019-08-06'

today  = datetime.strptime(today,'%Y-%m-%d')
today = today.date()
w_today  = datetime.strptime(w_today,'%Y-%m-%d')
w_today = w_today.date()
m_today  = datetime.strptime(m_today,'%Y-%m-%d')
m_today = m_today.date()


df= pd.read_csv('D:/stack_overflow/US Coffee Aug19-Jul20.csv')
df['Date']=pd.to_datetime(df['Date']).dt.date
# df['Date']= df['Date'].floor('D')

#df from today , each day DAILY
df_daily = df[
    df.Date>=today
]

df_daily = df_daily.reset_index(drop=True)

weekly = []
monthly = []

last_date = df_daily['Date'].iloc[-1]

while w_today < last_date:
    w_today+=timedelta(days=7)
    weekly.append(w_today)


while m_today < last_date:
    m_today+=timedelta(days=10)
    monthly.append(m_today)

df_weekly = df_daily[
    df_daily.Date.isin(weekly)
]

df_monthly = df_daily[
    df_daily.Date.isin(monthly)
]
fig, axs = plt.subplots(3)
axs[0].plot(df_daily['Date'],df_daily[['Open','High','Low' ,'Close']])
axs[0].set_title('Daily')
axs[1].plot(df_weekly['Date'],df_weekly[['Open','High','Low' ,'Close']])
axs[1].set_title('Weekly')
axs[2].plot(df_monthly['Date'],df_monthly[['Open','High','Low' ,'Close']])
axs[2].set_title('Monthly')

plt.show()

enter image description here

Upvotes: 1

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

Re-use the ax object to plot in the same figure:

import pandas as pd
import matplotlib.pyplot as plt

daily_df = pd.DataFrame([
   ["2019-08-01", 98.45, 96.40, 97.25],
   ["2019-08-02", 98.95, 96.75, 98.15],
   ["2019-08-05", 98.70, 94.30, 95.65],
   ["2019-08-06", 97.75, 95.20, 97.05],
   ["2019-08-07", 97.70, 96.05, 96.90]
], columns=["Date", "daily_High", "daily_Low", "daily_Close"]
)

weekly_df = pd.DataFrame([
   ["2019-08-04", 98.95, 96.75, 98.15],
   ["2019-08-11", 98.90, 96.65, 97.40],
   ["2019-08-18", 97.25, 94.15, 96.35],
   ["2019-08-25", 93.85, 92.25, 93.50],
   ["2019-09-01", 94.85, 93.40, 94.20]
], columns=["Date", "weekly_High", "weekly_Low", "weekly_Close"]
)

monthly_df = pd.DataFrame([
   ["2019-08-31", 98.95 , 96.75 , 98.15],
   ["2019-09-30", 104.90, 102.08, 103.83],
   ["2019-10-31", 102.90, 100.15, 102.05],
   ["2019-11-30", 118.90, 117.95, 118.55],
   ["2019-12-31", 138.40, 135.85, 135.90]
], columns=["Date", "monthly_High", "monthly_Low", "monthly_Close"]
)

daily_df["Date"] = pd.to_datetime(daily_df["Date"], format='%Y-%m-%d')
weekly_df["Date"] = pd.to_datetime(weekly_df["Date"], format='%Y-%m-%d')
monthly_df["Date"] = pd.to_datetime(monthly_df["Date"], format='%Y-%m-%d')

daily_df.set_index(["Date"], inplace=True)
weekly_df.set_index(["Date"], inplace=True)
monthly_df.set_index(["Date"], inplace=True)

ax = daily_df.plot()
weekly_df.plot(ax=ax)
monthly_df.plot(ax=ax)

plt.show()

enter image description here

Upvotes: 1

Related Questions