Feb-28-2023, 05:38 PM
(Feb-28-2023, 11:31 AM)get2sid Wrote:(Feb-22-2023, 08:14 PM)devansing Wrote: Hi,
I am trying to figure out how to create a graph similar to the following:
where the x axis has the monthly time data and the y axis represents the Dow Jones average (DJI column on my sample data). I am trying to get the recession months to plot as vertical bars corresponding to the "Regime" column in my sample data.
Here is the code; files are also attached – any help would be greatly appreciated:
import pandas as pd import plotly.graph_objects as go import matplotlib as mpl %matplotlib inline import matplotlib.pyplot as plt from matplotlib.pyplot import figure import datetime from datetime import date import numpy as np #import data history = pd.read_csv("sample_data.csv") history = history.replace(np. nan,'',regex = True) #filter out recession dates recession = history.loc[history["Regime"] == "Recession", "Date"] #plot axvspan for every recession month for month in recession: plt.axvspan(recession, recession + datetime.timedelta(days=1), color="grey", alpha=0.5) plt.show()
Hi,
To create a graph similar to the one you provided, you can use Plotly to plot the Dow Jones average (DJI) against monthly time data and use Matplotlib to add vertical bars for the recession months. Here's an example code that you can modify to fit your data:
import pandas as pd
import plotly.graph_objects as go
import matplotlib.pyplot as plt
import datetime
# Load data
history = pd.read_csv("sample_data.csv")
history["Date"] = pd.to_datetime(history["Date"], format="%Y-%m-%d")
history.set_index("Date", inplace=True)
# Filter out recession dates
recession = history.loc[history["Regime"] == "Recession"].index
# Plot Dow Jones average against time
fig = go.Figure()
fig.add_trace(go.Scatter(x=history.index, y=history["DJI"], mode="lines"))
# Add vertical bars for recession months
for month in recession:
plt.axvspan(month, month + pd.offsets.MonthEnd(0), color="grey", alpha=0.5)
# Update layout
fig.update_layout(
xaxis=dict(title="Date"),
yaxis=dict(title="Dow Jones Average"),
title="Dow Jones Average over Time",
)
# Show plot
fig.show()
In this code, we first load the data and filter out the recession dates. We then use Plotly to plot the Dow Jones average against time. Finally, we use Matplotlib to add vertical bars for the recession months.
Note that we use pd.offsets.MonthEnd(0) to find the end of the current month for each recession date. This ensures that the vertical bars cover the entire month.
Also, if you are using Jupyter Notebook or a similar environment, you may need to add the following code at the beginning of your notebook to display Plotly graphs properly:
import plotly.io as pio
pio.renderers.default = "notebook"
I hope this helps! Let me know if you have any questions.
Thanks for this – I typically use Matplotlib and like this approach! However, I ran your code and it produced two separate charts – one for the Dow Jones and the other for the recession bars?